Last active
December 11, 2015 21:58
-
-
Save abolibibelot/4665859 to your computer and use it in GitHub Desktop.
requests API >= 0.13.6 mandates passing prefetch=False when accessing the raw response as a file like object... (see https://github.com/kennethreitz/requests/issues/825 )
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 requests | |
import re | |
import os | |
import sys | |
def getFile( id ): | |
url = 'http://dl.free.fr/%s' % id | |
# get initial link | |
r = requests.get(url) | |
contents = r.content | |
href = re.findall(r'href="([^"]+)">Télécharger ce fichier', contents )[0] | |
filename = os.path.basename( href ) | |
cookies = r.cookies | |
# start download | |
print "downloading", filename | |
download = requests.get(href, cookies = cookies, prefetch=False).raw | |
CHUNK = 64 * 1024 | |
with open(filename, 'wb') as fp: | |
while True: | |
print '.', | |
sys.stdout.flush(). | |
chunk = download.read(CHUNK) | |
if not chunk: break | |
fp.write(chunk) | |
if __name__ == '__main__': | |
if len(sys.argv)>1: | |
getFile( sys.argv[1] ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment