Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created April 19, 2012 20:13
Show Gist options
  • Save mpickering/2423874 to your computer and use it in GitHub Desktop.
Save mpickering/2423874 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
'''Takes plug.dj user playlists and uploads them to youtube, the playlists are uploaded with the same name as the playlist on plug
Require libraries
Youtube Data API from http://code.google.com/p/gdata-python-client/
USAGE:
1. Navigate to plug.dj/whichever room you want/ in GOOGLE CHROME
2. Open your playlists, click on each of them, this saves them to local storage IN GOOGLE CHROME
3. Change all the <name>, <password>, <username> variables to your own
4. Run this script, you might need to change the location of the local storage file.
5. All files will upload to youtube playlists
Notes,
#playlist length must be less than 200
#
Questions to [email protected]
'''
import gdata.youtube
import gdata.youtube.service
import json
import itertools, time, sqlite3
username='Matt'
conn = sqlite3.connect('C:\Users\<name>\AppData\Local\Google\Chrome\User Data\Default\Local Storage\http_www.plug.dj_0.localstorage')
c=conn.cursor()
c.execute('''SELECT * FROM ItemTable''')
a=c.fetchall()
info={}
for x in [d for d in a if d[0] in ['media','playlist','playlistMedia']]:
st=unicode(x[1], errors='ignore').strip()
for char in ['\x03', '\x00']:
st=''.join(st.split(char))
illegal= [i for i in xrange(len(st)) if st[i] == '"' and (st[i+1]!=',' and st[i+1]!=':' and st[i+1]!='}' and st[i-1]!='{' and st[i-1]!=',' and st[i-1]!=':' and st[i-1]!='\\')]
count=0
for char in illegal:
char=char-count
print st[char-10:char+10], char, st[char]
st=st[:char]+st[char+1:]
print st[char-10:char+10]
count+=1
info[x[0]]=json.loads(st)
print info.keys()
yt_service = gdata.youtube.service.YouTubeService()
# Turn on HTTPS/SSL access.
# Note: SSL is not available at this time for uploads.
yt_service.ssl = True
yt_service.developer_key = 'AI39si5I1LLjktxI7GIIOGqcoPTVTcy31Pu_jkpaX_6pcmtGHGwK-YVSheo3UV4kEJYsCyqb3g_Sye4triCNt2rHb2atg51ZsA'
yt_service.client_id = 'plugdj'
yt_service.email = '[email protected]'
yt_service.password = '<password>'
yt_service.source = 'plugdjapp'
yt_service.ProgrammaticLogin()
playlist_feed = yt_service.GetYouTubePlaylistFeed(username='default')
playlistNames=[x.title.text for x in playlist_feed.entry]
playlists={}
for i in info['playlist'].values():
playlists[i['id']]={'name':i['name'],'items':[info['media'][pid['id']] for pid in info['playlistMedia'][i['id']].values()]}
for play in playlists.values():
print play['name']
print len(play['items'])
for name in [x['name'] for x in playlists.values()]:
if name not in playlistNames:
yt_service.AddPlaylist(name, 'Playlist imported from plug.dj: %s' % name)
playlist_feed = yt_service.GetYouTubePlaylistFeed(username='default')
links=dict([(x.title.text,x.feed_link[0].href) for x in playlist_feed.entry])
currentItems=[]
for x,y in links.items():
temp=yt_service.GetYouTubePlaylistVideoFeed(uri=y)
currentItems.append(temp.entry)
while int(temp.start_index.text)+25 < int(temp.total_results.text):
time.sleep(1)
temp=yt_service.Query(temp.GetNextLink().href)
currentItems.append(temp.entry)
ids=[]
for x in itertools.chain(*currentItems):
lid=[t.href for t in x.link if t.href.endswith('youtube_gdata')][0]
ids.append(lid[len('https://www.youtube.com/watch?v='):-len('&feature=youtube_gdata')])
print len(ids),sum([len([i for i in x['items'] if i['format']==1]) for x in playlists.values()])
for item in playlists.values():
for track in item['items']:
if track['format'] == 1 and track['cid'] not in ids:
try:
entry = yt_service.GetYouTubeVideoEntry(video_id=track['cid'])
print track['title']+ ' added'
response=yt_service.AddPlaylistVideoEntryToPlaylist(links[item['name']], track['cid'])
time.sleep(1)
except:
print track['title'] + ' is not availible')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment