Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hexchat
- from apiclient.discovery import build
- from apiclient.errors import HttpError
- __module_name__ = "youtubeTitleDisplay"
- __module_version__ = "1.3"
- __module_description__ = "Displays title & duration of YouTube video linked in chat"
- youtube = build("youtube", "v3", developerKey="AIzaSyBdObvQPz5ayaL2-RVv_jcSclOCCwJ3Mxw")
- vid_id = ''
- channel = '#gtsplus'
- def link_type(message):
- if "://www.youtube.com/watch?v=" in message[1]:
- start = message[1].find('watch?v=')
- vid_id = message[1][start+8:start+19]
- elif "://youtu.be/" in message[1]:
- start = message[1].find('.be/')
- vid_id = message[1][start+4:start+15]
- return vid_id
- def youtube_cb(word, word_eol, userdata):
- if hexchat.get_info('channel') == channel:
- if ("://www.youtube.com/watch?v=" in word[1]) or ("://youtu.be/" in word[1]):
- vid_id = link_type(word)
- vid = youtube.videos().list(id=vid_id, part='snippet, contentDetails').execute()
- vid_data = [' ',' ']
- vid_dur = 'hour:minute:second'
- for vid_res in vid.get("items", []):
- vid_data[0] = ("Title: %s" % (vid_res['snippet']['title']))
- vid_data[1] = ("%s" % (vid_res['contentDetails']['duration'])[2:])
- hours=minutes=seconds=True
- if "H" not in vid_data[1]:
- vid_dur = vid_dur.replace('hour:','')
- hours = False
- if "S" not in vid_data[1]:
- vid_dur = vid_dur.replace('second','00')
- seconds = False
- if "M" not in vid_data[1]:
- if not hours and seconds:
- vid_dur = vid_dur.replace('minute','0')
- minutes = False
- else:
- vid_dur = vid_dur.replace('minute','00')
- minutes = False
- if hours:
- vid_dur = vid_dur.replace('hour',vid_data[1][0])
- vid_data[1] = vid_data[1][2:]
- if minutes:
- start = vid_data[1].find('M')
- if (start - 1):
- vid_dur = vid_dur.replace('minute',vid_data[1][:2])
- vid_data[1] = vid_data[1][3:]
- else:
- if not hours:
- vid_dur = vid_dur.replace('minute',vid_data[1][0])
- vid_data[1] = vid_data[1][2:]
- else:
- vid_dur = vid_dur.replace('minute',('0' + vid_data[1][0]))
- vid_data[1] = vid_data[1][2:]
- if seconds:
- if len(vid_data[1]) == 3:
- vid_dur = vid_dur.replace('second',vid_data[1][0:2])
- else:
- vid_dur = vid_dur.replace('second',('0' + vid_data[1][0]))
- if vid_data[0] not in word[1]:
- hexchat.command("bs say %s %s [%s]" % (channel, vid_data[0], vid_dur))
- return hexchat.EAT_NONE
- EVENTS = [("Channel Message"),("Your Message"),("Your Action"),("Channel Action"),("Channel Msg Hilight")]
- for event in EVENTS:
- hexchat.hook_print(event, youtube_cb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement