-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·143 lines (109 loc) · 4.09 KB
/
main.py
File metadata and controls
executable file
·143 lines (109 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python3
from api import Learn
from getpass import getpass
import os
import json
import time
import urllib.request
import argparse
TOKEN_ROTTEN_TIME = 3000
TOKEN_FILENAME = "./token.json"
def pathify(course, section, file, root="."):
return os.path.join(root, course.strip(), section.strip(), file.strip())
def get_saved_token():
try:
if os.path.getmtime(TOKEN_FILENAME) + TOKEN_ROTTEN_TIME > time.time():
with open(TOKEN_FILENAME, 'r') as token_file:
return json.load(token_file)['token']
else:
return None
except FileNotFoundError:
return None
def save_token(token):
# Modify JSON file. Taken from https://stackoverflow.com/a/21035861
with open(TOKEN_FILENAME, 'w') as token_file:
data = {
"token": token
}
json.dump(data, token_file, indent=4)
def is_new(filepath, filesize):
if not os.path.exists(filepath):
return True
if os.path.getsize(filepath) != filesize:
print("{} failed size check. local:{} vs remote:{}".format(filepath, os.path.getsize(filepath), filesize))
return True
return False
def download(src, dst):
directory = os.path.dirname(dst)
os.makedirs(directory, exist_ok=True)
urllib.request.urlretrieve(src, dst)
def main(args):
username = args['user']
learn = None
if args['use_token']:
token = get_saved_token()
if token:
learn = Learn(token=token)
if not learn:
if not username:
username = input("Username: ")
password = getpass("Password: ")
learn = Learn(username, password)
save_token(learn.token)
download_count = 0
update_count = 0
for file in learn.all_files():
course = file['course_name']
section = file['section_name']
filename = file['filename']
filesize = file['filesize']
file_ext = os.path.splitext(filename)[1][1:] # cut off preceding '.'
if "ignore" in args and file_ext in args['ignore']:
continue # Skip this file
if "only" in args and file_ext not in args["only"]:
continue # Skip
filepath = pathify(course, section, filename, root=args['directory'])
if filesize > 0 and is_new(filepath, filesize):
download(file['url'], filepath)
download_count += 1
print("Downloaded {0}".format(filename))
else:
update_count += 1
if download_count > 0:
print("{} files downloaded".format(download_count))
elif update_count > 0:
print("{} files verified".format(update_count))
else:
print("Nothing to do")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download files off Learn")
parser.add_argument('directory',
default='learn',
nargs="?",
help='The target directory to place files')
parser.add_argument('--user', '-u',
metavar="abc12",
required=False,
type=str,
help='Learn username')
parser.add_argument('--refresh-token',
action='store_false',
dest="refresh_token",
help="Don't use cached token. Login with username and password and refresh it.")
parser.add_argument('--ignore', '-i',
metavar="ext,ext",
help="Download all files except ones ending with these file extentions. Comma Sepearated.")
parser.add_argument('--only',
metavar="ext,ext",
help="Only download files with this file extention. Comma seperated.")
parsed_args = parser.parse_args()
args = {
"user": parsed_args.user,
"directory": parsed_args.directory,
"use_token": parsed_args.refresh_token,
}
if parsed_args.ignore:
args['ignore'] = parsed_args.ignore.split(',')
if parsed_args.only:
args['only'] = parsed_args.only.split(',')
main(args)