def search(url, callback): QgsMessageLog.logMessage("URL:" + url, "Gazetteer") def requestFinished(reply): # Disconnect from the signal networkAccessManager = QgsNetworkAccessManager.instance() networkAccessManager.finished.disconnect(requestFinished) # Handle the reply if reply.error() != QNetworkReply.NoError: QgsMessageLog.logMessage("Network error #{0}: {1}".format(reply.error(), reply.errorString()), "Gazetteer") callback(u'') else: charset = 'UTF-8' try: _, params = cgi.parse_header(reply.header(QNetworkRequest.ContentTypeHeader)) charset = params['charset'] except: pass QgsMessageLog.logMessage("charset: " + charset, "Gazetteer") data = unicode(reply.readAll(), charset) reply.deleteLater() callback(data) networkAccessManager = QgsNetworkAccessManager.instance() networkAccessManager.finished.connect(requestFinished) networkAccessManager.get(QNetworkRequest(QUrl(QUrl.fromPercentEncoding(url))))
def __parseTask(self, task): """ :type task: QUrl :return: dict """ taskMap = {u'action': task.path()} for key, value in task.encodedQueryItems(): taskMap[unicode(key)] = QUrl.fromPercentEncoding(unicode(value)) return taskMap
def __test(): db_location = StoragePath + "database_test" import os if os.path.exists(db_location): os.remove(db_location) createTable() # jiawzhang XXX: The string to be saved to sqlite must be unicode first, otherwise, error happens. userInfo = UserInfo( u"代理梦想家80后", u"http://item.taobao.com/item.htm?id=9248227645", unicode( QUrl.fromPercentEncoding( u"http://www.taobao.com/webww/?ver=1&&touid=cntaobao%E4%BB%A3%E7%90%86%E6%A2%A6%E6%83%B3%E5%AE%B680%E5%90%8E&siteid=cntaobao&status=1&portalId=&gid=9190349629&itemsId=" ) ), 0.80, 1.00, ) saveUser(userInfo) saveUser(userInfo) userInfo.last_status_time = datetime.now() - timedelta(31) saveUser(userInfo) "get all users" users = getObjects(UserInfo, "select * from user_info") userInfo = users[0] for user in users: print user print "update active = 0 and then get usersMonthly:" updateUser(userInfo, active=0) print "userInfo.active: " + str(userInfo.active) print "__getUsersMonthly" users = __getUsersMonthly() for user in users: print user print "getActiveByTaobaoId" userInfo = getActiveUserByTaobaoId(u"代理梦想家80后") print userInfo print "getUnhandledUserInfoList" userInfos = getUnhandledUserInfoList() for userInfo in userInfos: print userInfo
def handle_unsupported_content(self, reply): """ Called basically when the reply from the request is not HTML or something else renderable by qwebview """ self.reply = reply self.content_type = self.reply.header(QNetworkRequest.ContentTypeHeader).toString() self.content_filename = re.match('.*;\s*filename=(.*);', self.reply.rawHeader('Content-Disposition')) self.content_filename = QUrl.fromPercentEncoding((self.content_filename and self.content_filename.group(1)) or '') content_url = self.reply.url() debug("Loading url %s of type %s" % (content_url.toString(), self.content_type)) if not self.content_handlers.get(str(self.content_type)): self.setHtml(UNKNOWN_CONTENT_TYPE % (self.content_type, self.content_filename, content_url.toString())) else: if str(self.url().toString()) in ('', 'about:blank'): self.setHtml(DOWNLOADING_MESSAGE % (self.content_filename, self.content_type, content_url.toString())) else: # print(self.url()) self.load(self.url()) self.connect(self.reply, SIGNAL("finished()"), self.display_downloaded_content)