Last active
August 16, 2016 14:25
-
-
Save tennox/55169f40ab75590eefd8a3f3ac82506f to your computer and use it in GitHub Desktop.
Phpwcms Backend File Downloader
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
/** | |
* Execute this on the phpwcms files page when all wanted folders are opened | |
* (all visible download buttons will be added to the array) | |
**/ | |
var save_buttons = $('img[src="img/button/download_disc.gif"]'); | |
function isFolder($tr) { | |
return $tr.attr('bgcolor') == '#EBF2F4'; | |
} | |
function getIndent($tr) { | |
if(isFolder($tr)) { | |
var spacer = $tr.find('td.msglist:eq(0) > img:eq(1)'); | |
var ind = spacer.attr('width'); | |
} else { | |
var spacer = $tr.find('td.msglist'); | |
var ind = spacer.attr('width') - 15; | |
} | |
return ind ? ind : 999; | |
} | |
function getName($tr) { | |
return $tr.find('strong').text(); | |
} | |
function getLink($e) { | |
return $e.closest('a').attr('href'); | |
} | |
function getFileName($e) { | |
return $e.closest('tr').find('td:eq(2) > a').text(); | |
} | |
function getParentDir($e) { | |
// get subtable | |
/*while (!(closest = $e.closest('table')).is('#files')) { | |
$e = closest; | |
}*/ | |
if (!$e.is('tr')) { | |
var $row = $e.closest('table').closest('tr'); | |
} else { | |
var $row = $e; | |
} | |
var ind = getIndent($row); | |
while($row.length && getIndent($row) >= ind) // skip boring tr's | |
$row = $row.prev(); | |
return $row; | |
} | |
function getPath($e) { | |
var path = ""; | |
while (($parent = getParentDir($e)).length) { | |
path = getName($parent) + "/" + path; | |
$e = $parent; | |
} | |
return path; | |
} | |
var data = {}; | |
for(var save of saves) { | |
var $save = $(save); | |
data[getLink($save)] = [getFileName($save), getPath($save)]; | |
} | |
// print result to console | |
console.log(JSON.stringify(data)); |
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
# 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