Advertisement
lraven

code for youtube

Mar 27th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import hexchat
  2. from apiclient.discovery import build
  3. from apiclient.errors import HttpError
  4.  
  5. # Galaxy's YouTube Display Script
  6. # v1.0 - First version
  7. # v1.1 - Title/nDuration -> Title [Duration]
  8. # - YouTube link can be anywhere in message [rather than just beginning and end]
  9. # - Consolidated to one function
  10. # - Added mobile YouTube link support
  11. # - Replaces unrecognized characters with '?' [for xChat]
  12. # v1.2 - now prevents recursion if youtube link is in youtube title [thanks Tyranisaur]
  13. # - only works for #gtsplus
  14. # - now displays duration is a fully readable fashion!
  15. #
  16. # Upcoming changes
  17. # imgur support
  18.  
  19. __module_name__ = "youtubeTitleDisplay"
  20. __module_version__ = "1.2"
  21. __module_description__ = "Displays title & duration of YouTube video linked in chat"
  22.  
  23. youtube = build("youtube", "v3", developerKey="AIzaSyBdObvQPz5ayaL2-RVv_jcSclOCCwJ3Mxw")
  24.  
  25. def youtube_cb(word, word_eol, userdata):
  26. if hexchat.get_info('channel') == '#gtsplus':
  27.  
  28. if "https://www.youtube.com/watch?v=" in word[1]:
  29. start = word[1].find('watch?v=')
  30. vid_id = word[1][start+8:start+19]
  31.  
  32. vid = youtube.videos().list(id=vid_id, part='snippet, contentDetails').execute()
  33. vid_data = [' ',' ']
  34. vid_dur = 'hour:minute:second'
  35.  
  36. for vid_res in vid.get("items", []):
  37. vid_data[0] = ("Title: %s" % (vid_res['snippet']['title']))
  38. vid_data[1] = ("%s" % (vid_res['contentDetails']['duration'])[2:])
  39.  
  40. hours=minutes=seconds=True
  41.  
  42. if "H" not in vid_data[1]:
  43. vid_dur = vid_dur.replace('hour:','')
  44. hours = False
  45. if "M" not in vid_data[1]:
  46. vid_dur = vid_dur.replace('minute','00')
  47. minutes = False
  48. if "S" not in vid_data[1]:
  49. vid_dur = vid_dur.replace('second','00')
  50. seconds = False
  51.  
  52. if hours:
  53. vid_dur = vid_dur.replace('hour',vid_data[1][0])
  54. vid_data[1] = vid_data[1][2:]
  55.  
  56. if minutes:
  57. start = vid_data[1].find('M')
  58. if (start - 1):
  59. vid_dur = vid_dur.replace('minute',vid_data[1][:2])
  60. vid_data[1] = vid_data[1][3:]
  61. else:
  62. vid_dur = vid_dur.replace('minute',vid_data[1][0])
  63. vid_data[1] = vid_data[1][2:]
  64.  
  65. if seconds:
  66. if len(vid_data[1]) == 3:
  67. vid_dur = vid_dur.replace('second',vid_data[1][0:2])
  68. else:
  69. vid_dur = vid_dur.replace('second',('0' + vid_data[1][0]))
  70.  
  71. if vid_data[0] not in word[1]:
  72. hexchat.command("bs say #gtsplus %s [%s]" % (vid_data[0], vid_dur))
  73.  
  74. if "://youtu.be/" in word[1]:
  75. start = word[1].find('.be/')
  76. vid_id = word[1][start+4:start+15]
  77.  
  78. vid = youtube.videos().list(id=vid_id, part='snippet, contentDetails').execute()
  79. vid_data = [' ',' ']
  80. vid_dur = 'hour:minute:second'
  81.  
  82. for vid_res in vid.get("items", []):
  83. vid_data[0] = ("Title: %s" % (vid_res['snippet']['title']))
  84. vid_data[1] = ("%s" % (vid_res['contentDetails']['duration'])[2:])
  85.  
  86. hours=minutes=seconds=True
  87.  
  88. if "H" not in vid_data[1]:
  89. vid_dur = vid_dur.replace('hour:','')
  90. hours = False
  91. if "M" not in vid_data[1]:
  92. vid_dur = vid_dur.replace('minute','00')
  93. minutes = False
  94. if "S" not in vid_data[1]:
  95. vid_dur = vid_dur.replace('second','00')
  96. seconds = False
  97.  
  98. if hours:
  99. vid_dur = vid_dur.replace('hour',vid_data[1][0])
  100. vid_data[1] = vid_data[1][2:]
  101.  
  102. if minutes:
  103. start = vid_data[1].find('M')
  104. if (start - 1):
  105. vid_dur = vid_dur.replace('minute',vid_data[1][:2])
  106. vid_data[1] = vid_data[1][3:]
  107. else:
  108. vid_dur = vid_dur.replace('minute',vid_data[1][0])
  109. vid_data[1] = vid_data[1][2:]
  110.  
  111. if seconds:
  112. if len(vid_data[1]) == 3:
  113. vid_dur = vid_dur.replace('second',vid_data[1][0:2])
  114. else:
  115. vid_dur = vid_dur.replace('second',('0' + vid_data[1][0]))
  116.  
  117. if vid_data[0] not in word[1]:
  118. hexchat.command("bs say #gtsplus %s [%s]" % (vid_data[0], vid_dur))
  119.  
  120. return hexchat.EAT_NONE
  121.  
  122. EVENTS = [("Channel Message"),("Your Message"),("Your Action"),("Channel Action")]
  123. for event in EVENTS:
  124. #if hexchat.get_info('channel') == '#gtsplus':
  125. hexchat.hook_print(event, youtube_cb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement