Created
April 25, 2019 15:48
-
-
Save jvgutierrez/6ccefd8a6a9d0afcda673a36a46d371a to your computer and use it in GitHub Desktop.
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 os | |
import re | |
import sys | |
import requests | |
TIMEOUT=10 | |
def fetch_catalog(url): | |
r = requests.get(url, timeout=TIMEOUT) | |
return r.json() | |
def main(catalog_url, path_regex=None): | |
regex = re.compile(path_regex) | |
print('[*] Fetching catalog...') | |
catalog = fetch_catalog(catalog_url) | |
print('[*] Dumping files...') | |
for resource in catalog['resources']: | |
if resource['type'] != 'File': | |
continue | |
if regex.match(resource['title']) is None: | |
continue | |
if 'content' not in resource['parameters']: | |
continue | |
path = resource['title'].lstrip('/') | |
os.makedirs(os.path.dirname(path), exist_ok=True) | |
with open(path, 'wb') as f: | |
f.write(resource['parameters']['content'].encode('utf-8')) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print("Usage: {} catalog_URL path_regex".format(sys.argv[0])) | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment