/
kcviewer.py
executable file
·171 lines (133 loc) · 5.71 KB
/
kcviewer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import (Qt, QUrl, pyqtSlot, QSettings, QIODevice, QFile,
QMetaObject, QStandardPaths, QVariant, QDateTime)
from PyQt5.QtWidgets import (QAction, QApplication, QWidget, QMainWindow)
from PyQt5.QtNetwork import (QNetworkProxy, QNetworkProxyFactory, QNetworkAccessManager,
QSslConfiguration, QSslCertificate, QSsl,
QNetworkDiskCache, QNetworkCookieJar, QNetworkCookie, QNetworkRequest)
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5 import QtWebKit, QtNetwork
from PyQt5.QtGui import QDesktopServices
import os
import urllib.parse
import re
from kcsapi import KcsCommand
import command
from ui.mainwindow import MainWindow
class NetworkAccessManager(QNetworkAccessManager):
def __init__(self, parent, apithread):
super(NetworkAccessManager, self).__init__(parent)
self.apithread = apithread
proxy_url = os.environ.get('http_proxy')
if proxy_url:
url = urllib.parse.urlparse(proxy_url)
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.HttpProxy)
proxy.setHostName(url.hostname)
proxy.setPort(url.port)
self.setProxy(proxy)
self.reply_content = {}
def createRequest(self, op, req, outgoing_data = None):
try:
path = req.url().path()
content_type = req.header(QNetworkRequest.ContentTypeHeader)
if content_type == 'application/x-www-form-urlencoded':
print(('req path: ', path))
content = ''
if outgoing_data is not None:
content = str(outgoing_data.peek(1024*1024), encoding='utf-8')
self.apithread.input_queue.put(KcsCommand.create_req_command(path, content))
except Exception as e:
print(e)
reply = super(NetworkAccessManager, self).createRequest(op, req, outgoing_data)
self.reply_content[reply] = ''
reply.readyRead.connect(self.on_ready_read)
reply.finished.connect(self.on_finished)
return reply
def _get_content(self, reply):
try:
path = reply.request().url().path()
content_type = reply.header(QNetworkRequest.ContentTypeHeader)
if re.search('application/json', content_type) or \
re.search('text/plain', content_type):
content = str(reply.peek(reply.bytesAvailable()), encoding='utf-8')
self.reply_content[reply] += content
except Exception as e:
print(e)
@pyqtSlot()
def on_ready_read(self):
reply = self.sender()
self._get_content(reply)
@pyqtSlot()
def on_finished(self):
reply = self.sender()
self._get_content(reply)
try:
path = reply.request().url().path()
content_type = reply.header(QNetworkRequest.ContentTypeHeader)
if re.search('application/json', content_type):
print(('res path: ', path))
content = self.reply_content[reply]
self.apithread.input_queue.put(KcsCommand.create_res_command(path, content))
elif re.search('text/plain', content_type):
print(('res path: ', path))
# FIXME split('svdata=')
for content in self.reply_content[reply].split('svdata=')[1:]:
self.apithread.input_queue.put(KcsCommand.create_res_command(path, content))
except Exception as e:
print(e)
del self.reply_content[reply]
class CookieJar(QNetworkCookieJar):
def __init__(self):
super(CookieJar, self).__init__()
cookie_location = os.path.join(QStandardPaths.writableLocation(QStandardPaths.DataLocation) ,'cookies.ini')
self.cookie_store = QSettings(cookie_location, QSettings.IniFormat)
self.load()
def load(self):
data = self.cookie_store.value('cookies', [])
if data:
self.setAllCookies([QNetworkCookie.parseCookies(c)[0] for c in data])
def save(self):
self.remove_expired_cookies()
lines = []
for cookie in self.allCookies():
if not cookie.isSessionCookie():
lines.append(cookie.toRawForm())
self.cookie_store.setValue('cookies', QVariant(lines))
def remove_expired_cookies(self):
now = QDateTime.currentDateTime()
cookies = [c for c in self.allCookies()
if c.isSessionCookie() or c.expirationDate() >= now]
self.setAllCookies(cookies)
class KCView(MainWindow):
def __init__(self, url, apithread):
super(KCView, self).__init__()
am = NetworkAccessManager(self, apithread)
self.web_view.page().setNetworkAccessManager(am)
disk_cache = QNetworkDiskCache()
cache_location = QStandardPaths.writableLocation(QStandardPaths.CacheLocation)
disk_cache.setCacheDirectory(cache_location)
am.setCache(disk_cache)
self.cookiejar = CookieJar()
am.setCookieJar(self.cookiejar)
web_setting = QtWebKit.QWebSettings.globalSettings()
web_setting.setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
web_setting.setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
self.web_view.load(url)
self.web_view.show()
def closeEvent(self, ev):
self.cookiejar.save()
super(KCView,self).closeEvent(ev)
def main():
import sys
if len(sys.argv) > 1:
url = QUrl(sys.argv[1])
else:
url = QUrl('https://google.com/')
app = QApplication(sys.argv)
browser = KCView(url)
browser.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()