Last active
December 29, 2015 06:09
-
-
Save strayer/7627277 to your computer and use it in GitHub Desktop.
Lists video page links of a YouTube playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import json | |
import sys | |
import os | |
GDATA_URL="https://gdata.youtube.com/feeds/api/playlists/{playlist_id}/?alt=json&v=2&max-results=50" | |
def get_playlist_links(playlist_id): | |
next_feed_page = GDATA_URL.format(playlist_id=playlist_id) | |
while next_feed_page is not None: | |
r = requests.get(next_feed_page) | |
gdata = json.loads(r.text) | |
for playlist_entry in gdata['feed']['entry']: | |
yield playlist_entry['content']['src'] | |
next_feed_page = next(i['href'] for i in gdata['feed']['link'] if i['rel'] == 'next') | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
sys.stderr.write("Usage: {} <playlist id>{}".format(os.path.basename(__file__), os.linesep)) | |
sys.exit(-1) | |
links = get_playlist_links(sys.argv[1]) | |
sys.stdout.write(os.linesep.join(links)) | |
sys.stdout.write(os.linesep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment