def check(self, decoded, encoded, safe=b'', encoding='utf-8'): self.assertEqual(uriencode(decoded, safe, encoding), encoded) self.assertEqual(uridecode(encoded, encoding), decoded) # swap bytes/string types self.assertEqual(uriencode(decoded.encode(encoding), safe, encoding), encoded) # noqa self.assertEqual(uridecode(encoded.decode('ascii'), encoding), decoded)
def redirectIfNotLoggedIn(self): if not self.getCurrentUser().is_authenticated: resp = self.error_response([authenticationNeeded], 302) startUrl = self.app.config.get("LOGIN_URL") requestUrl = self.getRequest().url uri = "{0}?next={1}".format(startUrl, uritools.uriencode(requestUrl).decode('utf-8')) resp.headers['Location'] = uri return resp
def test_auth_interface_redirects_to_redirect_uri(self): uri, baseUrl, uriBase, queryString = self.buildUris(self.app2) # @UnusedVariable targetUri = "{0}?next={1}".format(app.config.get('LOGIN_URL'), uritools.uriencode(uri).decode('utf-8')) with app.test_client() as client: resp = client.get( uriBase, query_string=queryString, base_url=baseUrl) self.assertEqual(resp.status_code, 302) self.assertEqual(resp.headers['Location'],targetUri)
def buildUris(self, theApp): baseUrl = app.config.get('BASE_URL') uriBase = "/v1/oauth2/auth" uri = "{0}{1}?redirect_uri={2}&response_type=code&client_id={3}".format(baseUrl, uriBase, uritools.uriencode(theApp.redirect_uri).decode('utf-8'), theApp.appid) queryString = uri.split('?')[1] return uri, baseUrl, uriBase, queryString
def woocommerce_oauth_request(http_method, consumer_key, consumer_secret, base_uri, params=None, **kwargs): if params is None: params = {} base_uri_encoded = uriencode(base_uri) params["oauth_consumer_key"] = consumer_key params["oauth_timestamp"] = str(int(time())) params["oauth_nonce"] = "".join([str(hex(randint(0, 15)).split("x")[1]) for i in range(32)]) params["oauth_signature_method"] = "HMAC-SHA256" params_sorted_list = sorted(["%s=%s" % (key, value) for key, value in params.items()]) params_encoded = uriencode("&".join(params_sorted_list)) raw = "&".join([http_method, base_uri_encoded, params_encoded]) hashed = hmac.new(consumer_secret + "&", raw, sha256) oauth_signature = hashed.digest().encode("base64").rstrip("\n") params["oauth_signature"] = oauth_signature return requests.request(http_method, base_uri, params=params, **kwargs)
def browse(self, uri): logger.debug(u"Browse being called for %s" % uri) level = urisplit(uri).path query = self._sanitize_query(dict(urisplit(uri).getquerylist())) logger.debug("Got parsed to level: %s - query: %s" % (level, query)) result = [] if not level: logger.error("No level for uri %s" % uri) # import pdb; pdb.set_trace() if level == 'root': for row in self._browse_genre(): result.append(Ref.directory( uri=uricompose('beetslocal', None, 'genre', dict(genre=row[0])), name=row[0] if bool(row[0]) else u'No Genre')) elif level == "genre": # artist refs not browsable via mpd for row in self._browse_artist(query): result.append(Ref.directory( uri=uricompose('beetslocal', None, 'artist', dict(genre=query['genre'][0], artist=row[1])), name=row[0] if bool(row[0]) else u'No Artist')) elif level == "artist": for album in self._browse_album(query): result.append(Ref.album( uri=uricompose('beetslocal', None, 'album', dict(album=album.id)), name=album.album)) elif level == "album": for track in self._browse_track(query): result.append(Ref.track( uri="beetslocal:track:%s:%s" % ( track.id, uriencode(track.path, '/')), name=track.title)) else: logger.debug('Unknown URI: %s', uri) # logger.debug(result) return result
def get_signature(method="GET", path="/", params=None): import uritools if params is None: params = {} params.update({"api_key": api_key, "request_time": int(time.time())}) sorted_params = sorted(params.items()) query_string = "&".join(["{}={}".format(k, uritools.uriencode(str(v))) for (k, v) in sorted_params if v]) path = os.path.join("/buzzfeed/api", BUZZ_API_VERSION, path.strip("/")) string_to_sign = "GET\n{}\n{}".format(path, query_string) signature = base64.b64encode(hmac.new(api_key_secret, msg=string_to_sign, digestmod=hashlib.sha256).digest()) sorted_params.append(("signature", signature)) new_parms = OrderedDict(sorted_params) return new_parms
def browse(self, uri): logger.debug(u"Browse being called for %s" % uri) level = urisplit(uri).path query = self._sanitize_query(dict(urisplit(uri).getquerylist())) logger.debug("Got parsed to level: %s - query: %s" % (level, query)) result = [] if not level: logger.error("No level for uri %s" % uri) # import pdb; pdb.set_trace() if level == 'root': for row in self._browse_genre(): result.append( Ref.directory( uri=uricompose('beetslocal', None, 'genre', dict(genre=row[0])), name=row[0] if bool(row[0]) else u'No Genre')) elif level == "genre": # artist refs not browsable via mpd for row in self._browse_artist(query): result.append( Ref.directory( uri=uricompose( 'beetslocal', None, 'artist', dict(genre=query['genre'][0], artist=row[1])), name=row[0] if bool(row[0]) else u'No Artist')) elif level == "artist": for album in self._browse_album(query): result.append( Ref.album(uri=uricompose('beetslocal', None, 'album', dict(album=album.id)), name=album.album)) elif level == "album": for track in self._browse_track(query): result.append( Ref.track(uri="beetslocal:track:%s:%s" % (track.id, uriencode(track.path, '/')), name=track.title)) else: logger.debug('Unknown URI: %s', uri) # logger.debug(result) return result
def get_signature(method='GET', path='/', params=None): BUZZ_API_VERSION = 'v2' api_key = settings.get('API_KEY') api_key_secret = settings.get('SECRET') if params is None: params = {} params.update({ 'api_key': api_key, 'request_time': int(time.time()), }) sorted_params = sorted(params.items()) query_string = '&'.join(['%s=%s' % (k, uritools.uriencode(str(v))) for (k, v) in sorted_params if v]) path = os.path.join('/buzzfeed/api', BUZZ_API_VERSION, path.strip('/')) string_to_sign = "GET\n%s\n%s" % (path, query_string) signature = base64.b64encode(hmac.new(api_key_secret, msg=string_to_sign, digestmod=hashlib.sha256).digest()) sorted_params.append(('signature', signature)) new_parms = OrderedDict(sorted_params) return new_parms
def _convert_item(self, item): """ Transforms a beets item into a mopidy Track """ if not item: return track_kwargs = {} album_kwargs = {} artist_kwargs = {} albumartist_kwargs = {} if 'track' in item: track_kwargs['track_no'] = int(item['track']) if 'tracktotal' in item: album_kwargs['num_tracks'] = int(item['tracktotal']) if 'artist' in item: artist_kwargs['name'] = item['artist'] albumartist_kwargs['name'] = item['artist'] if 'albumartist' in item: albumartist_kwargs['name'] = item['albumartist'] if 'album' in item: album_kwargs['name'] = item['album'] if 'title' in item: track_kwargs['name'] = item['title'] if 'disc' in item: track_kwargs['disc_no'] = item['disc'] if 'genre' in item: track_kwargs['genre'] = item['genre'] if 'comments' in item: track_kwargs['comment'] = item['comments'] if 'bitrate' in item: track_kwargs['bitrate'] = item['bitrate'] if 'mtime' in item: track_kwargs['last_modified'] = int(item['mtime'] * 1000) if self.backend.use_original_release_date: if 'original_year' in item: track_kwargs['date'] = self._build_date( item['original_year'], item['original_month'], item['original_day']) else: if 'year' in item: track_kwargs['date'] = self._build_date( item['year'], item['month'], item['day']) if 'mb_trackid' in item: track_kwargs['musicbrainz_id'] = item['mb_trackid'] if 'mb_albumid' in item: album_kwargs['musicbrainz_id'] = item['mb_albumid'] if 'mb_artistid' in item: artist_kwargs['musicbrainz_id'] = item['mb_artistid'] if 'mb_albumartistid' in item: albumartist_kwargs['musicbrainz_id'] = ( item['mb_albumartistid']) if 'path' in item: track_kwargs['uri'] = "beetslocal:track:%s:%s" % ( item['id'], uriencode(item['path'], '/')) if 'length' in item: track_kwargs['length'] = int(item['length']) * 1000 if artist_kwargs: artist = Artist(**artist_kwargs) track_kwargs['artists'] = [artist] if albumartist_kwargs: albumartist = Artist(**albumartist_kwargs) album_kwargs['artists'] = [albumartist] if album_kwargs: album = Album(**album_kwargs) track_kwargs['album'] = album track = Track(**track_kwargs) return track
def check(self, decoded, encoded, safe='', encoding='utf-8'): self.assertEqual(uriencode(decoded, safe, encoding), encoded) self.assertEqual(uridecode(encoded, encoding), decoded) # swap bytes/string types self.assertEqual(uriencode(decoded.encode(encoding), safe, encoding), encoded) # noqa self.assertEqual(uridecode(encoded.decode('ascii'), encoding), decoded)
def test_encode_bytes(self): cases = [ (b'\xf6lk\xfcrbis', b'%F6lk%FCrbis') ] for input, output in cases: self.assertEqual(uriencode(input, encoding=None), output)
def geturl(self, identifier, filename=None): if filename: path = identifier + '/' + uritools.uriencode(filename) else: path = identifier + '/' return uritools.urijoin(self.base_url, '/download/' + path)
def uri(feedurl, guid=None, safe=uritools.SUB_DELIMS+b':@/?'): uri = uritools.uridefrag('podcast+' + feedurl).uri if guid: return uri + '#' + uritools.uriencode(guid, safe=safe) else: return uri
def _convert_item(self, item): """ Transforms a beets item into a mopidy Track """ if not item: return track_kwargs = {} album_kwargs = {} artist_kwargs = {} albumartist_kwargs = {} if 'track' in item: track_kwargs['track_no'] = int(item['track']) if 'tracktotal' in item: album_kwargs['num_tracks'] = int(item['tracktotal']) if 'artist' in item: artist_kwargs['name'] = item['artist'] albumartist_kwargs['name'] = item['artist'] if 'albumartist' in item: albumartist_kwargs['name'] = item['albumartist'] if 'album' in item: album_kwargs['name'] = item['album'] if 'title' in item: track_kwargs['name'] = item['title'] if 'disc' in item: track_kwargs['disc_no'] = item['disc'] if 'genre' in item: track_kwargs['genre'] = item['genre'] if 'comments' in item: track_kwargs['comment'] = item['comments'] if 'bitrate' in item: track_kwargs['bitrate'] = item['bitrate'] if 'mtime' in item: track_kwargs['last_modified'] = int(item['mtime'] * 1000) if self.backend.use_original_release_date: if 'original_year' in item: track_kwargs['date'] = self._build_date( item['original_year'], item['original_month'], item['original_day']) else: if 'year' in item: track_kwargs['date'] = self._build_date( item['year'], item['month'], item['day']) if 'mb_trackid' in item: track_kwargs['musicbrainz_id'] = item['mb_trackid'] if 'mb_albumid' in item: album_kwargs['musicbrainz_id'] = item['mb_albumid'] if 'mb_artistid' in item: artist_kwargs['musicbrainz_id'] = item['mb_artistid'] if 'mb_albumartistid' in item: albumartist_kwargs['musicbrainz_id'] = (item['mb_albumartistid']) if 'path' in item: track_kwargs['uri'] = "beetslocal:track:%s:%s" % ( item['id'], uriencode(item['path'], '/')) if 'length' in item: track_kwargs['length'] = int(item['length']) * 1000 if artist_kwargs: artist = Artist(**artist_kwargs) track_kwargs['artists'] = [artist] if albumartist_kwargs: albumartist = Artist(**albumartist_kwargs) album_kwargs['artists'] = [albumartist] if album_kwargs: album = Album(**album_kwargs) track_kwargs['album'] = album track = Track(**track_kwargs) return track
def getitemuri(self, guid, safe=uritools.SUB_DELIMS+b':@/?'): return self.uri + '#' + uritools.uriencode(guid, safe=safe)
def getitemuri(self, guid, safe=uritools.SUB_DELIMS + b':@/?'): return self.uri + '#' + uritools.uriencode(guid, safe=safe)