2014-09-01, 14:08
Hey there people. With the youtube plugin, playlists only work if I have it logged into my account, and even then only my playlists work. So I have written a little python script which converts any youtube playlist to a playlist file for XBMC:
Presented in the hope that it will be useful.
Code:
""" youtube_playlist_ripper rips a youtube playlist for XMBC.
Copyright (C) 2014 Mark Wolstenholme <markfiend at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import urllib2, json
from sys import argv
class PlayListRipper:
"""Given a youtube playlist, convert it to a playlist for XBMC.
Requires the youtube plugin for XBMC.
PROPERTIES:
url: string: Base URL for the youtube playlist API
snippets: dict: Unicode string snippets to build the output
output: string: Unicode output for the output playlist
METHODS:
__init__: Initialisation.
output_add: Append a line to the output
save: Write the output to file
rip: Translate the JSON from the youtube API into format for XBMC
usage: Print out some help.
You will need a free youtube API key from Google. Generate a
public API access key for server applications and paste it into
the code just below.
"""
API_KEY = '***REMOVED***'
def __init__(self):
"Initialise the object."
self.url = 'https://www.googleapis.com/youtube/v3/playlistItems'
self.snippets = {'header': u"#EXTM3U",
'line1': u'#EXTINF:-1, ',
'line2': (u'plugin://plugin.video.youtube/' +
'?action=play_video&videoid=')}
self.output = u''
def output_add(self, string):
"Append a line to the output."
self.output += string + '\n'
def save(self, filename):
"Actually write the output to the file."
with open(filename, 'w') as f:
f.write(self.output.encode("UTF-8"))
def rip(self, playlist_id):
"""Get the JSON object from the playlist and translate it.
The youtube API will not allow you to retrieve the entire
playlist in one go; we have to do it a page at a time. The
JSON will contain a nextPageToken item if there is a next
page.
"""
pl_url = self.url + '?part=snippet&key=' + \
self.API_KEY + '&playlistId=' + playlist_id
nextpage = None
self.output_add(self.snippets['header'])
while True:
# Loop through the playlist's pages.
if nextpage is None:
url = pl_url
else:
url = pl_url + '&pageToken=' + nextpage
data = json.load(urllib2.urlopen(url))
for item in data[u'items']:
print item[u'snippet'][u'title'],
self.output_add(self.snippets['line1'] +
item[u'snippet'][u'title'])
print item[u'snippet'][u'resourceId'][u'videoId']
self.output_add(self.snippets['line2'] +
item[u'snippet'][u'resourceId'][u'videoId'])
if u'nextPageToken' in data:
nextpage = data[u'nextPageToken']
else:
break
def usage(self):
"print usage/help message"
print 'youtube_playlist_ripper.py Copyright (C) 2014 Mark Wolstenholme'
print
print 'Usage:'
print 'python youtube_playlist_ripper.py PLAYLISTID [outputfile]'
print 'Converts the youtube playlist from https://www.youtube.com/playlist?list=PLAYLISTID to XMBC playlist format.'
print 'If outputfile not specified, outputs to playlist.m3u'
print
print 'python youtube_playlist_ripper.py -h displays this message'
if __name__ == "__main__":
if len(argv) < 3:
outputfilename = 'playlist.m3u'
else:
outputfilename = argv[2]
PLR = PlayListRipper()
if '-h' in argv or '--help' in argv or len(argv) < 2:
PLR.usage()
else:
playlist = argv[1]
PLR.rip(playlist)
PLR.save(outputfilename)