def send_feedback(dsn, event_id, name, email, comment, timeout): """Send feedback, blocking. Args: dsn (str): The DSN event_id (str): The event ID this feedback should be attached to name (text_type): The user name email (text_type): The user email comment (text_type): The feedback text timeout (float): The timeout for this request Raises: SentryError: In case of timeout or other errors """ name = text_type(name).encode("utf-8") email = text_type(email).encode("utf-8") comment = text_type(comment).encode("utf-8") data = urlencode([('name', name), ('email', email), ('comments', comment)]) if not isinstance(data, bytes): # py3 data = data.encode("utf-8") headers = {"Referer": "https://quodlibet.github.io"} params = urlencode([("dsn", dsn), ("eventId", event_id)]) try: req = Request("https://sentry.io/api/embed/error-page/?" + params, data=data, headers=headers) urlopen(req, timeout=timeout).close() except EnvironmentError as e: raise SentryError(e)
def get_url(url, post=None, get=None): post_params = urlencode(post or {}) get_params = urlencode(get or {}) if get: get_params = '?' + get_params # add post, get data and headers url = '%s%s' % (url, get_params) if post_params: request = Request(url, post_params) else: request = Request(url) # for discogs request.add_header('Accept-Encoding', 'gzip') request.add_header('User-Agent', USER_AGENT) url_sock = urlopen(request) enc = get_encoding_from_socket(url_sock) # unzip the response if needed data = url_sock.read() if url_sock.headers.get("content-encoding", "") == "gzip": data = gzip.GzipFile(fileobj=cBytesIO(data)).read() url_sock.close() content_type = url_sock.headers.get('Content-Type', '').split(';', 1)[0] domain = re.compile('\w+://([^/]+)/').search(url).groups(0)[0] print_d("Got %s data from %s" % (content_type, domain)) return (data if content_type.startswith('image') else data.decode(enc))
def get_url(url, post={}, get={}): post_params = urlencode(post) get_params = urlencode(get) if get: get_params = "?" + get_params # add post, get data and headers url = "%s%s" % (url, get_params) if post_params: request = Request(url, post_params) else: request = Request(url) # for discogs request.add_header("Accept-Encoding", "gzip") request.add_header("User-Agent", USER_AGENT) url_sock = urlopen(request) enc = get_encoding_from_socket(url_sock) # unzip the response if needed data = url_sock.read() if url_sock.headers.get("content-encoding", "") == "gzip": data = gzip.GzipFile(fileobj=cBytesIO(data)).read() url_sock.close() return data, enc
def send_feedback(dsn, event_id, name, email, comment, timeout): """Send feedback, blocking. Args: dsn (str): The DSN event_id (str): The event ID this feedback should be attached to name (text_type): The user name email (text_type): The user email comment (text_type): The feedback text timeout (float): The timeout for this request Raises: SentryError: In case of timeout or other errors """ name = text_type(name).encode("utf-8") email = text_type(email).encode("utf-8") comment = text_type(comment).encode("utf-8") data = urlencode( [('name', name), ('email', email), ('comments', comment)]) if not isinstance(data, bytes): # py3 data = data.encode("utf-8") headers = {"Referer": "https://quodlibet.github.io"} params = urlencode([("dsn", dsn), ("eventId", event_id)]) try: req = Request( "https://sentry.io/api/embed/error-page/?" + params, data=data, headers=headers) urlopen(req, timeout=timeout).close() except EnvironmentError as e: raise SentryError(e)
def __process(self, results): req_data = [] req_data.append( urlencode({ "format": "json", "client": APP_KEY, "batch": "1", })) for i, result in enumerate(results): postfix = ".%d" % i req_data.append( urlencode({ "duration" + postfix: str(int(round(result.length))), "fingerprint" + postfix: result.chromaprint, })) req_data.append("meta=releases+recordings+tracks+sources") urldata = "&".join(req_data) obj = cBytesIO() gzip.GzipFile(fileobj=obj, mode="wb").write(urldata.encode()) urldata = obj.getvalue() headers = { "Content-Encoding": "gzip", "Content-type": "application/x-www-form-urlencoded" } req = Request(self.URL, urldata, headers) releases = {} error = "" try: response = urlopen(req, timeout=self.TIMEOUT) except EnvironmentError as e: error = "urllib error: " + str(e) else: try: data = response.read() data = json.loads(data.decode()) except ValueError as e: error = str(e) else: if data["status"] == "ok": for result_data in data.get("fingerprints", []): if "index" not in result_data: continue index = result_data["index"] releases[index] = parse_acoustid_response(result_data) for i, result in enumerate(results): yield LookupResult(result, releases.get(str(i), []), error)
def __process(self, results): req_data = [] req_data.append(urlencode({ "format": "json", "client": APP_KEY, "batch": "1", })) for i, result in enumerate(results): postfix = ".%d" % i req_data.append(urlencode({ "duration" + postfix: str(int(round(result.length))), "fingerprint" + postfix: result.chromaprint, })) req_data.append("meta=releases+recordings+tracks+sources") urldata = "&".join(req_data) obj = cBytesIO() gzip.GzipFile(fileobj=obj, mode="wb").write(urldata) urldata = obj.getvalue() headers = { "Content-Encoding": "gzip", "Content-type": "application/x-www-form-urlencoded" } req = Request(self.URL, urldata, headers) releases = {} error = "" try: response = urlopen(req, timeout=self.TIMEOUT) except EnvironmentError as e: error = "urllib error: " + str(e) else: try: data = response.read() data = json.loads(data) except ValueError as e: error = str(e) else: if data["status"] == "ok": for result_data in data.get("fingerprints", []): if "index" not in result_data: continue index = result_data["index"] releases[index] = parse_acoustid_response(result_data) for i, result in enumerate(results): yield LookupResult(result, releases.get(str(i), []), error)
def _put(self, path, callback, **kwargs): args = self._default_params() args.update(kwargs) msg = Soup.Message.new('PUT', self._url(path)) body = urlencode(args) msg.set_request('application/x-www-form-urlencoded', Soup.MemoryUse.COPY, body) download_json(msg, self._cancellable, callback, None)
def _authorize_url(self): url = '%s/connect' % (self.API_ROOT, ) options = { 'scope': 'non-expiring', 'client_id': self.__CLIENT_ID, 'response_type': 'code', 'redirect_uri': self.REDIRECT_URI } return '%s?%s' % (url, urlencode(options))
def _authorize_url(self): url = '%s/connect' % (self.API_ROOT,) options = { 'scope': 'non-expiring', 'client_id': self.__CLIENT_ID, 'response_type': 'code', 'redirect_uri': self.REDIRECT_URI } return '%s?%s' % (url, urlencode(options))
def _post(self, path, callback, **kwargs): args = self._default_params() args.update(kwargs) msg = Soup.Message.new('POST', self._url(path)) post_body = urlencode(args) if not isinstance(post_body, bytes): post_body = post_body.encode("ascii") msg.set_request('application/x-www-form-urlencoded', Soup.MemoryUse.COPY, post_body) download_json(msg, self._cancellable, callback, None)
def _delete(self, path, callback, **kwargs): args = self._default_params() args.update(kwargs) # Turns out the SC API doesn't mind body arguments for DELETEs, # and as it's neater and slightly more secure, let's do that. body = urlencode(args) msg = Soup.Message.new('DELETE', self._url(path)) msg.set_request('application/x-www-form-urlencoded', Soup.MemoryUse.COPY, body) download(msg, self._cancellable, callback, None, try_decode=False)
def build_issue_url(title, body): """Returns an URL which provides a pre-filled github issue. Args: title (text_type): The issue title body (text_type): The issue content Returns: str: the URL to open """ title = text_type(title).encode("utf-8") body = text_type(body).encode("utf-8") params = urlencode([("title", title), ("body", body)]) return "https://github.com/quodlibet/quodlibet/issues/new?%s" % params
def __send(self, urldata): if self.__stopped: return gatekeeper.wait() self.__done += len(urldata) basedata = urlencode({ "format": "xml", "client": APP_KEY, "user": get_api_key(), }) urldata = "&".join([basedata] + list(map(urlencode, urldata))) obj = cBytesIO() gzip.GzipFile(fileobj=obj, mode="wb").write(urldata.encode()) urldata = obj.getvalue() headers = { "Content-Encoding": "gzip", "Content-type": "application/x-www-form-urlencoded" } req = Request(self.URL, urldata, headers) error = None try: response = urlopen(req, timeout=self.TIMEOUT) except EnvironmentError as e: error = "urllib error: " + str(e) else: xml = response.read() try: dom = parseString(xml) except: error = "xml error" else: status = dom.getElementsByTagName("status") if not status or not status[0].childNodes or not \ status[0].childNodes[0].nodeValue == "ok": error = "response status error" if error: print_w("[fingerprint] Submission failed: " + error) # emit progress self.__idle(self.__progress_cb, float(self.__done) / len(self.__results))
def __send(self, urldata): if self.__stopped: return gatekeeper.wait() self.__done += len(urldata) basedata = urlencode({ "format": "xml", "client": APP_KEY, "user": get_api_key(), }) urldata = "&".join([basedata] + map(urlencode, urldata)) obj = cBytesIO() gzip.GzipFile(fileobj=obj, mode="wb").write(urldata) urldata = obj.getvalue() headers = { "Content-Encoding": "gzip", "Content-type": "application/x-www-form-urlencoded" } req = Request(self.URL, urldata, headers) error = None try: response = urlopen(req, timeout=self.TIMEOUT) except EnvironmentError as e: error = "urllib error: " + str(e) else: xml = response.read() try: dom = parseString(xml) except: error = "xml error" else: status = dom.getElementsByTagName("status") if not status or not status[0].childNodes or not \ status[0].childNodes[0].nodeValue == "ok": error = "response status error" if error: print_w("[fingerprint] Submission failed: " + error) # emit progress self.__idle(self.__progress_cb, float(self.__done) / len(self.__results))
def apicall(method, **kwargs): """Performs Last.fm API call.""" real_args = { 'api_key': API_KEY, 'format': 'json', 'method': method, } real_args.update(kwargs) url = ''.join(["https://ws.audioscrobbler.com/2.0/?", urlencode(real_args)]) log(url) uobj = urlopen(url) resp = json.load(uobj) if 'error' in resp: errmsg = 'Last.fm API error: %s' % resp.get('message', '') log(errmsg) raise EnvironmentError(resp['error'], errmsg) return resp
def apicall(method, **kwargs): """Performs Last.fm API call.""" real_args = { 'api_key': API_KEY, 'format': 'json', 'method': method, } real_args.update(kwargs) url = ''.join( ["https://ws.audioscrobbler.com/2.0/?", urlencode(real_args)]) log(url) uobj = urlopen(url) resp = json.load(uobj) if 'error' in resp: errmsg = 'Last.fm API error: %s' % resp.get('message', '') log(errmsg) raise EnvironmentError(resp['error'], errmsg) return resp
def _check_submit(self, url, data): data_str = urlencode(data).encode("ascii") try: resp = urlopen(url, data_str) except EnvironmentError: print_d("Audioscrobbler server not responding, will try later.") return False resp_save = resp.read().decode("utf-8", "ignore") status = resp_save.rstrip().split("\n")[0] print_d("Submission status: %s" % status) if status == "OK": return True elif status == "BADSESSION": self.handshake_sent = False return False else: return False
def _check_submit(self, url, data): data_str = urlencode(data) try: resp = urlopen(url, data_str) except EnvironmentError: print_d("Audioscrobbler server not responding, will try later.") return False resp_save = resp.read() status = resp_save.rstrip().split("\n")[0] print_d("Submission status: %s" % status) if status == "OK": return True elif status == "BADSESSION": self.handshake_sent = False return False else: return False
def _url(self, path, args=None): path = "%s%s" % (self.root, path) return "%s?%s" % (path, urlencode(args)) if args else path