Skip to content

Commit 45d1a6a

Browse files
authored
add --dist/-d parameter to specify saving directory (#26)
* add --dist/-d parameter to specify saving directory * add --dist/-d parameter in learn.py to specify saving directory, set to the current directory by default.
1 parent bba50a4 commit 45d1a6a

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
__pycache__
2+
venv/
3+
.pass

learn.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import ssl
1515

1616
ssl._create_default_https_context = ssl._create_unverified_context
17+
global dist_path
18+
dist_path = ''
1719

1820
url = 'https://learn.tsinghua.edu.cn'
1921
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
@@ -156,7 +158,8 @@ def build_notify(s):
156158

157159

158160
def sync_notify(c):
159-
pre = os.path.join(c['kcm'], '公告')
161+
global dist_path
162+
pre = os.path.join(dist_path, c['kcm'], '公告')
160163
if not os.path.exists(pre):
161164
os.makedirs(pre)
162165
try:
@@ -187,8 +190,9 @@ def sync_notify(c):
187190

188191

189192
def sync_file(c):
193+
global dist_path
190194
now = os.getcwd()
191-
pre = os.path.join(c['kcm'], '课件')
195+
pre = os.path.join(dist_path, c['kcm'], '课件')
192196
if not os.path.exists(pre):
193197
os.makedirs(pre)
194198

@@ -246,7 +250,8 @@ def sync_file(c):
246250

247251

248252
def sync_info(c):
249-
pre = os.path.join(c['kcm'], '课程信息.txt')
253+
global dist_path
254+
pre = os.path.join(dist_path, c['kcm'], '课程信息.txt')
250255
try:
251256
if c['_type'] == 'student':
252257
html = get_page('/f/wlxt/kc/v_kcxx_jskcxx/student/beforeXskcxx?wlkcid=%s&sfgk=-1' % c['wlkcid'])
@@ -269,13 +274,13 @@ def append_hw_csv(fname, stu):
269274
f[i] = info_str
270275
else:
271276
f.append(info_str)
272-
csv.writer(open(fname, 'w', encoding='utf8')).writerows(f)
273-
277+
csv.writer(open(fname, 'w')).writerows(f)
274278

275279

276280
def sync_hw(c):
281+
global dist_path
277282
now = os.getcwd()
278-
pre = os.path.join(c['kcm'], '作业')
283+
pre = os.path.join(dist_path, c['kcm'], '作业')
279284
if not os.path.exists(pre):
280285
os.makedirs(pre)
281286
data = {'aoData': [{"name": "wlkcid", "value": c['wlkcid']}]}
@@ -340,7 +345,8 @@ def build_discuss(s):
340345

341346

342347
def sync_discuss(c):
343-
pre = os.path.join(c['kcm'], '讨论')
348+
global dist_path
349+
pre = os.path.join(dist_path, c['kcm'], '讨论')
344350
if not os.path.exists(pre):
345351
os.makedirs(pre)
346352
try:
@@ -428,11 +434,14 @@ def get_args():
428434
parser.add_argument("--course", nargs='+', type=str, default=[])
429435
parser.add_argument('-p', "--_pass", type=str, default='.pass')
430436
parser.add_argument('-c', "--cookie", type=str, default='', help='Netscape HTTP Cookie File')
437+
parser.add_argument('-d', '--dist', type=str, default='', help='download path')
431438
args = parser.parse_args()
432439
return args
433440

434441

435442
def main(args):
443+
global dist_path
444+
dist_path = args.dist
436445
if args.clear:
437446
clear(args)
438447
exit()
@@ -453,8 +462,8 @@ def main(args):
453462
for c in courses:
454463
c['_type'] = {'0': 'teacher', '3': 'student'}[c['jslx']]
455464
print('Sync ' + c['xnxq'] + ' ' + c['kcm'])
456-
if not os.path.exists(c['kcm']):
457-
os.makedirs(c['kcm'])
465+
if not os.path.exists(os.path.join(dist_path, c['kcm'])):
466+
os.makedirs(os.path.join(dist_path, c['kcm']))
458467
sync_info(c)
459468
sync_discuss(c)
460469
sync_notify(c)
@@ -464,3 +473,4 @@ def main(args):
464473

465474
if __name__ == '__main__':
466475
main(get_args())
476+

0 commit comments

Comments
 (0)