Advertisement
lraven

youtube link

Jun 25th, 2016
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import hexchat
  2. from apiclient.discovery import build
  3. from apiclient.errors import HttpError
  4.  
  5. __module_name__ = "youtubeTitleDisplay"
  6. __module_version__ = "1.3"
  7. __module_description__ = "Displays title & duration of YouTube video linked in chat"
  8.  
  9. youtube = build("youtube", "v3", developerKey="AIzaSyBdObvQPz5ayaL2-RVv_jcSclOCCwJ3Mxw")
  10. vid_id = ''
  11. channel = '#gtsplus'
  12.  
  13. def link_type(message):
  14.  
  15. if "://www.youtube.com/watch?v=" in message[1]:
  16. start = message[1].find('watch?v=')
  17. vid_id = message[1][start+8:start+19]
  18. elif "://youtu.be/" in message[1]:
  19. start = message[1].find('.be/')
  20. vid_id = message[1][start+4:start+15]
  21.  
  22. return vid_id
  23.  
  24.  
  25. def youtube_cb(word, word_eol, userdata):
  26.  
  27. if hexchat.get_info('channel') == channel:
  28.  
  29. if ("://www.youtube.com/watch?v=" in word[1]) or ("://youtu.be/" in word[1]):
  30. vid_id = link_type(word)
  31. vid = youtube.videos().list(id=vid_id, part='snippet, contentDetails').execute()
  32. vid_data = [' ',' ']
  33. vid_dur = 'hour:minute:second'
  34.  
  35. for vid_res in vid.get("items", []):
  36. vid_data[0] = ("Title: %s" % (vid_res['snippet']['title']))
  37. vid_data[1] = ("%s" % (vid_res['contentDetails']['duration'])[2:])
  38.  
  39. hours=minutes=seconds=True
  40.  
  41. if "H" not in vid_data[1]:
  42. vid_dur = vid_dur.replace('hour:','')
  43. hours = False
  44. if "S" not in vid_data[1]:
  45. vid_dur = vid_dur.replace('second','00')
  46. seconds = False
  47. if "M" not in vid_data[1]:
  48. if not hours and seconds:
  49. vid_dur = vid_dur.replace('minute','0')
  50. minutes = False
  51. else:
  52. vid_dur = vid_dur.replace('minute','00')
  53. minutes = False
  54.  
  55. if hours:
  56. vid_dur = vid_dur.replace('hour',vid_data[1][0])
  57. vid_data[1] = vid_data[1][2:]
  58.  
  59. if minutes:
  60. start = vid_data[1].find('M')
  61. if (start - 1):
  62. vid_dur = vid_dur.replace('minute',vid_data[1][:2])
  63. vid_data[1] = vid_data[1][3:]
  64. else:
  65. if not hours:
  66. vid_dur = vid_dur.replace('minute',vid_data[1][0])
  67. vid_data[1] = vid_data[1][2:]
  68. else:
  69. vid_dur = vid_dur.replace('minute',('0' + vid_data[1][0]))
  70. vid_data[1] = vid_data[1][2:]
  71.  
  72. if seconds:
  73. if len(vid_data[1]) == 3:
  74. vid_dur = vid_dur.replace('second',vid_data[1][0:2])
  75. else:
  76. vid_dur = vid_dur.replace('second',('0' + vid_data[1][0]))
  77.  
  78. if vid_data[0] not in word[1]:
  79. hexchat.command("bs say %s %s [%s]" % (channel, vid_data[0], vid_dur))
  80.  
  81. return hexchat.EAT_NONE
  82.  
  83. EVENTS = [("Channel Message"),("Your Message"),("Your Action"),("Channel Action"),("Channel Msg Hilight")]
  84. for event in EVENTS:
  85. hexchat.hook_print(event, youtube_cb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement