def __createPageContextMenu(self, menu): """ Private method to populate the basic context menu. @param menu reference to the menu to be populated @type QMenu """ ## menu.addSeparator() menu.addAction( self.tr("Open native web"), lambda: QDesktopServices.openUrl(self.url()) ) ## language = QLocale.system().name() if not language: languages = [] else: languages = MyEngineView.expand(QLocale(language).language()) if languages: menu.addSeparator() language = languages[0] langCode = language.split("[")[1][:2] googleTranslatorUrl = QUrl.fromEncoded( b"http://translate.google.com/translate?sl=auto&tl=" + langCode.encode() + b"&u=" + QUrl.toPercentEncoding(bytes(self.url().toEncoded()).decode())) menu.addAction( self.tr("Google Translate"), lambda :self.load(googleTranslatorUrl) )#TODO: maybe has google bug blank.
def parseTemplate(cls, searchTerm, searchTemplate): """ Class method to parse a search template. @param searchTerm term to search for (string) @param searchTemplate template to be parsed (string) @return parsed template (string) """ locale = QLocale(Preferences.getHelp("SearchLanguage")) language = locale.name().split("_")[0] country = language.lower() result = searchTemplate result = result.replace("{count}", "20") result = result.replace("{startIndex}", "0") result = result.replace("{startPage}", "0") result = result.replace("{language}", language) result = result.replace("{country}", country) result = result.replace("{inputEncoding}", "UTF-8") result = result.replace("{outputEncoding}", "UTF-8") result = result.replace( "{searchTerms}", bytes(QUrl.toPercentEncoding(searchTerm)).decode()) result = re.sub(r"""\{([^\}]*:|)source\??\}""", Program, result) return result
def complete_duckduckgo(text): if not text: return [] url = ("https://www.duckduckgo.com/ac/?q={}&type=list".format( str(QUrl.toPercentEncoding(text), "utf-8"))) with urlopen(url) as conn: return json.loads(str(conn.read(), "utf-8"))[1]
def _find(self, entitytype, handler, **kwargs): filters = [] limit = kwargs.pop("limit") if limit: filters.append(("limit", limit)) is_search = kwargs.pop("search", False) if is_search: if config.setting["use_adv_search_syntax"]: query = kwargs["query"] else: query = escape_lucene_query(kwargs["query"]).strip().lower() filters.append(("dismax", 'true')) else: query = [] for name, value in kwargs.items(): value = escape_lucene_query(value).strip().lower() if value: query.append('%s:(%s)' % (name, value)) query = ' '.join(query) if query: filters.append(("query", query)) queryargs = {} for name, value in filters: queryargs[name] = bytes(QUrl.toPercentEncoding(str(value))).decode() path_list = [entitytype] return self.get(path_list, handler, queryargs=queryargs, priority=True, important=True, mblogin=False, refresh=False)
def _find(self, entitytype, handler, **kwargs): filters = [] limit = kwargs.pop("limit") if limit: filters.append(("limit", limit)) is_search = kwargs.pop("search", False) if is_search: if config.setting["use_adv_search_syntax"]: query = kwargs["query"] else: query = escape_lucene_query(kwargs["query"]).strip().lower() filters.append(("dismax", 'true')) else: query = [] for name, value in kwargs.items(): value = escape_lucene_query(value).strip().lower() if value: query.append('%s:(%s)' % (name, value)) query = ' '.join(query) if query: filters.append(("query", query)) queryargs = {} for name, value in filters: value = QUrl.toPercentEncoding(string_(value)) queryargs[string_(name)] = value path_list = [entitytype] return self.get(path_list, handler, queryargs=queryargs, priority=True, important=True, mblogin=False, refresh=False)
def google_complete(text): if not text: return [] url = ("https://www.google.com/complete/search?client=firefox&q=" + str(QUrl.toPercentEncoding(text), "utf-8")) with urlopen(url) as conn: return json.loads(str(conn.read(), "latin1"))[1]
def _find(self, entitytype, handler, kwargs): host = config.setting["server_host"] port = config.setting["server_port"] filters = [] limit = kwargs.pop("limit") if limit: filters.append(("limit", limit)) is_search = kwargs.pop("search", False) if is_search: if config.setting["use_adv_search_syntax"]: query = kwargs["query"] else: query = escape_lucene_query(kwargs["query"]).strip().lower() filters.append(("dismax", 'true')) else: query = [] for name, value in kwargs.items(): value = escape_lucene_query(value).strip().lower() if value: query.append('%s:(%s)' % (name, value)) query = ' '.join(query) if query: filters.append(("query", query)) queryargs = {} for name, value in filters: value = QUrl.toPercentEncoding(unicode(value)) queryargs[str(name)] = value path = "/ws/2/%s" % (entitytype) return self.get(host, port, path, handler, queryargs=queryargs)
def value(self): value = super().value() if value is None: return # split webjumps and protocols between command and argument if re.match(r"^\S+://.*", value): args = value.split("://", 1) else: args = value.split(" ", 1) command = args[0] # Look for webjumps webjump = None if command in WEBJUMPS: webjump = WEBJUMPS[command] else: # Look for a incomplete webjump, accepting a candidate # if there is a single option candidates = [wj for wj in WEBJUMPS if wj.startswith(command)] if len(candidates) == 1: webjump = WEBJUMPS[candidates[0]] if webjump: if not webjump.allow_args: # send the url as is return webjump.url elif len(args) < 2: # send the url without a search string return webjump.url.replace("%s", "") else: # format the url as entered if webjump.protocol: return value else: return webjump.url.replace( "%s", str(QUrl.toPercentEncoding(args[1]), "utf-8")) # Look for a bookmark bookmarks = {name: url for url, name in self.bookmarks} if value in bookmarks: return bookmarks[value] # Look for a incomplete bookmarks, accepting a candidate # if there is a single option candidates = [bm for bm in bookmarks if bm.startswith(command)] if len(candidates) == 1: return bookmarks[candidates[0]] # No webjump, no bookmark, look for a url if "://" not in value: url = QUrl.fromUserInput(value) if url.isValid(): # default scheme is https for us if url.scheme() == "http": url.setScheme("https") return url return value
def _encode_acoustid_args(self, args, format_='json'): filters = [] args['client'] = ACOUSTID_KEY args['clientversion'] = PICARD_VERSION_STR args['format'] = format_ for name, value in args.items(): value = string_(QUrl.toPercentEncoding(value)) filters.append('%s=%s' % (string_(name), value)) return '&'.join(filters)
def _encode_acoustid_args(self, args, format='xml'): filters = [] args['client'] = ACOUSTID_KEY args['clientversion'] = PICARD_VERSION_STR args['format'] = format for name, value in args.items(): value = string_(QUrl.toPercentEncoding(value)) filters.append('%s=%s' % (string_(name), value)) return '&'.join(filters)
def _encode_acoustid_args(self, args, format_='json'): filters = [] args['client'] = ACOUSTID_KEY args['clientversion'] = PICARD_VERSION_STR args['format'] = format_ for name, value in args.items(): value = bytes(QUrl.toPercentEncoding(value)).decode() filters.append('%s=%s' % (name, value)) return '&'.join(filters)
def _encode_acoustid_args(self, args): filters = [] args['client'] = self.client_key args['clientversion'] = self.client_version args['format'] = 'json' for name, value in args.items(): value = bytes(QUrl.toPercentEncoding(value)).decode() filters.append('%s=%s' % (name, value)) return '&'.join(filters)
def queue_images(self): release_group_id = self.metadata["musicbrainz_releasegroupid"] path = "/v3/music/albums/%s" % \ (release_group_id, ) queryargs = { "api_key": QUrl.toPercentEncoding(FANART_APIKEY), "client_key": QUrl.toPercentEncoding(self._client_key), } log.debug("CoverArtProviderFanartTv.queue_downloads: %s" % path) self.album.tagger.webservice.download(FANART_HOST, FANART_PORT, path, partial(self._json_downloaded, release_group_id), priority=True, important=False, queryargs=queryargs) self.album._requests += 1 return CoverArtProvider.WAIT
def get_map_link(self): """Create hyperlink depending on map_id and map_name.""" map_name_sef = self.api.map.map_name\ .replace(" ", "-").replace(".", "").lower() sef_url = QUrl.toPercentEncoding(map_name_sef).data().decode('utf-8') self.current_map_url = '{0}map/{1}/{2}'.format(self.api.editor_host, self.api.map.map_id, sef_url) tooltip = ('This is a link to the {0} map published ' + 'from QGIS to GIS Cloud.').format(self.api.map.map_name) self.update_done_dock.open_map.setToolTip(tooltip) tooltip += ("\nChanges you made to your QGIS project " + "since publishing the map are not synced yet.") self.update_dock.open_map.setToolTip(tooltip)
def getSearchRequestData(cls, term): """ Class method to assemble the search request data structure. @param term search term (string) @return tuple of network request object, operation and parameters (QNetworkRequest, QNetworkAccessManager.Operation, QByteArray) """ request = QNetworkRequest(QUrl(cls.SearchUrl)) request.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") op = QNetworkAccessManager.PostOperation params = QByteArray("chain=").append(QUrl.toPercentEncoding(term)) return (request, op, params)
def get_url(value): args = value.split(" ", 1) command = args[0] + " " try: webjump = WEBJUMPS[command] except KeyError: url = urlparse(value) if '.' in url.path and value[0] != ' ' and not url.scheme: return 'https://' + value return value if webjump.allow_args: args = args[1] if len(args) > 1 else "" return webjump.url % str(QUrl.toPercentEncoding(args), "utf-8") else: return webjump.url
def queue_images(self): release_group_id = self.metadata["musicbrainz_releasegroupid"] path = "/api/v1/json/%s/album-mb.php" % (THEAUDIODB_APIKEY, ) queryargs = { "i": bytes(QUrl.toPercentEncoding(release_group_id)).decode() } log.debug("TheAudioDB: Queued download: %s?i=%s", path, queryargs["i"]) self.album.tagger.webservice.get(THEAUDIODB_HOST, THEAUDIODB_PORT, path, self._json_downloaded, priority=True, important=False, parse_response_type='json', queryargs=queryargs) self.album._requests += 1 return CoverArtProvider.WAIT
def getTranslation(self, requestObject, text, originalLanguage, translationLanguage): """ Public method to translate the given text. @param requestObject reference to the request object (TranslatorRequest) @param text text to be translated (string) @param originalLanguage language code of the original (string) @param translationLanguage language code of the translation (string) @return tuple of translated text (string) and flag indicating success (boolean) """ encodedText = str( QUrl.toPercentEncoding(Utilities.html_encode(text + ".")), "utf-8") request = QByteArray( "{{dirCode:'{0}{1}', template:'General', text:'{2}', lang:'de'," " limit:3000, useAutoDetect:true, key:'', ts:'MainSite', tid:''}}". format(self.__mapping[originalLanguage], self.__mapping[translationLanguage], encodedText).encode("utf-8")) response, ok = requestObject.post(QUrl(self.TranslatorUrl), request, "json") if ok: try: responseDict = json.loads(response) except ValueError: return self.tr("Invalid response received"), False if "d" in responseDict: responseDict = responseDict["d"] result = responseDict["result"][:-1] # get rid of stub if responseDict["errCode"] == 0: if responseDict["ptsDirCode"] == "": result = self.tr( "This direction of translation is not available.") ok = False else: result = responseDict["errMessage"] ok = False else: result = response return result, ok
def submitUrl(self, url): """ Public method to submit an URL to be scanned. @param url url to be scanned (QUrl) """ request = QNetworkRequest(QUrl(self.ScanUrlUrl)) request.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") params = QByteArray("key={0}&url=".format( Preferences.getHelp("VirusTotalServiceKey")))\ .append(QUrl.toPercentEncoding(url.toString())) import Helpviewer.HelpWindow nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager() reply = nam.post(request, params) reply.finished.connect(self.__submitUrlFinished) self.__replies.append(reply)
def submitUrl(self, url): """ Public method to submit an URL to be scanned. @param url url to be scanned (QUrl) """ request = QNetworkRequest(QUrl(self.ScanUrlUrl)) request.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") params = QByteArray("apikey={0}&url=".format( Preferences.getWebBrowser("VirusTotalServiceKey")).encode( "utf-8")).append(QUrl.toPercentEncoding(url.toString())) import WebBrowser.WebBrowserWindow nam = (WebBrowser.WebBrowserWindow.WebBrowserWindow.networkManager()) reply = nam.post(request, params) reply.finished.connect(self.__submitUrlFinished) self.__replies.append(reply)
build_qurl, parse_json, ) from picard.util.xml import parse_xml from picard.webservice import ratecontrol COUNT_REQUESTS_DELAY_MS = 250 TEMP_ERRORS_RETRIES = 5 USER_AGENT_STRING = '%s-%s/%s (%s;%s-%s)' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR, platform.platform(), platform.python_implementation(), platform.python_version()) CLIENT_STRING = bytes(QUrl.toPercentEncoding('%s %s-%s' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR))).decode() DEFAULT_RESPONSE_PARSER_TYPE = "json" Parser = namedtuple('Parser', 'mimetype parser') class UnknownResponseParserError(Exception): def __init__(self, response_type): message = "Unknown parser for response type '%s'. Parser for given response type does not exist." % response_type super().__init__(message)
def url_fn(text): if not text: return None return ("https://www.duckduckgo.com/ac/?q=%s&type=list" % str(QUrl.toPercentEncoding(text), "utf-8"))
u'first-seen': getCreated, u'mtime': getModified, u'last-seen': '__last_seen', u'bitrate': getBitRate, u'disc-number': 'discnumber' } setLength = lambda length: {'duration': str(audioinfo.lnglength(length))} setCreated = lambda created: {'first-seen': str(audioinfo.lngtime(created))} setBitrate = lambda bitrate: { 'bitrate': str(audioinfo.lngfrequency(bitrate) / 1000) } setModified = lambda modified: {'last-seen': str(audioinfo.lngtime(modified))} setFilename = lambda filename: { u'location': 'file://' + str(QUrl.toPercentEncoding(filename, '/()"\'')).encode('utf8') } RECONVERSION = { 'title': 'title', 'artist': 'artist', 'album': 'album', 'track': 'track-number', 'discnumber': 'disc-number', 'genre': 'genre', '__length': setLength, '__created': setCreated, '__bitrate': setBitrate, '__modified': setModified, '__filename': setFilename, '__size': 'file-size'
def _encode_url(url): """Encode an QUrl suitable to pass to QWebHistory.""" data = bytes(QUrl.toPercentEncoding(url.toString(), b':/#?&+=@%*')) return data.decode('ascii')
def url_fn(text): if not text: return None return ("https://www.google.com/complete/search?client=firefox&q=" + str(QUrl.toPercentEncoding(text), "utf-8"))
def complete_wikipedia(): return WebJumpRequestCompleter( lambda text: ("https://en.wikipedia.org/w/api.php?action=opensearch&search=" + str( QUrl.toPercentEncoding(text), "utf-8")) if text else None, lambda response: json.loads(str(response, "utf-8"))[1])
from picard import (PICARD_APP_NAME, PICARD_ORG_NAME, PICARD_VERSION_STR, config, log) from picard.const import (ACOUSTID_KEY, ACOUSTID_HOST, ACOUSTID_PORT, CAA_HOST, CAA_PORT) from picard.oauth import OAuthManager from picard.util import build_qurl COUNT_REQUESTS_DELAY_MS = 250 REQUEST_DELAY = defaultdict(lambda: 1000) REQUEST_DELAY[(ACOUSTID_HOST, ACOUSTID_PORT)] = 333 REQUEST_DELAY[(CAA_HOST, CAA_PORT)] = 0 USER_AGENT_STRING = '%s-%s/%s (%s;%s-%s)' % ( PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR, platform.platform(), platform.python_implementation(), platform.python_version()) CLIENT_STRING = string_( QUrl.toPercentEncoding( '%s %s-%s' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR))) def escape_lucene_query(text): return re.sub(r'([+\-&|!(){}\[\]\^"~*?:\\/])', r'\\\1', text) def _wrap_xml_metadata(data): return ( '<?xml version="1.0" encoding="UTF-8"?>' + '<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">%s</metadata>' % data) class XmlNode(object): def __init__(self):
config, log) from picard.oauth import OAuthManager from picard.util import build_qurl, parse_json from picard.util.xml import parse_xml COUNT_REQUESTS_DELAY_MS = 250 TEMP_ERRORS_RETRIES = 5 USER_AGENT_STRING = '%s-%s/%s (%s;%s-%s)' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR, platform.platform(), platform.python_implementation(), platform.python_version()) CLIENT_STRING = string_(QUrl.toPercentEncoding('%s %s-%s' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR))) # ============================================================================ # Throttling/congestion avoidance # ============================================================================ # Throttles requests to a given hostkey by assigning a minimum delay between # requests in milliseconds. # # Plugins may assign limits to their associated service(s) like so: # # >>> from picard.webservice import REQUEST_DELAY_MINIMUM # >>> REQUEST_DELAY_MINIMUM[('myservice.org', 80)] = 100 # 10 requests/second REQUEST_DELAY_MINIMUM = defaultdict(lambda: 1000)
log) from picard.oauth import OAuthManager from picard.util import build_qurl, parse_json from picard.util.xml import parse_xml from picard.webservice import ratecontrol COUNT_REQUESTS_DELAY_MS = 250 TEMP_ERRORS_RETRIES = 5 USER_AGENT_STRING = '%s-%s/%s (%s;%s-%s)' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR, platform.platform(), platform.python_implementation(), platform.python_version()) CLIENT_STRING = string_(QUrl.toPercentEncoding('%s %s-%s' % (PICARD_ORG_NAME, PICARD_APP_NAME, PICARD_VERSION_STR))) DEFAULT_RESPONSE_PARSER_TYPE = "json" Parser = namedtuple('Parser', 'mimetype parser') class UnknownResponseParserError(Exception): def __init__(self, response_type): message = "Unknown parser for response type '%s'. Parser for given response type does not exist." % response_type super().__init__(message)
def encode_queryarg(arg): return bytes(QUrl.toPercentEncoding(arg)).decode()
def url_encode_password(password): return QUrl.toPercentEncoding(password) \ .replace(" ", "+") \ .replace("~", "%6E")
def url_fn(text): if not text: return None # took from https://github.com/jarun/ddgr/blob/master/ddgr return ('https://duckduckgo.com/ac/?q=%s&kl=wt-wt' % str(QUrl.toPercentEncoding(text), "utf-8"))