Advertisement
here2share

# Tk_youtube_add2playlist.py

Oct 10th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. # Tk_youtube_add2playlist.py
  2.  
  3. from Tkinter import *
  4. from urllib2 import urlopen
  5. import re
  6. import webbrowser
  7. import tempfile
  8. import subprocess # to execute the js file as only in the background
  9.  
  10. tf = tempfile.mktemp(".js", "youtube_")
  11.  
  12. yt = 'https://www.youtube.com/watch?v='
  13. ya = 'https://www.youtube.com/playlist?list='
  14.  
  15. '''https://www.youtube.com/playlist?list=PL3dxi8yxvrTBg9g6ZrqsumlLbYuIWjV09'''
  16.  
  17. root = Tk()
  18. label = LabelFrame(root, text=' youtube urls to playlist ', height=200, width=800, bg="aqua")
  19. label.pack(fill=X)
  20. label.pack_propagate(False)
  21.  
  22. def inEntry(z):
  23.     try:
  24.         z = str(z.get())
  25.     except: pass
  26.     try:
  27.         z = z[z.index('=')+1:]
  28.         try: z = z[:z.index('&')]
  29.         except: pass
  30.     except: pass
  31.     return z.strip()
  32.  
  33. WL = 'Watch Later Playlist'
  34. def getA(z):
  35.     if not z.strip() or z == WL:
  36.         z = 'WL'
  37.         stA.set(WL)
  38.     return z
  39.  
  40. def getB(z): # ZZZ
  41.     if not z.strip():
  42.         z = root.clipboard_get()
  43.         stB.set(inEntry(z))
  44.     return z
  45.  
  46. def verify():
  47.     oB = inEntry(stB)
  48.     if oB:
  49.         webbrowser.open(ya+oB)
  50.     else: print "*** entry is invalid ***"
  51. #
  52. def addto():
  53.     oA = getA(inEntry(stA))
  54.     oB = getB(inEntry(stB))
  55.     root.update()
  56.     if oB:
  57.         h = html.replace('***AAA***',oA).replace('***BBB***',oB)
  58.         t = open(tf, 'w')
  59.         t.write(h)
  60.         t.close()
  61.         p = subprocess.Popen(tf, shell=True)
  62.         stdoutdata, stderrdata = p.communicate()
  63.  
  64. def ypad(y=0):
  65.     if y == -1:
  66.         pass
  67.     elif y in ['text']:
  68.         root.ypad+=30
  69.     elif y:
  70.         root.ypad=y+2
  71.     else:
  72.         root.ypad+=21
  73.     return root.ypad
  74.  
  75. btn = Button(label, text='Add To Playlist', command=addto)
  76. btn.pack()
  77. btn.place(x=5,y=ypad(1))
  78.  
  79. btn = Button(label, text='Check Target Playlist', command=verify)
  80. btn.pack()
  81. btn.place(x=100,y=ypad(-1))
  82.  
  83. t = Label(label, text='Copy FROM youtube playlist (note: "Watch Later Playlist" is default if blank) -- ', bg="aqua")
  84. t.pack()
  85. t.place(x=5,y=ypad('text'))
  86.  
  87. stA = StringVar()
  88. ent = Entry(label,textvariable=stA)
  89. ent.pack()
  90. ent.place(x=5,y=ypad(),width=760)
  91.  
  92. t = Label(label, text='Copy TO youtube playlist -- ', bg="aqua")
  93. t.pack()
  94. t.place(x=5,y=ypad('text'))
  95.  
  96. stB = StringVar()
  97. ent = Entry(label,textvariable=stB)
  98. ent.pack()
  99. ent.place(x=5,y=ypad(),width=760)
  100.  
  101. oA = oB = ''
  102.  
  103. html = '''
  104. var OAUTH2_CLIENT_ID = '28993181493-c9o6hdll3di0ssvebfd4atf13edqfu9g.apps.googleusercontent.com';
  105. var OAUTH2_SCOPES = [
  106.     'https://www.googleapis.com/auth/youtube'
  107. ];
  108. var init = false;
  109. googleApiClientReady = function() {
  110.     gapi.auth.init(function() {
  111.         window.setTimeout(checkAuth, 1);
  112.     });
  113. }
  114.  
  115. function checkAuth() {
  116.     gapi.auth.authorize({
  117.         client_id: OAUTH2_CLIENT_ID,
  118.         scope: OAUTH2_SCOPES,
  119.         immediate: true
  120.     }, handleAuthResult);
  121. }
  122. // Handle the result of a gapi.auth.authorize() call.
  123. function handleAuthResult(authResult) {
  124.  
  125.     if (authResult && !authResult.error) {
  126.         // Authorization was successful. Hide authorization prompts and show
  127.         // content that should be visible after authorization succeeds.
  128.         $('.pre-auth').hide();
  129.         $('.post-auth').show();
  130.         loadAPIClientInterfaces();
  131.     }
  132. }
  133.  
  134. function loadAPIClientInterfaces() {
  135.     gapi.client.load('youtube', 'v3', function() {
  136.         if (init) {
  137.             init = false;
  138.             addVideoToPlaylist();
  139.         }
  140.     });
  141. }
  142. // Add a video ID specified in the form to the playlist.
  143. function addVideoToPlaylist() {
  144.     addToPlaylist($('#video-id').val());
  145. }
  146. // Add a video to a playlist.
  147. function addToPlaylist(id) {
  148.     var details = {
  149.         videoId: id,
  150.         kind: 'youtube#video'
  151.     }
  152.     var request = gapi.client.youtube.playlistItems.insert({
  153.         part: 'snippet',
  154.         resource: {
  155.             snippet: {
  156.                 playlistId: "***AAA***",
  157.                 resourceId: details
  158.             }
  159.         }
  160.     });
  161.     request.execute(function(response) {
  162.         console.log(response);
  163.         if (!response.code) {
  164.             $('#status').html('<pre>Succesfully added the video : ' + JSON.stringify(response.result) + '</pre>');
  165.         } else if (response.code == 409) {
  166.             $('#status').html('<p>Conflict : this video is already on your Watch Later playlist</p>');
  167.         } else if (response.code == 404) {
  168.             $('#status').html('<p>Not Found : this video hasnt been found</p>');
  169.         } else {
  170.             $('#status').html('<p>Error : code ' + response.code + '</p>');
  171.         }
  172.     });
  173. }
  174. '''[1:-1]
  175.  
  176. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement