-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
103 lines (87 loc) · 2.83 KB
/
main.py
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
"""
Main module
This module contains methods to response user input
"""
import json
import database
import bangumi
import downloader
import configure
import scraper
if configure.ENABLE_AUTO_DOWNLOAD:
import utorrent
def main():
"""
Scan database and retrieve torrents from remote servers
usage:
py aad.py
"""
print('Importing scrapers')
scrapers = scraper.import_scrapers()
print('Caculating available episodes')
avail_episode = database.fetch_available_episodes()
if len(avail_episode) == 0:
print('No episode is available at present')
return
print('These episodes are available:')
for ep in avail_episode:
print('{} of {}'.format(ep['ep'], ep['name']))
print('Download starts:')
for ep in avail_episode:
print('Ep.{} of {} is processing'.format(ep['ep'], ep['name']))
for __scraper in scrapers:
try:
url = __scraper.get_download_url(**ep)
path = downloader.download(url=url,
save_path=configure.TORRENT_SAVE_PATH,
**ep)
if configure.ENABLE_AUTO_DOWNLOAD:
if not utorrent.is_token_initialized():
print('Refreshing token')
utorrent.refresh_token()
print('Importing torrent into utorrent.')
utorrent.add_torrent(path, ep['folder'])
print('Import completed successfully.')
database.set_downloaded_episode(ep['name'], ep['ep'])
break
except FileNotFoundError:
print('Scraper cannot find the file')
# If last scraper is used
if __scraper is scrapers[-1]:
print('File cannot be found in all scraper. Try next time.')
def update(file_path):
"""
Update database with JSON file
usage:
py aad.py update file_path
param:
file_path Path to JSON file
"""
f = open(file_path, 'r', encoding='utf-8')
parsed = json.loads(f.read(), encoding='utf-8')
f.close()
for anime in parsed:
database.update_anime_info(
anime['name'],
bangumi.parsed_json_to_new_dict(anime)
)
def add(file_path):
"""
Add entries to database with JSON file
usage:
py aad.py add file_path
param:
file_path Path to JSON file
"""
animes = bangumi.read_bangumi_from_file(file_path)
database.add_bangumis(animes)
def check_database():
"""
Check constraints in database
usage:
py aad.py check
"""
print('Program is checking data in the database. Any data violating '
'constraints will be removed from database.')
database.remove_finished_anime()
print('Data checking is completed.')