Last active
August 29, 2015 14:06
-
-
Save shichao-an/129969edca4fc1282296 to your computer and use it in GitHub Desktop.
Utility script for 115wangpan
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
#!/usr/bin/env python | |
# vim: tabstop=4 shiftwidth=4 expandtab softtabstop=4 smarttab textwidth=78 | |
from __future__ import print_function | |
import os | |
import sys | |
from u115 import API, File, Directory | |
ARGV = sys.argv | |
GLOB_ALL = False | |
USAGE = 'usage: %s [entry_number [file_number]]' | |
# Environment variables | |
NUM_ENTRIES = int(os.environ.get('U115_NUM_ENTIRES', 25)) | |
DL = True if os.environ.get('U115_DL') else False | |
def get_entries(): | |
i = None | |
j = None | |
# Parse arguments | |
if len(ARGV) == 2 or len(ARGV) == 3: | |
try: | |
i = int(ARGV[1]) | |
except ValueError: | |
print('Invalid argument %s.' % ARGV[1]) | |
print_usage() | |
if i <= 0: | |
print('Number must be a postive integer.') | |
sys.exit(1) | |
if len(ARGV) == 3: | |
if ARGV[2] == '*': | |
global GLOB_ALL | |
GLOB_ALL = True | |
else: | |
try: | |
j = int(ARGV[2]) | |
except ValueError: | |
print('Invalid argument %s.' % ARGV[2]) | |
print_usage() | |
if j <= 0: | |
print('Number must be a postive integer.') | |
sys.exit(1) | |
# API login | |
api = API() | |
api.login() | |
entries = api.downloads_directory.list(NUM_ENTRIES) | |
if len(ARGV) == 1: | |
for k, entry in enumerate(entries, start=1): | |
print(k, repr(entry)) | |
elif len(ARGV) == 2: | |
list_entry(entries[i - 1]) | |
elif len(ARGV) == 3: | |
entry = entries[i - 1] | |
print(repr(entry)) | |
files = entry.list() | |
if GLOB_ALL: | |
for f in files: | |
print(repr(f)) | |
download_file(f, not DL) | |
else: | |
f = files[j - 1] | |
print(repr(f)) | |
download_file(f, not DL) | |
else: | |
print_usage() | |
def list_entry(entry): | |
print(repr(entry)) | |
for k, f in enumerate(entry.list(), start=1): | |
print('\t', k, repr(f)) | |
def print_usage(): | |
print(USAGE) | |
sys.exit(1) | |
def download_file(f, dry_run=False): | |
if isinstance(f, File): | |
print(f.url) | |
if not dry_run: | |
f.download() | |
elif isinstance(f, Directory): | |
ffiles = f.list() | |
for ff in ffiles: | |
print(ff) | |
print(ff.url) | |
if not dry_run: | |
ff.download() | |
if __name__ == '__main__': | |
get_entries() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment