plurals and fanciness
This commit is contained in:
parent
85d8c4e5cb
commit
0bb1574ebd
1 changed files with 8 additions and 15 deletions
|
@ -13,6 +13,10 @@ search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1'
|
||||||
video_url = "http://youtu.be/%s"
|
video_url = "http://youtu.be/%s"
|
||||||
|
|
||||||
|
|
||||||
|
def plural(num=0, text=''):
|
||||||
|
return "%d %s%s" % (num, text, "s"[num==1:])
|
||||||
|
|
||||||
|
|
||||||
def get_video_description(video_id):
|
def get_video_description(video_id):
|
||||||
request = http.get_json(api_url.format(video_id))
|
request = http.get_json(api_url.format(video_id))
|
||||||
|
|
||||||
|
@ -35,12 +39,11 @@ def get_video_description(video_id):
|
||||||
out += "%ds\x02" % (length % 60)
|
out += "%ds\x02" % (length % 60)
|
||||||
|
|
||||||
if 'ratingCount' in data:
|
if 'ratingCount' in data:
|
||||||
ratings = data['ratingCount']
|
likes = plural(int(data['likeCount']), "like")
|
||||||
likes = int(data['likeCount'])
|
dislikes = plural(data['ratingCount'] - int(data['likeCount']), "dislike")
|
||||||
dislikes = ratings - likes
|
|
||||||
|
|
||||||
percent = 100 * float(likes)/float(ratings)
|
percent = 100 * float(data['likeCount'])/float(data['ratingCount'])
|
||||||
out += ' - {} likes, {} dislikes (\x02{:.1f}\x02%)'.format(likes,
|
out += ' - {}, {} (\x02{:.1f}\x02%)'.format(likes,
|
||||||
dislikes, percent)
|
dislikes, percent)
|
||||||
|
|
||||||
if 'viewCount' in data:
|
if 'viewCount' in data:
|
||||||
|
@ -61,16 +64,6 @@ def get_video_description(video_id):
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def GetInHMS(seconds):
|
|
||||||
hours = seconds / 3600
|
|
||||||
seconds -= 3600 * hours
|
|
||||||
minutes = seconds / 60
|
|
||||||
seconds -= 60 * minutes
|
|
||||||
if hours == 0:
|
|
||||||
return "%02d:%02d" % (minutes, seconds)
|
|
||||||
return "%02d:%02d:%02d" % (hours, minutes, seconds)
|
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(*youtube_re)
|
@hook.regex(*youtube_re)
|
||||||
def youtube_url(match):
|
def youtube_url(match):
|
||||||
return get_video_description(match.group(1))
|
return get_video_description(match.group(1))
|
||||||
|
|
Reference in a new issue