forked from whusnoopy/renrenBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
168 lines (128 loc) · 4.54 KB
/
gui.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
# coding: utf-8
import json
import logging
import logging.config
import math
import threading
import urllib.parse
import PySimpleGUI as sg
from config import config
from crawl.crawler import Crawler
from export import export_all
from fetch import prepare_db, fetch_user, update_fetch_info
from web import app
OUTPUT_COLUMNS = 80
OUTPUT_ROWS = 40
logging.config.dictConfig(config.LOGGING_CONF)
logger = logging.getLogger(__name__)
buffer = []
class GUILoggingHandler(logging.StreamHandler):
def __init__(self, window):
logging.StreamHandler.__init__(self)
self._window = window
def emit(self, record):
global buffer # pylint: disable=W0603
format_msg = self.format(record)
msg = f"{record.asctime} [{record.levelname}] {format_msg}"
if buffer:
flush_rows = math.ceil(len(msg) / OUTPUT_COLUMNS)
buffer = buffer[flush_rows - OUTPUT_ROWS :]
buffer.append(msg)
else:
buffer = [msg]
try:
self._window["-LOUT-"].update(value="\n".join(buffer))
except NameError:
# GUI window not inited, just pass
pass
def run_fetch(uid, fetch_status, fetch_gossip, fetch_album, fetch_blog):
fetched = fetch_user(
uid,
fetch_status=fetch_status,
fetch_gossip=fetch_gossip,
fetch_album=fetch_album,
fetch_blog=fetch_blog
)
if not fetched:
logger.info("nothing need to fetch, just test login")
if fetched:
update_fetch_info(uid)
def run_server():
app.run()
def run_export(filename=config.BAK_OUTPUT_TAR):
client_app = app.test_client()
export_all(filename, client_app)
logger.info("已全部导出至 {filename}".format(filename=filename))
def main():
cookie = Crawler.load_cookie()
fetched_info = {}
if cookie:
cookie_str = cookie.values()[0]
cookie_json = json.loads(urllib.parse.unquote(cookie_str))
fetched_info = update_fetch_info(cookie_json["userId"])
form_column = [
[sg.Text("人人网账号"), sg.Input("", size=(24, 1), key="-INPUT-EMAIL-")],
[
sg.Text("人人网密码"),
sg.Input("", size=(24, 1), key="-INPUT-PASSWORD-", password_char="*"),
],
[
sg.Checkbox("状态", key="-FETCH-STATUS-"),
sg.Checkbox("日志", key="-FETCH-BLOG-"),
sg.Checkbox("相册", key="-FETCH-ALBUM-"),
sg.Checkbox("留言", key="-FETCH-GOSSIP-"),
],
[sg.Button("开始抓取", key="-FETCH-")],
[sg.Text("", key="-HINT-")],
[],
[sg.Button("开启本地服务", key="-START-"), sg.Button("导出可查看文件", key="-EXPORT-")],
]
log_column = [
[
sg.Text(
fetched_info.get("name", "unknown"),
size=(OUTPUT_COLUMNS, OUTPUT_ROWS),
key="-LOUT-",
)
]
]
layout = [[sg.Column(form_column), sg.VSeparator(), sg.Column(log_column)]]
window = sg.Window("人人网备份小工具", layout, margins=(20, 20))
svr = None
ch = GUILoggingHandler(window)
ch.setLevel(logging.INFO)
logging.getLogger("").addHandler(ch)
while True:
event, values = window.read()
if event == "OK" or event == sg.WIN_CLOSED:
if svr:
logger.info("terminate web server")
break
if event == "-FETCH-":
email = values["-INPUT-EMAIL-"]
password = values["-INPUT-PASSWORD-"]
if not email or not password:
window["-HINT-"].update(value="必须输入用户名和密码才可以抓取")
continue
fetch_status = values["-FETCH-STATUS-"]
fetch_blog = values["-FETCH-BLOG-"]
fetch_album = values["-FETCH-ALBUM-"]
fetch_gossip = values["-FETCH-GOSSIP-"]
prepare_db()
config.crawler = Crawler(email, password)
uid = config.crawler.uid
fetch_thread = threading.Thread(
target=run_fetch,
args=(uid, fetch_status, fetch_gossip, fetch_album, fetch_blog),
daemon=True,
)
fetch_thread.start()
elif event == "-START-":
svr = threading.Thread(target=run_server, args=(), daemon=True)
svr.start()
elif event == "-EXPORT-":
export_thread = threading.Thread(target=run_export, args=(), daemon=True)
export_thread.start()
window.close()
if __name__ == "__main__":
main()