Last active
December 15, 2015 16:18
-
-
Save gaganpreet/5287761 to your computer and use it in GitHub Desktop.
A quick and dirty script to download videos from pyvideo.org
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
import json | |
import sys | |
import requests | |
import urllib | |
base_url = 'http://pyvideo.org' | |
class Video(): | |
title = None | |
summary = None | |
speaker = None | |
link = None | |
def __repr__(self): | |
return self.title + ' - ' + self.speaker | |
def api_fetch(path): | |
''' Make an api request ''' | |
response_object = requests.get(base_url + path) | |
return response_object.json() | |
def fetch_videos(slug): | |
''' Main function ''' | |
# check for slug in json object | |
for category in json['objects']: | |
if category['slug'] == slug: | |
video_pages = category['videos'] | |
break | |
else: | |
print 'Slug not found' | |
sys.exit(1) | |
for video_page in video_pages[:3]: | |
video = get_video_info(video_page) | |
download_video(video) | |
def get_video_info(video): | |
json = api_fetch(video) | |
video = Video() | |
video.title = json['title'] | |
video.summary = json['summary'] | |
video.speaker = ', '.join(json['speakers']) | |
video.link = json['video_mp4_url'] | |
return video | |
def download_video(video): | |
video_file = repr(video) + '.mp4' | |
descr_file = repr(video) + '.txt' | |
description = '''%s - %s\n | |
%s\n'''%(video.title, video.speaker, video.summary) | |
with open(descr_file, 'w') as f: | |
f.write(description) | |
print "Downloading ", video_file | |
urllib.urlretrieve(video.link, video_file) | |
if __name__ == '__main__': | |
json = api_fetch('/api/v1/category/?limit=50') | |
if len(sys.argv) < 2: | |
print "Slug not passed, listing all available ones" | |
for category in json['objects']: | |
print category['slug'] | |
sys.exit(0) | |
slug = sys.argv[1] | |
fetch_videos(slug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment