forked from leVirve/NTHU-Library
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo.py
More file actions
82 lines (62 loc) · 2.03 KB
/
demo.py
File metadata and controls
82 lines (62 loc) · 2.03 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
import os
import json
from nthu_library import NTHULibrary, timeit
from nthu_library.user import Account, NotLoginException
def get_newest_books(lib, **kwargs):
"""
:param lang: default is `None` to get both languages,
'en' for English; or 'zh' for Chinese
"""
return lib.get_newest_books(**kwargs)
def get_top_circulations(lib, **kwargs):
"""
:param year: 4-digit number
:param type: 'loaned' or 'reserved'
"""
return lib.get_top_circulated_materials(**kwargs)
def get_personal_info(lib):
try:
return {
'personal': lib.get_info(),
'借閱歷史': lib.get_borrow_history(),
'借閱中': lib.get_current_borrow(),
'預約紀錄': lib.get_reserve_history(),
}
except NotLoginException:
print('Exception: Not login yet')
return None
def get_lost(lib):
"""
:param lib:
:return: <list()> 失物招領物品列表
"""
return lib.get_lost()
def get_past_year_questions(lib):
return lib.get_past_year_questions()
def get_available_space(lib):
return lib.get_available_space()
@timeit
def start(instr, lib, dump=False):
results = {
'personal': get_personal_info,
'new': get_newest_books,
'top': get_top_circulations,
'lost': get_lost,
'questions': get_past_year_questions,
'space': get_available_space,
}[instr](lib)
if dump:
with open('%s-library-data.json' % instr, 'w', encoding='utf8') as f:
json.dump(results, f, indent=2, ensure_ascii=False, sort_keys=True)
if __name__ == '__main__':
import getpass
account = os.getenv('NTHU_LIBRARY_ID') or input('ID: ')
password = os.getenv('NTHU_LIBRARY_PWD') or getpass.getpass('PWD: ')
library = NTHULibrary(Account(account, password))
# start testing crawler function
start('personal', library)
start('top', library)
start('new', library)
start('lost', library)
start('questions', library)
start('space', library)