eric6/WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py

changeset 7269
0c63ea7f94bd
parent 7229
53054eb5b15a
child 7360
9190402e4505
equal deleted inserted replaced
7268:a28338eaf694 7269:0c63ea7f94bd
9 9
10 10
11 import json 11 import json
12 import base64 12 import base64
13 13
14 from PyQt5.QtCore import pyqtSignal, QObject, QDateTime, QUrl, QByteArray, \ 14 from PyQt5.QtCore import (
15 QCoreApplication, QEventLoop 15 pyqtSignal, QObject, QDateTime, QUrl, QByteArray, QCoreApplication,
16 QEventLoop
17 )
16 from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply 18 from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
17 19
18 from WebBrowser.WebBrowserWindow import WebBrowserWindow 20 from WebBrowser.WebBrowserWindow import WebBrowserWindow
19 21
20 from .SafeBrowsingThreatList import ThreatList 22 from .SafeBrowsingThreatList import ThreatList
117 "clientVersion": self.ClientVersion, 119 "clientVersion": self.ClientVersion,
118 }, 120 },
119 "listUpdateRequests": [], 121 "listUpdateRequests": [],
120 } 122 }
121 123
122 for (threatType, platformType, threatEntryType), currentState in \ 124 for (threatType, platformType, threatEntryType), currentState in (
123 clientStates.items(): 125 clientStates.items()
126 ):
124 requestBody["listUpdateRequests"].append( 127 requestBody["listUpdateRequests"].append(
125 { 128 {
126 "threatType": threatType, 129 "threatType": threatType,
127 "platformType": platformType, 130 "platformType": platformType,
128 "threatEntryType": threatEntryType, 131 "threatEntryType": threatEntryType,
185 188
186 for prefix in prefixes: 189 for prefix in prefixes:
187 requestBody["threatInfo"]["threatEntries"].append( 190 requestBody["threatInfo"]["threatEntries"].append(
188 {"hash": base64.b64encode(prefix).decode("ascii")}) 191 {"hash": base64.b64encode(prefix).decode("ascii")})
189 192
190 for (threatType, platformType, threatEntryType), currentState in \ 193 for (threatType, platformType, threatEntryType), currentState in (
191 clientState.items(): 194 clientState.items()
195 ):
192 requestBody["clientStates"].append(currentState) 196 requestBody["clientStates"].append(currentState)
193 if threatType not in requestBody["threatInfo"]["threatTypes"]: 197 if threatType not in requestBody["threatInfo"]["threatTypes"]:
194 requestBody["threatInfo"]["threatTypes"].append(threatType) 198 requestBody["threatInfo"]["threatTypes"].append(threatType)
195 if platformType not in \ 199 if (
196 requestBody["threatInfo"]["platformTypes"]: 200 platformType not in
201 requestBody["threatInfo"]["platformTypes"]
202 ):
197 requestBody["threatInfo"]["platformTypes"].append( 203 requestBody["threatInfo"]["platformTypes"].append(
198 platformType) 204 platformType)
199 if threatEntryType not in \ 205 if (
200 requestBody["threatInfo"]["threatEntryTypes"]: 206 threatEntryType not in
207 requestBody["threatInfo"]["threatEntryTypes"]
208 ):
201 requestBody["threatInfo"]["threatEntryTypes"].append( 209 requestBody["threatInfo"]["threatEntryTypes"].append(
202 threatEntryType) 210 threatEntryType)
203 211
204 data = QByteArray(json.dumps(requestBody).encode("utf-8")) 212 data = QByteArray(json.dumps(requestBody).encode("utf-8"))
205 url = QUrl(self.GsbUrlTemplate.format("fullHashes:find", 213 url = QUrl(self.GsbUrlTemplate.format("fullHashes:find",
243 """ 251 """
244 if not self.__fairUse or minimumWaitDuration is None: 252 if not self.__fairUse or minimumWaitDuration is None:
245 self.__nextRequestNoSoonerThan = QDateTime() 253 self.__nextRequestNoSoonerThan = QDateTime()
246 else: 254 else:
247 waitDuration = int(float(minimumWaitDuration.rstrip("s"))) 255 waitDuration = int(float(minimumWaitDuration.rstrip("s")))
248 self.__nextRequestNoSoonerThan = \ 256 self.__nextRequestNoSoonerThan = (
249 QDateTime.currentDateTime().addSecs(waitDuration) 257 QDateTime.currentDateTime().addSecs(waitDuration)
258 )
250 259
251 def fairUseDelayExpired(self): 260 def fairUseDelayExpired(self):
252 """ 261 """
253 Public method to check, if the fair use wait period has expired. 262 Public method to check, if the fair use wait period has expired.
254 263
293 ) 302 )
294 urlStr = url.toString() 303 urlStr = url.toString()
295 304
296 # check the local cache first 305 # check the local cache first
297 if urlStr in self.__lookupApiCache: 306 if urlStr in self.__lookupApiCache:
298 if self.__lookupApiCache[urlStr]["validUntil"] > \ 307 if (
299 QDateTime.currentDateTime(): 308 self.__lookupApiCache[urlStr]["validUntil"] >
309 QDateTime.currentDateTime()
310 ):
300 # cached entry is still valid 311 # cached entry is still valid
301 return self.__lookupApiCache[urlStr]["threatInfo"], error 312 return self.__lookupApiCache[urlStr]["threatInfo"], error
302 else: 313 else:
303 del self.__lookupApiCache[urlStr] 314 del self.__lookupApiCache[urlStr]
304 315

eric ide

mercurial