Change tabs to spaces

This commit is contained in:
dpedu 2015-08-31 20:33:13 -07:00
parent 2e25a7ed04
commit 58d5586713
1 changed files with 66 additions and 66 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
""" """
.. module:: Youtube .. module:: Youtube
:synopsis: Search youtube with .youtube/.yt :synopsis: Search youtube with .youtube/.yt
.. moduleauthor:: Dave Pedu <dave@davepedu.com> .. moduleauthor:: Dave Pedu <dave@davepedu.com>
@ -12,68 +12,68 @@ from requests import get
import time import time
class Youtube(ModuleBase): class Youtube(ModuleBase):
def __init__(self, bot, moduleName): def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName); ModuleBase.__init__(self, bot, moduleName);
self.hooks=[ModuleHook("PRIVMSG", self.youtube)] self.hooks=[ModuleHook("PRIVMSG", self.youtube)]
def youtube(self, args, prefix, trailing): def youtube(self, args, prefix, trailing):
cmd = self.bot.messageHasCommand(".youtube", trailing) cmd = self.bot.messageHasCommand(".youtube", trailing)
if not cmd: if not cmd:
cmd = self.bot.messageHasCommand(".yt", trailing) cmd = self.bot.messageHasCommand(".yt", trailing)
if cmd and args[0][0:1]=="#": if cmd and args[0][0:1]=="#":
#TOTO search youtube #TOTO search youtube
if cmd.args_str.strip() =="": if cmd.args_str.strip() =="":
self.bot.act_PRIVMSG(args[0], '.youtube <query> -- returns the first YouTube search result for <query>') self.bot.act_PRIVMSG(args[0], '.youtube <query> -- returns the first YouTube search result for <query>')
return return
j = get("http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1", params={"q":trailing}).json() j = get("http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1", params={"q":trailing}).json()
if 'error' in j or j['data']['totalItems']==0: if 'error' in j or j['data']['totalItems']==0:
self.bot.act_PRIVMSG(args[0], "YouTube: No results found.") self.bot.act_PRIVMSG(args[0], "YouTube: No results found.")
else: else:
vid_id = j['data']['items'][0]['id'] vid_id = j['data']['items'][0]['id']
self.bot.act_PRIVMSG(args[0], "http://youtu.be/%s :: %s" % (vid_id, self.get_video_description(vid_id))) self.bot.act_PRIVMSG(args[0], "http://youtu.be/%s :: %s" % (vid_id, self.get_video_description(vid_id)))
def get_video_description(self, vid_id): def get_video_description(self, vid_id):
j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json() j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json()
if j.get('error'): if j.get('error'):
return return
j = j['data'] j = j['data']
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title'] out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title']
if not j.get('duration'): if not j.get('duration'):
return out return out
out += ' - length \x02' out += ' - length \x02'
length = j['duration'] length = j['duration']
if length / 3600: # > 1 hour if length / 3600: # > 1 hour
out += '%dh ' % (length / 3600) out += '%dh ' % (length / 3600)
if length / 60: if length / 60:
out += '%dm ' % (length / 60 % 60) out += '%dm ' % (length / 60 % 60)
out += "%ds\x02" % (length % 60) out += "%ds\x02" % (length % 60)
if 'rating' in j: if 'rating' in j:
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'], out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
j['ratingCount']) j['ratingCount'])
if 'viewCount' in j: if 'viewCount' in j:
out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount']) out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount'])
upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z") upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
out += ' - \x02%s\x02 on \x02%s\x02' % ( out += ' - \x02%s\x02 on \x02%s\x02' % (
j['uploader'], time.strftime("%Y.%m.%d", upload_time)) j['uploader'], time.strftime("%Y.%m.%d", upload_time))
if 'contentRating' in j: if 'contentRating' in j:
out += ' - \x034NSFW\x02' out += ' - \x034NSFW\x02'
return out return out
def group_int_digits(self, number, delimiter=',', grouping=3): def group_int_digits(self, number, delimiter=',', grouping=3):
base = str(number).strip() base = str(number).strip()
builder = [] builder = []
while base: while base:
builder.append(base[-grouping:]) builder.append(base[-grouping:])
base = base[:-grouping] base = base[:-grouping]
builder.reverse() builder.reverse()
return delimiter.join(builder) return delimiter.join(builder)