|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""A script to downloads all projects for a particular user.""" |
| 3 | + |
| 4 | +# This is necessary in order to run the script; otherwise the script needs to |
| 5 | +# be run as a python module (which is inconvenient). |
| 6 | +if __name__ == '__main__' and __package__ is None: |
| 7 | + from os import sys, path |
| 8 | + sys.path.append(path.join(path.dirname(path.abspath(__file__)), '..')) |
| 9 | + |
| 10 | + |
| 11 | +from urllib.request import urlopen |
| 12 | +from urllib.request import urlretrieve |
| 13 | +import argparse |
| 14 | +import os |
| 15 | + |
| 16 | +from codebender_testing.config import LIVE_SITE_URL |
| 17 | + |
| 18 | +from lxml import html |
| 19 | + |
| 20 | + |
| 21 | +def download_projects(url, user, path): |
| 22 | + connection = urlopen('/'.join([url, 'user', user])) |
| 23 | + dom = html.fromstring(connection.read().decode('utf8')) |
| 24 | + os.chdir(path) |
| 25 | + for link in dom.xpath('//table[@id="user_projects"]//a'): |
| 26 | + project_name = link.xpath('text()')[0] |
| 27 | + sketch_num = link.xpath('@href')[0].split(':')[-1] |
| 28 | + print("Downloading %s (sketch %s)" % (project_name, sketch_num)) |
| 29 | + urlretrieve('%s/utilities/download/%s' % (url, sketch_num), |
| 30 | + os.path.join(path, '%s.zip' % project_name)) |
| 31 | + |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + parser = argparse.ArgumentParser() |
| 35 | + parser.add_argument("user", help="the user whose projects we want to download") |
| 36 | + parser.add_argument("-u", "--url", help="url of the codebender site to use", |
| 37 | + default=LIVE_SITE_URL) |
| 38 | + parser.add_argument("-d", "--directory", help="output directory of the downloaded projects", |
| 39 | + default=".") |
| 40 | + args = parser.parse_args() |
| 41 | + download_projects(args.url, args.user, args.directory) |
0 commit comments