forked from SVilgelm/tkSIInformer
/
main.py
executable file
·118 lines (105 loc) · 4.22 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from core import models, views
import settings
import core
import os
def main():
views.init().mainloop()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--database', dest='db', default=settings.DB)
parser.add_argument('-u', '--use-proxy', action='store_true',
dest='use_proxy', default=False)
parser.add_argument('-p', '--proxy', dest='proxy', default=None,
help="./main.py -p socks://localhost:8080")
parser.add_argument('-c', '--check', action='store_true', dest='check',
default=False)
parser.add_argument('-a', '--add-author', action='append',
dest='add_authors', default=[])
parser.add_argument('-f', '--url-fix', action='store_true', dest='url_fix',
default=False, help="'zhurnal.lib.ru' replace to 'samlib.ru'")
parser.add_argument('-r', '--remove-author', action='append',
dest='remove_authors', default=[])
parser.add_argument('-e', '--exclude-book', action='append',
dest='exclude_books', default=[])
parser.add_argument('-s', '--show', dest='show',
choices=['all', 'new', 'updates', 'authors'])
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
default=False)
parser.add_argument('-x', '--import-xml', dest='import_xml')
parser.add_argument('-z', '--zen-of-python', action='store_true',
dest='zen', default=False)
args = parser.parse_args()
if args.zen:
import this
exit()
settings.DB = args.db
settings.USE_PROXY = args.use_proxy or args.proxy is not None
if settings.USE_PROXY and args.proxy:
settings.PROXY = args.proxy
is_console = False
try:
init = not os.path.exists(settings.DB)
models.init_connection(init=init)
if init:
core.create_author("http://samlib.ru/p/pupkin_wasja_ibragimowich/"
"indexdate.shtml")
if args.import_xml:
core.import_from_xml(args.import_xml)
if args.url_fix:
#core.authors_urls_to_samlib()
core.authors_urls_to_zhurnal_lib()
if args.check:
is_console = True
for author in core.check_all_authors():
pass
for url in args.remove_authors:
is_console = True
core.delete_author(url)
for url in args.add_authors:
is_console = True
core.create_author(url)
for url in args.exclude_books:
is_console = True
core.exclude_book(url)
if args.show:
is_console = True
only_authors = args.show == 'authors'
only_new = args.show in ['new','updates']
mark_as_read = args.show in ['all', 'new']
descriptions = args.verbose
for author in sorted(models.Author.get(),
key=lambda author: author.name
):
if only_authors:
print('{name:>s}: {url:>}'.format(name=author.name,
url=author.url))
else:
books = models.Book.get_by_author(
author=author,
only_new=only_new
)
if books:
print('{name:>s}: {url:>}'.format(name=author.name,
url=author.url))
for book in books:
if book.is_new:
template = '\t>>> {name:>s}: {url:>}'
if mark_as_read:
core.book_read(book)
else:
template = '\t{name:>s}: {url:>}'
if descriptions:
template += '\n\t\t{desc:>}'
print(template.format(
name=book.name,
url=book.url,
desc=book.desc)
)
if not is_console:
main()
except KeyboardInterrupt:
if is_console:
print('Exit')