def get_Lyrics(track_Name, artist_Name): method = "matcher.lyrics.get" title = "&q_track=" artist = "&q_artist=" _title = encode(track_Name) _artist = encode(artist_Name) parameters = title + _title + artist + _artist return call_API(method, parameters)
def urlEncodeAndJoin(self, seq, sepr=','): '''sepr.join(urlencode(seq)) Args: seq: string list to be urlencoded sepr: join seq with sepr Returns: str ''' try: from urllib.parse import quote_plus as encode return sepr.join([encode(x, encoding=CHARSET_UTF8) for x in seq]) except ImportError: from urllib import quote as encode return sepr.join([i for i in map(lambda x: encode(x), seq)])
def _url_request(self, url, request_parameters, request_type='GET', files=None, repeat=0, error_text="Error", raise_on_failure=True): """Send a new request and format the json response. Keyword arguments: url - the url of the request request_parameters - a dictionay containing the name of the parameter and its correspoinsding value request_type - the type of request: 'GET', 'POST' files - the files to be uploaded repeat - the nuber of times to repeat the request in the case of a failure error_text - the message to log if an error is returned raise_on_failure - indicates if an exception should be raised if an error is returned and repeat is 0""" if files is not None: mpf = _MultiPartForm(param_dict=request_parameters, files=files) req = request(url) body = mpf.make_result req.add_header('Content-type', mpf.get_content_type()) req.add_header('Content-length', len(body)) req.data = body elif request_type == 'GET': req = request('?'.join((url, encode(request_parameters)))) else: headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',} req = request(url, encode(request_parameters).encode('UTF-8'), headers) req.add_header('Accept-encoding', 'gzip') response = urlopen(req) if response.info().get('Content-Encoding') == 'gzip': buf = io.BytesIO(response.read()) with gzip.GzipFile(fileobj=buf) as gzip_file: response_bytes = gzip_file.read() else: response_bytes = response.read() response_text = response_bytes.decode('UTF-8') response_json = json.loads(response_text) if "error" in response_json: if repeat == 0: if raise_on_failure: raise Exception("{0}: {1}".format(error_text, response_json)) return response_json repeat -= 1 time.sleep(2) response_json = self._url_request( url, request_parameters, request_type, files, repeat, error_text) return response_json
def write_save(self, level=0): if level == 0: return self.write_save(1) elif level == 3: print("Warning: commented save not available") return self.write_save(2) ret = "" ret += self.version + "|" # Game version ret += "|" # "//just in case we need some more stuff here" ret += ";".join( str(int(round(x * 1000))) for x in (self.session_start, self.legacy_start, self.save_tstamp)) + ";" + ";".join( (self.name, self.seed)) + "|" ret += "".join(self.ALL_ON_PREFERENCES[x] if self. preferences[x] else toggle(self.ALL_ON_PREFERENCES[x]) for x in range(len(self.preferences))) + "|" ret += self.cookie_data + "|" ret += self.building_data + "|" ret += self.upgrades + "|" ret += " ".strip().join("1" if self.achievements[x] else "0" for x in range(achievements.v)) + "|" ret += self.buffs if level == 2: return ret ret = encode(base64.b64encode(ret.encode("utf-8")) + b"!END!") return ret
def driverdata(self, drivername): """ Personal data of driver using its name in the request (i.e drivername="Victor Beltran"). """ URL_DRIVER_STATUS = "https://members.iracing.com/membersite/member/" \ "GetDriverStatus?%s" r = self.__request(method="GET", url=URL_DRIVER_STATUS % (encode({ 'searchTerms': drivername}))) return r.json()
def sound(text): t = encode((('text', text), ('format', 'wav'), ('lang', 'ru-RU'), ('speaker', 'zahar'), ('key', '1e0146ea-0b80-425b-8057-fd2feb8ef590'))) ans = url('https://tts.voicetech.yandex.net/generate?' + t).read() f = open('bereza.wav', 'wb') f.write(ans) play('bereza.wav', fn)
def get_Genius_Song_ID_From_Track_Name_And_Artist_Name(track_Name, artist): full_Name = track_Name+' '+artist query=encode(full_Name) search_url = genius_Base_Url + '/search?q='+query response=requests.get(search_url, headers=headers) result=response.json()['response']['hits'] if result!=[]: song_ID=find_Closest_Match(result, full_Name) return song_ID
def url_request(self, in_url, params=None, req_type="GET", headers=None): if params == None: params = {'f': 'json'} elif 'f' not in params: params['f'] = 'json' if "token" not in params and self.token: params['token'] = self.token if req_type == 'GET': req = request('?'.join((in_url, encode(params)))) elif req_type == 'MULTIPART': req = request(in_url, params) else: req = request(in_url, encode(params).encode('UTF-8')) req.add_header('Accept-encoding', 'gzip') req.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') req.add_header('User-Agent', 'AllFunctions.py') if headers: for key, value in list(headers.items()): req.add_header(key, value) try: response = urlopen(req) except HTTPError as e: print("HTTP_ERROR_MSG {} -- {}".format(in_url, e.code)) return except URLError as e: print("URL_ERROR_MSG {} -- {}".format(in_url, e.reason)) return if response.info().get('Content-Encoding') == 'gzip': buf = BytesIO(response.read()) with gzip.GzipFile(fileobj=buf) as gzip_file: response_bytes = gzip_file.read() else: response_bytes = response.read() response_text = response_bytes.decode('UTF-8') return json.loads(response_text)
def search(query, year=''): result = requests.get(SEARCHURL.format(TMDbToken, encode(query), year)).json() if result['total_results'] == 0: return ('Error: Movie Not Found') movies = result['results'] for movie in movies: if movie['title'].lower() == query.lower(): return movieData(movie) return movieData(movies[0])
def sendReq(self, URL, query_dict=None, headers=None): # Takes a URL and a dictionary and sends the request, returns the JSON # qData = parse.urlencode(qDict).encode('UTF-8') if qDict else None if query_dict: query_string = encode(query_dict).encode('UTF-8') else: query_string = encode('').encode('UTF-8') if headers: req = request(URL) for key, value in headers.iteritems(): req.add_header(key, value) else: req = URL jsonResponse = urlopen(req, query_string) jsonOuput = json.loads(jsonResponse.read().decode('utf-8')) return jsonOuput
def testComplex(self): urlQuery = ('item1={0}&item2[key1.1][][subkey1.1][]={1}&item2[key1.1][][]={1}' '&item2[key2.1]={2}&item2[key3.1][subkey3.1]={3}&item3[key1.1][subkey1.1]={4}&noValue' ).format(encode('''!D"e#m$o%'''), encode('''&D'e(m)o*'''), encode('''+D e,m-o.'''), encode('''/D[e]m>o<'''), encode('Demo')) parseDict = parseStr(urlQuery) matchDict = { 'item1': '!D"e#m$o%', 'item2': { 'key1.1': [{'subkey1.1': ["&D'e(m)o*"]}, "&D'e(m)o*"], 'key2.1': '+D e,m-o.', 'key3.1': {'subkey3.1': '/D[e]m>o<'} }, 'item3': { 'key1.1': {'subkey1.1': 'Demo'} }, 'noValue': None } self.assertDictEqual(parseDict, matchDict, 'Complex: {0}'.format(urlQuery))
def _get_clans_by_name(token, cla_name): logger.info(f"Obtendo a lista de clãs pelo nome {cla_name}") url = BASE_URL + "clans?name=" + encode(cla_name) headers = { 'Authorization': 'Bearer ' + token } logger.debug("Realizando requisição GET: " + url) try: response = requests.request("GET", url, headers=headers, data = {}) logger.debug(json.dumps(response.json(), indent=3)) return response.json()["items"] except Exception as err: message_error = ("Erro ao realizar a requisição GET para obter a lista de clãs. " f"Detalhes: {err}") logger.error(message_error) raise
def _get_list_of_clan_members(token, clan_tag): logger.info(f"Obtendo a lista dos membros do clã {clan_tag}") url = BASE_URL + "clans/" + encode(clan_tag) + "/members" headers = { 'Authorization': 'Bearer ' + token } logger.debug("Realizando requisição GET: " + url) try: response = requests.request("GET", url, headers=headers, data = {}) logger.debug(json.dumps(response.json(), indent=3)) return response.json()["items"] except Exception as err: message_error = ("Erro ao realizar a requisição GET para obter a lista de membros do clã. " f"Detalhes: {err}") logger.error(message_error) raise
import urllib.request as ur import urllib.parse as up import sys webpage = sys.argv[1] request = ur.Request(webpage,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'}) data = up.encode({'name':'*****@*****.**'}) web_code = ur.urlopen(request,data).read() print(webpage) """ This idea of this script was to make requests in webpage that are supposed to help users recover their passwords. In case the email or username are incorrect, the page should give us an error and that tells us the username does not exist, in the contrary we know the username exists. Maybe it would be a good idea to use Requests to do this. """
def parseAndReadable(weirdDateAndTime): dateTimeObject = parse(weirdDateAndTime) return dateTimeObject.date().strftime("%d %B, %Y (%A)") method="chart.tracks.get" parameters = "?format=jsonp&callback=callback&country=in" topTracksIndiaData = callAPI(method,parameters) listOfTracks={} for trackDictionary in topTracksIndiaData["message"]["body"]["track_list"]: for dictionary in trackDictionary.items(): title = "&q_track=" artist = "&q_artist=" _title= encode(dictionary[1]["track_name"]) _artist = encode(dictionary[1]["artist_name"]) parameters = title + _title + artist + _artist method="matcher.lyrics.get" lyrics = callAPI(method,parameters) listOfTracks[ dictionary[1]["track_name"]] = \ { "track_id": dictionary[1]["track_id"], "first_release_date": parseAndReadable(dictionary[1]["first_release_date"]), "artist_name": dictionary[1]["track_name"], "lyrics": lyrics["message"]["body"]["lyrics"]["lyrics_body"] } #track_id = 86487954 for track_name, trackInfo in listOfTracks.items():
def get_Genius_Song_ID(track_Name, artist): query = encode(track_Name + ' ' + artist) search_url = genius_Base_Url + '/search?q=' + query response = requests.get(search_url, headers=headers) if response.json()['response']['hits'] != []: return response.json()['response']['hits'][0]['result']['id']
def make_shiled_url(): return "https://img.shields.io/badge/{left}-{right}-{color}.svg".format( left=encode(left), right=encode(right), color=encode(color))
def _url_request(self, url, request_parameters, request_type='GET', files=None, repeat=0, error_text="Error", raise_on_failure=True): """Send a new request and format the json response. Keyword arguments: url - the url of the request request_parameters - a dictionay containing the name of the parameter and its correspoinsding value request_type - the type of request: 'GET', 'POST' files - the files to be uploaded repeat - the nuber of times to repeat the request in the case of a failure error_text - the message to log if an error is returned raise_on_failure - indicates if an exception should be raised if an error is returned and repeat is 0""" if files is not None: mpf = _MultiPartForm(param_dict=request_parameters, files=files) req = request(url) body = mpf.make_result req.add_header('Content-type', mpf.get_content_type()) req.add_header('Content-length', len(body)) req.data = body elif request_type == 'GET': req = request('?'.join((url, encode(request_parameters)))) else: headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', } req = request(url, encode(request_parameters).encode('UTF-8'), headers) req.add_header('Accept-encoding', 'gzip') response = urlopen(req) if response.info().get('Content-Encoding') == 'gzip': buf = io.BytesIO(response.read()) with gzip.GzipFile(fileobj=buf) as gzip_file: response_bytes = gzip_file.read() else: response_bytes = response.read() response_text = response_bytes.decode('UTF-8') response_json = json.loads(response_text) if "error" in response_json: if repeat == 0: if raise_on_failure: raise Exception("{0}: {1}".format(error_text, response_json)) return response_json repeat -= 1 time.sleep(2) response_json = self._url_request(url, request_parameters, request_type, files, repeat, error_text) return response_json
def url_request(self, in_url, request_parameters=None, request_type='GET', additional_headers=None, files=None, repeat=0): """ Make a request to the portal, provided a portal URL and request parameters, returns portal response. By default, returns a JSON response, and reuses the current token. Arguments: in_url -- portal url request_parameters -- dictionary of request parameters. request_type -- HTTP verb (default: GET) additional_headers -- any headers to pass along with the request. files -- any files to send. repeat -- repeat the request up to this number of times. Returns: dictionary of response from portal instance. """ # multipart requests pre-encode the parameters if request_type == 'MULTIPART': parameters = request_parameters else: parameters = {'f': 'json'} # if we haven't logged in yet, won't have a valid token if self.token: parameters['token'] = self.token if request_parameters: parameters.update(request_parameters) if request_type == 'GET': req = request('?'.join((in_url, encode(parameters)))) elif request_type == 'MULTIPART': req = request(in_url, parameters) elif request_type == 'WEBMAP': if files: req = request(in_url, *self.encode_multipart_data(parameters, files)) else: arcpy.AddWarning("Multipart request made, but no files provided.") return else: req = request( in_url, encode(parameters).encode('UTF-8'), self.headers) if additional_headers: for key, value in list(additional_headers.items()): req.add_header(key, value) req.add_header('Accept-encoding', 'gzip') try: response = urlopen(req) except HTTPError as e: arcpy.AddWarning("{} {} -- {}".format( HTTP_ERROR_MSG, in_url, e.code)) return except URLError as e: arcpy.AddWarning("{} {} -- {}".format( URL_ERROR_MSG, in_url, e.reason)) return if response.info().get('Content-Encoding') == 'gzip': buf = BytesIO(response.read()) with gzip.GzipFile(fileobj=buf) as gzip_file: response_bytes = gzip_file.read() else: response_bytes = response.read() response_text = response_bytes.decode('UTF-8') # occasional timing conflicts; repeat until we get back a valid response. response_json = json.loads(response_text) # Check that data returned is not an error object if not response_json or "error" in response_json: rerun = False if repeat > 0: repeat -= 1 rerun = True # token has expired. Revalidate, then rerun request if response_json['error']['code'] is 498: if self.debug: arcpy.AddWarning("token invalid, retrying.") if self.login_method is 'token': # regenerate the token if we're logged in via the application self.token_login() else: self.login(self.username, self._password, repeat=0) # after regenerating token, we should have something long-lived if not self.token or self.valid_for < 5: arcpy.AddError("Unable to get signin token.") return rerun = True if rerun: time.sleep(2) response_json = self.url_request( in_url, request_parameters, request_type, additional_headers, files, repeat) return response_json
def translate(text_to_translate, to_languages='auto', from_language='auto'): agents = {'User-Agent':"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)"} translation_locator = 'class="t0">' text_to_translate = text_to_translate if isinstance(text_to_translate, list) else [text_to_translate] to_languages = to_languages if isinstance(to_languages, list) else [to_languages] from_language = from_language[0] if isinstance(from_language, list) else from_language result = {} for to_language in to_languages: result[to_language] = [] print('translating to: ' + to_language + ' ...' ) for text in text_to_translate: req = request.Request( "http://translate.google.com/m?hl=%s&sl=%s&q=%s" % (to_language, from_language, encode(text).replace(" ", "+")), headers=agents) page = request.urlopen(req) encoding = page.headers['content-type'].split('charset=')[-1] content = str(page.read(), encoding=encoding) word_translation = content[content.find(translation_locator) + len(translation_locator):].split("<")[0] result[to_language].append( word_translation ) return result
def url_request(self, in_url, request_parameters=None, request_type='GET', additional_headers=None, files=None, repeat=0): """ Make a request to the portal, provided a portal URL and request parameters, returns portal response. By default, returns a JSON response, and reuses the current token. Arguments: in_url -- portal url request_parameters -- dictionary of request parameters. request_type -- HTTP verb (default: GET) additional_headers -- any headers to pass along with the request. files -- any files to send. repeat -- repeat the request up to this number of times. Returns: dictionary of response from portal instance. """ # multipart requests pre-encode the parameters if request_type == 'MULTIPART': parameters = request_parameters else: parameters = {'f': 'json'} # if we haven't logged in yet, won't have a valid token if self.token: parameters['token'] = self.token if request_parameters: parameters.update(request_parameters) if request_type == 'GET': req = request('?'.join((in_url, encode(parameters)))) elif request_type == 'MULTIPART': req = request(in_url, parameters) elif request_type == 'WEBMAP': if files: req = request(in_url, *self.encode_multipart_data(parameters, files)) else: arcpy.AddWarning( "Multipart request made, but no files provided.") return else: req = request(in_url, encode(parameters).encode('UTF-8'), self.headers) if additional_headers: for key, value in list(additional_headers.items()): req.add_header(key, value) req.add_header('Accept-encoding', 'gzip') try: response = urlopen(req) except HTTPError as e: arcpy.AddWarning("{} {} -- {}".format(HTTP_ERROR_MSG, in_url, e.code)) return except URLError as e: arcpy.AddWarning("{} {} -- {}".format(URL_ERROR_MSG, in_url, e.reason)) return if response.info().get('Content-Encoding') == 'gzip': buf = BytesIO(response.read()) with gzip.GzipFile(fileobj=buf) as gzip_file: response_bytes = gzip_file.read() else: response_bytes = response.read() response_text = response_bytes.decode('UTF-8') # occasional timing conflicts; repeat until we get back a valid response. response_json = json.loads(response_text) # Check that data returned is not an error object if not response_json or "error" in response_json: rerun = False if repeat > 0: repeat -= 1 rerun = True # token has expired. Revalidate, then rerun request if response_json['error']['code'] is 498: if self.debug: arcpy.AddWarning("token invalid, retrying.") if self.login_method is 'token': # regenerate the token if we're logged in via the application self.token_login() else: self.login(self.username, self._password, repeat=0) # after regenerating token, we should have something long-lived if not self.token or self.valid_for < 5: arcpy.AddError("Unable to get signin token.") return rerun = True if rerun: time.sleep(2) response_json = self.url_request(in_url, request_parameters, request_type, additional_headers, files, repeat) return response_json