Skip to content

Instantly share code, notes, and snippets.

@estevaofon
Created October 27, 2017 19:38
Show Gist options
  • Save estevaofon/07de2c71b035fe116353e47f98c96b0a to your computer and use it in GitHub Desktop.
Save estevaofon/07de2c71b035fe116353e47f98c96b0a to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
import os
import datetime
directory = "db"
if not os.path.exists(directory):
os.makedirs(directory)
dir_location = os.path.abspath(directory)
URL = 'https://www.pythonanywhere.com/login/?next=/'
client = requests.session()
soup = BeautifulSoup(client.get(URL).text)
csrftoken = str(soup.find("input", value=True)["value"])
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome",
"Accept": "text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/*;q=0.8"}
soup = BeautifulSoup(client.get(URL).text)
login_data = {'username': 'user@gmail.com', 'password': 'xxxx', 'csrfmiddlewaretoken': csrftoken}
r = client.post(URL, data=login_data, headers=dict(Referer=URL))
db_link = "https://www.pythonanywhere.com/user/username/files/home/db-backup.sql"
#function to download a big file in chunks
def download_file(url):
local_filename = url.split('/')[-1]
local_filename = datetime.datetime.now().strftime("%d-%m-%Y")+"-"+local_filename
local_filepath = os.path.join(dir_location, local_filename)
r = client.get(url, stream=True)
with open(local_filepath, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_filename
download_file(db_link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment