Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2010 10:34
Show Gist options
  • Save anonymous/655177 to your computer and use it in GitHub Desktop.
Save anonymous/655177 to your computer and use it in GitHub Desktop.
gameone-dl, a stupid downloader for game one shows
#!/usr/bin/env python3
# A simple script to download stuff from Game One, a show on MTV.
# This code is public domain (if not applicable, then MIT licensed)
import re, urllib.request, urllib.parse, subprocess, sys, json, posixpath
# regex to get the variables from the page that the flash player uses
variable_re = re.compile(r'var variables = (.*);')
# crude regex to convert JS-dict syntax into JSON syntax
jsonize_re = re.compile(r' (\w+):')
# regex to get the path to the SWF file
swf_re = re.compile(r'swfobject.embedSWF\("(.*?)"')
def get_variables(source):
"""Rips out the JS code with the variables, converts it to JSON
and parses that"""
result = variable_re.search(source)
js = result.group(1)
jsonized = jsonize_re.sub(r' "\1":', js)
return json.loads(jsonized)
def get_swf_path(source):
"""Returns the URL to the SWF file that is going to play the file"""
return swf_re.search(source).group(1)
def main():
# grab URL to download from
url = sys.argv[1]
# construct base url from what we got
scheme, netloc = urllib.parse.urlparse(url)[:2]
base_url = "{0}://{1}".format(scheme, netloc)
# read the page and extract useful information
with urllib.request.urlopen(url) as page:
content = page.read().decode('utf-8')
variables = get_variables(content)
swf = get_swf_path(content)
# from this, get the path of the JSON config file
json_path = base_url + variables['playlistfile']
# grab that and parse
with urllib.request.urlopen(json_path) as page:
content = page.read().decode('utf-8')
config = json.loads(content)
streamer = variables['streamer']
path = config['playlist'][0]['hqv']
basename = posixpath.basename(path)
# call rtmpdump with the information that we gathered
rtmpdump = subprocess.Popen(('rtmpdump',
'-r', streamer,
'--swfVfy', swf,
'-y', 'mp4:' + path,
'-o', basename))
rtmpdump.communicate()
print("Downloaded file to {0}".format(basename))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment