forked from knarfeh/zhihu2ebook
/
ee-book.py
195 lines (171 loc) · 6.97 KB
/
ee-book.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import getopt
import sys # 修改默认编码
import os # 添加系统路径
from src.exception import UnsupportTypeException
from src.main import EEBook
from src.tools.debug import Debug
from src.constants import __version__
from src.utils import log
from src.tools.match import Match
from src.login import Login
from src.constants import url_info
reload(sys)
base_path = unicode(os.getcwd())
# Python早期版本可以直接用sys.setdefaultencoding('utf-8'),新版本需要先reload一下
sys.setdefaultencoding('utf-8')
sys.setrecursionlimit(100000) # 为BS解析知乎上的长答案增加递归深度限制
short_options = 'Vhgu:r:i:l:c:d'
long_options = ['version', 'help', 'url=', 'gui', 'info', 'login=', 'cookies=', 'debug', 'file=']
help_info = 'Usage: ee-book [OPTION]... [URL]... \n\n'
help_info += '''Starup options:
-V | --version Print version and exit
-h | --help Print help and exit
-i | --info Print information of URLs
-l | --login <URL> Login via command line
-c | --cookies <COOKIES_FILE> Load cookies.txt or cookies.sqlite.
-d | --debug Show traceback and other debug info.
\n
'''
help_info += '''Run options:
-g | --gui Graphical user interface. under developing.
-u | --url <URL> URL to download, if not setted, read from ReadList.txt(default)
-r | --file <file-path> Read from the given file. Notice: only one kind could be accepted in one file
'''
def main():
debug = False
def version():
log.info_log('version %s' % __version__)
try:
opts, args = getopt.getopt(sys.argv[1:], short_options, long_options)
except getopt.GetoptError as err:
log.error_log(u"Try ee-book --help for more options")
sys.exit(2)
for option, args in opts:
if option in ('-V', '--version'):
version()
sys.exit()
elif option in ('-d', '--debug'):
print u"Debug mode..."
debug = True
elif option in ('-h', '--help'):
version()
print(help_info)
sys.exit()
elif option in ('-g', '--gui'):
print(u"Under developing...")
sys.exit()
# graphviz = GraphvizOutput(output_file='filter_gui.png')
# with PyCallGraph(output=graphviz, config=config):
# from PyQt4.QtGui import QApplication
# from PyQt4.QtGui import QIcon
# from src.gui.ui import MainWindow
# from src.resources import qrc_resources
# app = QApplication(sys.argv)
# app.setWindowIcon(QIcon(":/icon.png"))
# app.setApplicationName('EE-Book')
# window = MainWindow()
# window.show()
# sys.exit(app.exec_())
elif option in ('-l', '--login'):
url = args
try:
recipe_kind = Match.get_url_kind(url)
except UnsupportTypeException, e:
print e
print u"Please try again."
sys.exit()
zhihu = EEBook(recipe_kind=recipe_kind) # Init path, e.g. config, only zhihu are supported now
login = Login(recipe_kind=recipe_kind)
login.start()
sys.exit()
elif option in ('-u', '--url'):
url = args
try:
recipe_kind = Match.get_website_kind(url)
except UnsupportTypeException, e:
print e
print u"Please check url."
sys.exit()
game = EEBook(recipe_kind=recipe_kind, url=url, debug=debug)
game.begin()
sys.exit()
elif option in ('-i', '--info'):
url = args
url_kind = Match.get_url_kind(url)
if url_kind == 'Unknown type':
print('Unsupported website or url type. \nPlease check url.')
else:
info = url_info[url_kind]
print(info)
sys.exit()
elif option in('-r', '--file'):
file_name = args
log.print_log(u'read from %s' % file_name)
counter = 1
try:
with open(file_name, 'r') as read_list:
read_list = read_list.readlines()
line = read_list[0]
recipe_kind = Match.get_website_kind(line)
counter += 1
if recipe_kind == 'Unsupport type':
print('Unsupported website or url type. \nPlease check url.')
sys.exit()
except IOError as e:
Debug.logger.debug(u"\nCreating " + file_name + "...")
with open(file_name, 'w') as read_list:
read_list.close()
if 1 == counter:
print(u"\nOops! No content in " + file_name + u". Please check it out.")
sys.exit()
print(u"website type:" + str(recipe_kind))
game = EEBook(recipe_kind=recipe_kind, url=None, read_list=file_name)
game.begin()
sys.exit()
elif option in('-c', '--cookies'):
cookie_file = args
print("Cookie_file:" + cookie_file)
print("TODO: read from cookie to login")
# with open(cookie_file) as f:
# content = f.read()
# game = EEBook(recipe_kind='zhihu')
# from src.tools.db import DB
# from src.tools.extra_tools import ExtraTools
# DB.execute('delete from LoginRecord') # 登陆成功后清除数据库中原有的登录记录,避免下次登陆时取到旧记录
# data = dict()
# data['account'] = 'zhihu2ebook@hotmail.com'
# data['password'] = 'Zhihu2Ebook'
# data['recordDate'] = ExtraTools.get_today()
# data['cookieStr'] = content
# DB.save(data, 'LoginRecord')
# DB.commit()
sys.exit()
file_name = 'ReadList.txt'
log.print_log(u'read from %s' % file_name)
counter = 1
try:
with open(file_name, 'r') as read_list:
read_list = read_list.readlines()
line = read_list[0]
recipe_kind = Match.get_website_kind(line)
counter += 1
if recipe_kind == 'Unsupport type':
print('Unsupported website or url type. \nPlease check url.')
sys.exit()
except IOError as e:
print(u"\nOops! No " + file_name + ". creating " + file_name + "...")
with open(file_name, 'w') as read_list:
read_list.close()
sys.exit()
except IndexError:
if 1 == counter:
print(u"\nOops! No content in " + file_name + u". Please check it out.")
sys.exit()
print(u"website type:" + str(recipe_kind))
game = EEBook(recipe_kind=recipe_kind, url=None, read_list=file_name)
game.begin()
sys.exit()
if __name__ == '__main__':
main()