Skip to content

Commit cfe2cc3

Browse files
committed
Fix youtube.py nsfw tags
1 parent fed2bd5 commit cfe2cc3

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Fix matching exception in horoscope test
1616
- Fix youtube.py ISO time parse
1717
- Fix grammatical error in food sentence (beer)
18+
- Update youtube plugin to use proper contentRating API
1819

1920
## [1.3.0] 2020-03-17
2021
### Added

plugins/youtube.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from cloudbot import hook
77
from cloudbot.bot import bot
8-
from cloudbot.util import timeformat
8+
from cloudbot.util import colors, timeformat
99
from cloudbot.util.formatting import pluralize_auto
1010

1111
youtube_re = re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-zA-Z0-9]+)', re.I)
@@ -75,8 +75,13 @@ def get_video_description(video_id):
7575
out += ' - \x02{}\x02 on \x02{}\x02'.format(uploader,
7676
upload_time.strftime("%Y.%m.%d"))
7777

78-
if 'contentRating' in content_details:
79-
out += ' - \x034NSFW\x02'
78+
try:
79+
yt_rating = content_details['contentRating']['ytRating']
80+
except KeyError:
81+
pass
82+
else:
83+
if yt_rating == "ytAgeRestricted":
84+
out += colors.parse(' - $(red)NSFW$(reset)')
8085

8186
return out
8287

tests/plugin_tests/test_youtube.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_success_nsfw(self, mock_requests, mock_api_keys):
159159
from plugins import youtube
160160

161161
data = deepcopy(video_data)
162-
data['items'][0]['contentDetails']['contentRating'] = "18+"
162+
data['items'][0]['contentDetails']['contentRating'] = {"ytRating": "ytAgeRestricted"}
163163

164164
mock_requests.add(
165165
'GET',
@@ -172,7 +172,7 @@ def test_success_nsfw(self, mock_requests, mock_api_keys):
172172
'\x02some title\x02 - length \x0217m 2s\x02 - '
173173
'4,633 likes, 31 dislikes (\x0299.3\x02%) - '
174174
'\x0268,905\x02 views - \x02a channel\x02 on \x022019.10.10\x02 - '
175-
'\x034NSFW\x02'
175+
'\x0304NSFW\x0f'
176176
)
177177

178178
assert youtube.get_video_description('phL7P6gtZRM') == result

0 commit comments

Comments
 (0)