Skip to content

Instantly share code, notes, and snippets.

@vchahun
Last active December 25, 2015 05:39
Show Gist options
  • Save vchahun/6926539 to your computer and use it in GitHub Desktop.
Save vchahun/6926539 to your computer and use it in GitHub Desktop.
Do some dark magic.
import os
import argparse
import requests
import progressbar
def main():
parser = argparse.ArgumentParser(description='All your MUBI are belong to us.')
parser.add_argument('film_id', type=int)
parser.add_argument('resolution', choices=('360p', '480p', '640w', '720w', '1280w'))
parser.add_argument('output_file')
parser.add_argument('--start', type=int, default=0)
parser.add_argument('--chunk_size', type=int, default=1024)
args = parser.parse_args()
with open(os.path.join(os.path.dirname(__file__), 'cookie.txt')) as f:
cookie = f.read().strip()
referer = 'http://us.mubi.com/assets/AuteursPlayer.swf?20120213000000'
headers = {'Referer': referer, 'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:27.0) Gecko/20100101 Firefox/27.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate'}
url_template = 'http://us.mubi.com/films/{}/secure_url?starttime={}&version={}'
url = url_template.format(args.film_id, args.start, args.resolution)
r1 = requests.get(url, headers=headers)
print('Getting film URL from {}'.format(url))
url = r1.text
assert (url and url.startswith('http')), url
r2 = requests.get(url, headers={'Referer': referer}, stream=True)
length = int(r2.headers['content-length'])
print('File size: {:.1f} MB').format(float(length)/(1024 * 1024))
pbar = progressbar.ProgressBar(maxval=length,
widgets=[progressbar.ETA(), progressbar.Bar(), progressbar.Percentage()])
pbar.start()
with open(args.output_file, 'w') as f:
while True:
f.write(r2.raw.read(args.chunk_size))
length -= args.chunk_size
if not length > 0:
break
pbar.update(pbar.currval + args.chunk_size)
pbar.finish()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment