-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarcqtviewer.py
199 lines (161 loc) · 6.41 KB
/
warcqtviewer.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
190
191
192
193
194
195
196
197
198
199
# Copyright (c) 2013 David Bern
import sys
import os
from PySide import QtCore, QtGui, QtUiTools, QtWebKit, QtNetwork
from PySide import QtXml # Necessary for py2exe @UnusedImport
import pkg_resources # Necessary for py2exe. Used by warctools @UnusedImport
from warcreplay.warcmanager import MetaRecordInfo, WarcReplayHandler, dump
from warcreplay.warcreplay import ReplayServerFactory
class WarcRecordItem(QtGui.QStandardItem, MetaRecordInfo):
def __init__(self, *args, **kwargs):
"""
Creates a generic representation of a WARC Record
"""
MetaRecordInfo.__init__(self, *args, **kwargs)
QtGui.QStandardItem.__init__(self)
def toString(self):
return self.rtype + (': ' + self.uri if self.uri else '')
def data(self, i):
if i == QtCore.Qt.DisplayRole:
return self.toString()
else:
return QtGui.QStandardItem.data(self, i)
class TwistedApp(QtCore.QObject):
def __init__(self):
QtCore.QObject.__init__(self, None)
port = 1080
# dynamically loads a Qt ui file without compiling it to python:
loader = QtUiTools.QUiLoader()
f = QtCore.QFile(os.path.join(self.getBaseDir(), "mainwindow.ui"))
f.open(QtCore.QFile.ReadOnly)
self.ui = loader.load(f)
assert isinstance(self.ui, QtGui.QMainWindow)
f.close()
self.webViewShowing = False
self.webView = self.createWebView(port)
self.ui.actionOpen.triggered.connect(self.openAction)
self.ui.actionExtract.triggered.connect(self.actionExtract)
self.ui.actionClear.triggered.connect(self.actionClear)
self.model = QtGui.QStandardItemModel()
self.ui.listView.setModel(self.model)
self.ui.listView.clicked.connect(self.showItem)
self.ui.listView.doubleClicked.connect(self.previewItem)
self.ui.listView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.show = self.ui.show
self.wrp = WarcReplayHandler()
self.wrp.metarecordinfo = WarcRecordItem
self.rsf = ReplayServerFactory(wrp=self.wrp)
reactor.listenTCP(port, self.rsf)
@staticmethod
def getBaseDir():
"""
Basedir changes if the application is running from a PyInstaller onefile
"""
if getattr(sys, 'frozen', False) and getattr(sys, '_MEIPASS', False):
return sys._MEIPASS # @UndefinedVariable
else:
try:
return os.path.dirname(__file__)
except:
return ""
def createWebView(self, port=1080):
"""
This webView ignores all SSL errors
:rtype : QtWebKit.QWebView
"""
w = QtWebKit.QWebView()
nam = w.page().networkAccessManager()
assert isinstance(nam, QtNetwork.QNetworkAccessManager)
proxy = QtNetwork.QNetworkProxy()
proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)
proxy.setHostName("127.0.0.1")
proxy.setPort(port)
nam.setProxy(proxy)
nam.sslErrors.connect(self.sslErrorHandler)
return w
@staticmethod
def sslErrorHandler(reply, _):
"""
:type reply: QtNetwork.QNetworkReply
"""
reply.ignoreSslErrors()
def showItem(self, index):
"""
Called when an item is single-clicked
:type index: QtCore.QModelIndex
"""
i = index.model().itemFromIndex(index)
""":type : WarcRecordItem"""
r = self.wrp.readRecord(i.filename, i.offset)
self.ui.plainTextEdit.setPlainText(dump(r))
def previewItem(self, index):
"""
Called when an item is double-clicked
:type index: QtCore.QModelIndex
"""
i = index.model().itemFromIndex(index)
""":type : WarcRecordItem"""
self.showWebView()
self.gotoUrl(i.uri)
def openAction(self):
""" Called when the Open button is pressed in the menu """
filt = "WARC files (*.warc *.warc.gz)\nAny files (*)"
f, _ = QtGui.QFileDialog.getOpenFileName(None, "Open WARC", "", filt)
if not f:
return
self.model.clear()
self.wrp.loadWarcFile(f)
map(self.model.appendRow, self.wrp.metaRecords)
def actionClear(self):
self.model.clear()
self.wrp.clear()
@staticmethod
def urlToFilename(url):
"""
Creates a filename of sorts from a given url
:type url: str
:rtype: str
"""
n = url.rsplit('/', 1)[1]
if '.' not in n:
n += '.html'
return n
def actionExtract(self):
""" Called when the Extract button is pressed in the menu """
item = self.ui.listView.selectionModel().currentIndex()
if not item or not item.model():
print "No item selected for extraction"
return
i = item.model().itemFromIndex(item)
""":type : WarcRecordItem"""
if not i or i.rtype != 'response':
print "Please select a response record to extract"
return
title = "Save url response from " + i.uri
f, _ = QtGui.QFileDialog.getSaveFileName(None, title,
self.urlToFilename(i.uri),
"")
if not f:
return
b = self.wrp.extractPayload(self.wrp.readRecord(i.filename, i.offset))
f = open(f, 'wb')
f.write(b)
f.close()
def showWebView(self):
if not self.webViewShowing:
self.ui.splitter_right.addWidget(self.webView)
def gotoUrl(self, url):
self.webView.load(QtCore.QUrl(url))
if __name__ == "__main__":
# Thanks to
# https://groups.google.com/forum/#!msg/pyinstaller/fbl5XOOSAtk/zstUlkcHIN4J
# This is required for the PyInstaller exe to work.
del sys.modules['twisted.internet.reactor']
app = QtGui.QApplication(sys.argv)
import qt4reactor
qt4reactor.install()
from twisted.internet import reactor
myapp = TwistedApp()
myapp.show()
sys.exit(app.exec_())
#reactor.run()