Skip to content

Instantly share code, notes, and snippets.

@tennox
Last active August 16, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tennox/55169f40ab75590eefd8a3f3ac82506f to your computer and use it in GitHub Desktop.
Save tennox/55169f40ab75590eefd8a3f3ac82506f to your computer and use it in GitHub Desktop.
Phpwcms Backend File Downloader
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# coding: utf-8
# This scripts downloads files from the phpwcms backend
#
# ### Required data:
# In[19]:
# ENTER DATA HERE:
backend_links_file = 'wcms-files.json' # contains download links+paths of wcms backend, generated by a JS script
target_dir = 'wcms-files/' # target directory for downloads
wcms_baseurl = 'http://www.your-site.org/'
cookies = {'PHPSESSID': 'XYZ'} # for Phpwcms backend
# # Initialize
# In[5]:
import json
import codecs
# In[3]:
# small helper function to peek into variables
def glance(collection, n=3):
print("%d entries" % len(collection))
if type(collection) == dict:
return list(collection.items())[:n]
elif type(collection) == list:
return collection[:n]
# # Load Data
# In[10]:
# generated by JS
with codecs.open(backend_links_file, encoding='utf-8') as f:
wcms_links = json.load(f)
glance(wcms_links) # quick glance
# # Download
# In[15]:
import os
import requests
def dl(link, path, filename):
r = requests.get(wcms_baseurl + link, cookies=cookies, stream=True)
r.raise_for_status()
os.makedirs(target_dir + path, exist_ok=True) # create directory if not existent
with open(target_dir + path + filename, 'wb') as fd:
for chunk in r.iter_content(1024):
fd.write(chunk)
# In[18]:
done_count = 0
total_count = len(wcms_links)
for link, loc in wcms_links.items():
print('Downloading [%d / %d]: %s -> %s' % (done_count+1, total_count, link, loc[1] + loc[0]))
dl(link, loc[1], loc[0])
done_count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment