I am not a Python user or expert at all, and have been provided this script to scrape some video content to allow me to work offline, so please forgive my lack of knowledge and understanding here...
When I run the code below, it flags up as 'Invalid Syntax' on each of the apostrophes, if I remove one, it flags up the next. If I add one, it still shows as being a problem.
For info, I am running the script from cmdline:
python.exe download.py uberRequest.json
Any ideas please?
When I run the code below, it flags up as 'Invalid Syntax' on each of the apostrophes, if I remove one, it flags up the next. If I add one, it still shows as being a problem.
For info, I am running the script from cmdline:
python.exe download.py uberRequest.json
Any ideas please?
Code:
#!/usr/bin/env python
import json
import sys
import requests
import os
import argparse
from collections import OrderedDict
# Help text and arguments
parser = argparse.ArgumentParser(description='Download videos using this script.')
parser.add_argument('-q', choices=['SD', 'HD'], default='HD', help='Download quality selector; will default to HD (720p).')
parser.add_argument('uberRequest', metavar='uberRequest.json', help='uberRequest.json file, see instructions in git repo.')
args = parser.parse_args()
# Convert input to number
video_quality = 1
if args.q == 'SD':
video_quality = 0
# Read uber response from file
with open(args.uberRequest, 'r') as json_data:
uber = json.load(json_data)
print 'Course ': + uber['course']['name']
if not os.path.exists(uber['course']['name']):
os.makedirs(uber['course']['name'])
is_looping = True
section_counter = 1
# Iterate sections
for section in uber['course']['childNodes']:
print 'Section: ' + section['name']
section_path = uber['course']['name'] + '/' + str(section_counter) + '. ' + section['name']
if not os.path.exists(section_path):
os.makedirs(section_path)
lesson_counter = 1
# Iterate lessons
for lesson in section['childNodes'][0]['learningObjects']:
print 'Downloading: ' + lesson['metadata']['name']
lesson_path = section_path + '/' + str(lesson_counter) + '. ' + lesson['metadata']['name']
if not os.path.exists(lesson_path):
os.makedirs(lesson_path)
# Grab base URL
baseUrl = lesson['metadata']['baseUrl']
# Prepare request to script.json to grab video URLs and set cookies
cookies = OrderedDict()
for cookie in reversed(lesson['metadata']['cookies']):
cookies[cookie['key']] = cookie['value']
# Get video list
response = requests.get(baseUrl+'/script.json', cookies=cookies)
video_counter = 1
if response.status_code == 200:
# Iterate videos
for video in json.loads(response.content)['slides']:
# Download with title
download_response = requests.get(baseUrl+video['video'][video_quality]['URI'], cookies=cookies)
open(lesson_path + '/' + str(video_counter) + '. ' + video['title'].replace("/","_") + '.mp4', 'wb').write(download_response.content)
video_counter = video_counter + 1
else:
print 'Request for video list failed, your cookies have probably expired.'
is_looping = False
break
lesson_counter = lesson_counter + 1
if not is_looping:
# Break out of outer loop in the event of expired cookies
break
section_counter = section_counter + 1