Youtube plugin and playlists
#1
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:
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)
Presented in the hope that it will be useful.
Reply
#2
Image
Reply
#3
did you paste a youtube api key in?
Reply
#4
Hello, I can't seem to get it to do anything but print out the title and video id's of the given playlist id.
In the log it prints out...

08:22:34 T:4268 NOTICE: AC/DC - Rock or Bust
08:22:34 T:4268 NOTICE: _NdHySPJr8I
08:22:34 T:4268 NOTICE: Hozier - Take Me To Church
08:22:34 T:4268 NOTICE: MYSVMgRr6pw
08:22:34 T:4268 NOTICE: Drake - Worst Behavior
08:22:34 T:4268 NOTICE: U5pzmGX8Ztg

but how do i get it to actually save the playlist.m3u file?
I'm calling the function "PlayListRipper().rip("PLFgquLnL59akhdhcSukD-WMIBEFW53iYT")"
and it prints into the log but i can't seem to figure out how to get it to save the m3u file?
thanks for any help
Reply
#5
If you use helix and YouTube v5.X.X you can do this:

Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT

and you're done. If the playlist is public (which is common) no login is needed.

Additionally you can provide the order, so the addon won't ask.

default order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=default

reverse order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=reverse

shuffle:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=shuffle
Reply
#6
(2015-02-17, 21:47)bromix Wrote: If you use helix and YouTube v5.X.X you can do this:

Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT

and you're done. If the playlist is public (which is common) no login is needed.

Additionally you can provide the order, so the addon won't ask.

default order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=default

reverse order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=reverse

shuffle:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=shuffle
Reply
#7
(2015-02-17, 21:47)bromix Wrote: If you use helix and YouTube v5.X.X you can do this:

Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT

and you're done. If the playlist is public (which is common) no login is needed.

Additionally you can provide the order, so the addon won't ask.

default order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=default

reverse order:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=reverse

shuffle:
Code:
plugin://plugin.video.youtube/play/?playlist_id=PLFgquLnL59akhdhcSukD-WMIBEFW53iYT&order=shuffle
Reply
#8
This gives me a error saying " Bad Request". Anyone have idea about it?
Reply

Logout Mark Read Team Forum Stats Members Help
Youtube plugin and playlists0