# Tk_youtube_add2playlist.py from Tkinter import * from urllib2 import urlopen import re import webbrowser import tempfile import subprocess # to execute the js file as only in the background tf = tempfile.mktemp(".js", "youtube_") yt = 'https://www.youtube.com/watch?v=' ya = 'https://www.youtube.com/playlist?list=' '''https://www.youtube.com/playlist?list=PL3dxi8yxvrTBg9g6ZrqsumlLbYuIWjV09''' root = Tk() label = LabelFrame(root, text=' youtube urls to playlist ', height=200, width=800, bg="aqua") label.pack(fill=X) label.pack_propagate(False) def inEntry(z): try: z = str(z.get()) except: pass try: z = z[z.index('=')+1:] try: z = z[:z.index('&')] except: pass except: pass return z.strip() WL = 'Watch Later Playlist' def getA(z): if not z.strip() or z == WL: z = 'WL' stA.set(WL) return z def getB(z): # ZZZ if not z.strip(): z = root.clipboard_get() stB.set(inEntry(z)) return z def verify(): oB = inEntry(stB) if oB: webbrowser.open(ya+oB) else: print "*** entry is invalid ***" # def addto(): oA = getA(inEntry(stA)) oB = getB(inEntry(stB)) root.update() if oB: h = html.replace('***AAA***',oA).replace('***BBB***',oB) t = open(tf, 'w') t.write(h) t.close() p = subprocess.Popen(tf, shell=True) stdoutdata, stderrdata = p.communicate() def ypad(y=0): if y == -1: pass elif y in ['text']: root.ypad+=30 elif y: root.ypad=y+2 else: root.ypad+=21 return root.ypad btn = Button(label, text='Add To Playlist', command=addto) btn.pack() btn.place(x=5,y=ypad(1)) btn = Button(label, text='Check Target Playlist', command=verify) btn.pack() btn.place(x=100,y=ypad(-1)) t = Label(label, text='Copy FROM youtube playlist (note: "Watch Later Playlist" is default if blank) -- ', bg="aqua") t.pack() t.place(x=5,y=ypad('text')) stA = StringVar() ent = Entry(label,textvariable=stA) ent.pack() ent.place(x=5,y=ypad(),width=760) t = Label(label, text='Copy TO youtube playlist -- ', bg="aqua") t.pack() t.place(x=5,y=ypad('text')) stB = StringVar() ent = Entry(label,textvariable=stB) ent.pack() ent.place(x=5,y=ypad(),width=760) oA = oB = '' html = ''' var OAUTH2_CLIENT_ID = '28993181493-c9o6hdll3di0ssvebfd4atf13edqfu9g.apps.googleusercontent.com'; var OAUTH2_SCOPES = [ 'https://www.googleapis.com/auth/youtube' ]; var init = false; googleApiClientReady = function() { gapi.auth.init(function() { window.setTimeout(checkAuth, 1); }); } function checkAuth() { gapi.auth.authorize({ client_id: OAUTH2_CLIENT_ID, scope: OAUTH2_SCOPES, immediate: true }, handleAuthResult); } // Handle the result of a gapi.auth.authorize() call. function handleAuthResult(authResult) { if (authResult && !authResult.error) { // Authorization was successful. Hide authorization prompts and show // content that should be visible after authorization succeeds. $('.pre-auth').hide(); $('.post-auth').show(); loadAPIClientInterfaces(); } } function loadAPIClientInterfaces() { gapi.client.load('youtube', 'v3', function() { if (init) { init = false; addVideoToPlaylist(); } }); } // Add a video ID specified in the form to the playlist. function addVideoToPlaylist() { addToPlaylist($('#video-id').val()); } // Add a video to a playlist. function addToPlaylist(id) { var details = { videoId: id, kind: 'youtube#video' } var request = gapi.client.youtube.playlistItems.insert({ part: 'snippet', resource: { snippet: { playlistId: "***AAA***", resourceId: details } } }); request.execute(function(response) { console.log(response); if (!response.code) { $('#status').html('
Succesfully added the video : ' + JSON.stringify(response.result) + '
'); } else if (response.code == 409) { $('#status').html('

Conflict : this video is already on your Watch Later playlist

'); } else if (response.code == 404) { $('#status').html('

Not Found : this video hasnt been found

'); } else { $('#status').html('

Error : code ' + response.code + '

'); } }); } '''[1:-1] root.mainloop()