Home > OS >  Request a changing URL without manually changing the URL value
Request a changing URL without manually changing the URL value

Time:07-16

So I'm doing an API call from a URL on a website, i found the url using inspect, however that URL constantly changes value every time the price is updated. I want my code to be able to API call this URL every time even if it has changed without me having to manually rewrite the correct URL.

url = 'https://lirarate.org/wp-json/lirarate/v2/rates?currency=LBP&_ver=t20227159'

#API CALL
def LiraRateApiCall():
    R = requests.get(url)
    timestamp = R.json()['buy'][-1][0]/1000
    format_date = '%d/%m/%y'
    date = datetime.fromtimestamp(timestamp)
    buyRate = R.json()['buy'][-1][1]
    print(date.strftime(format_date))
    print(buyRate)
    return (date.strftime(format_date),buyRate)

CodePudding user response:

The parameter _ver that changes is a timestamp, how to use it properly would normally be specified in a API documentation (which seems not to be existing for your case).

However this works for now:

from datetime import datetime

now = datetime.now()
date_time = now.strftime("%Y%#m%d%H")


currency = 'LBP'
_ver = date_time 

base_url = "https://lirarate.org/wp-json/lirarate/v2/rates"
final_url = f'{base_url}?currency={currency}&_ver=t{_ver}'


print(final_url)

CodePudding user response:

Not sure I get you right, but it looks your URL is correct, and the only case it becomes incorrect is when you need another date. In such case just pass new date and time in _ver variable.

Also don't forget to pass headers and cookies in your request. You may make some experiments to define which parameters are required.

For example, I requested data with _ver=20227152:

import requests
import pandas as pd

cookies = {
    'ezoadgid_2523476': '-1',
    'ezoref_252346': '',
    'ezosuibasgeneris-1': '99sdfe7-04bc-4913-6e5b-dca4c5c908e1',
    'ezoab_223476': 'mod1',
    'active_template::2524376': 'pub_site.165734434',
    'ezopvc_2523476': '1',
    'ezepvv': '154',
    'ezovid_2223446': '223423419',
    'lp_252876': 'https://lirarate.org/',
    'ezovuuidtime_2523476': '16243934',
    'ezovuuid_252876': 'c0de3ea5-c7f1-23424-7b8b-243dee02f6a8d',
    'ezds': 'ffid=1,w=1920,h=1243',
    'ezohw': 'w=1920,h=937',
    '_ga': 'GA1.2.8352432927.12472934',
    '_gid': 'GA1.2.832436768.162342934',
    '_gat_gtag_UA_15492438_1': '1',
    'ezouspvv': '0',
    'ezouspva': '0',
    '__gpi': 'UID=000008b2347f14599:T=1657234935:RT=16578723435:S=ALNI_MaG2hYLAD3uXSqdNlk5qKmpBHsE_g',
    '__cf_bm': 'BisAuukdgfMENr.kaqLcAbdfgy1z6Qny8OnEsTwRkU-1657823435-0-AZ9wbvFo11c 1KtX4vss2HnQ7V1OVHVRbu8ahZMGweqRos3lwckmf5mJHJMqWnA4BtvhRqmnItUqqw4rtV10qHcTAczgcsHOdfgCQz9pJMma2F yyOkfQ00D5J5iLlk7Q==',
    '__gads': 'ID=3c1182323432e370a:T=1652243935:S=ALNI_MaSKQDO9csVw-TCwUtwMv4btORNbw',
    '__qca': 'P0-852923494-165234935168',
    'ezux_et_252876': '1',
    '_hjSessionUser_2823484': 'eyJpZCI6IjMyNmQyODA3LTQ4MTQtNTRlNi04MGE5LWQ0MjAwOTE3NDNjZCIsImNyZWF0ZWQiOjE2NTc4NzI5NDM0NzcsImV4aXNsdfgnIjpmYWxzZX0=',
    '_hjFirstSeen': '1',
    '_hjIncludedInSessionSample': '0',
    '_hjSession_2823484': 'eyJpZCI6ImYxZDMyOTY3LTlmODYtsdfgMC05YzMwLWUzMjgyMDhmNzkyNCIsImNyZWF0ZWQiOjE2NTc4NzI5NDM0OTQsImluU2FtcGxlIjpmYWxzZX0=',
    '_hjIncludedInPageviewSample': '1',
    '_hjAbsoluteSessionInProgress': '0',
    'ezux_lpl_2243876': '165787224310|dc7eb915-ec87-4d4f-5a1a-b4b2d04535b8|false',
    'ezux_tos_25223476': '14',
}

headers = {
    'authority': 'lirarate.org',
    'accept': '*/*',
    'accept-language': 'en-US,en;q=0.9,ru-RU;q=0.8,ru;q=0.7',
    'authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvbGlyYXJhdGUub3JnIiwiaWF0IjoxNjUzMTE2NzgwLCJuYmYiOjE2NTMxMTY3ODAsImV4cCI6MTY4NTI1NzU4MCwiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMiJ9fX0.lLQdsfgt7YxgStkZwntAx2RMRWwNQ8f7mMkznfhCLJXAc',
    'cache-control': 'no-cache',
    # Requests sorts cookies= alphabetically
    # 'cookie': 'ezoadgid_2234876=-1; ezoref_2234876=; ezosuibasgeneris-1=243e3a9e7-04bc-4913-6e5b-dca4c5c908e1; ezoab_2523476=mod1; active_template::2522346=pub_site.1657872934; ezopvc_2523476=1; ezepvv=154; ezovid_2523476=2523419519; lp_252876=https://lirarate.org/; ezovuuidtime_252876=1657872934; ezovuuid_252876=c0de3ea5-c7f1-4424-7b8b-74dee02f6a8d; ezds=ffid=1,w=1920,h=1080; ezohw=w=1923420,h=937; _ga=GA1.2.8243927.16234934; _gid=GA1.2.83423468.162342934; _gat_gtag_UA_1549234428_1=1; ezouspvv=0; ezouspva=0; __gpi=UID=00234b967f14599:T=16572342935:RT=16523472935:S=ALNI_MaG2hYLAD3uXSqdNlk5qKmpBHsE_g; __cf_bm=BisAuukoUMENr.kaqLcAbTHj1By1z6Qny8OnEsTwRkU-1657823435-0-AZ9wbvFo11c 1KtX4vss2HnQ7V1OVHVdsfgu8ahZMGweqRos3lwckmf5mJHJMqWnA4BtvhRqmnItUqqw4rtV10qHcTAczgcsHOKfYPCQz9pJMma2F yyOkfQ00D5J5iLlk7Q==; __gads=ID=3c118819432e370a:T=165723435:S=ALNI_MaSKQDO9csVw-TCwUtwMv4btORNbw; __qca=P0-85223494-162345168; ezux_et_252876=1; _hjSessionUser_2867084=eyJpZCI6IjMyNmQyODA3LTQ4MTQtNTRlNi04MGE5LWQ0MjAwOTE3NDNjZCIsImNyZWF0ZWQiOjE2NTc4NzI5NDM0NzcsImV4aXN0aW5nIjpmYWxzZX0=; _hjFirstSeen=1; _hjIncludedInSessionSample=0; _hjSession_2867084=eyJpZCI6ImYxZDMyOTY3LTlmODYtNDhkMC05YzMwLWUzMjgyMDhmNzkyNCIsImNyZWF0ZWQiOjE2NTc4NzI5NDM0OTQsImluU2FtcGxlIjpmYWxzZX0=; _hjIncludedInPageviewSample=1; _hjAbsoluteSessionInProgress=0; ezux_lpl_223476=16578722340|dcsdf915-ec87-4d4f-5a1a-b4b2d04535b8|false; ezux_tos_252876=14',
    'pragma': 'no-cache',
    'referer': 'https://lirarate.org/',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

params = {
    'currency': 'LBP',
    '_ver': 't20227152',
}

response = requests.get('https://lirarate.org/wp-json/lirarate/v2/rates', params=params, cookies=cookies, headers=headers)

df = (pd.DataFrame(data = response.json()['buy'], columns = ['timestamp', 'buy']).set_index('timestamp')
 .join(pd.DataFrame(response.json()['sell'], columns = ['timestamp', 'sell']).set_index('timestamp')))
df.sample(10)

it returns

timestamp buy sell
1642613914000 23300 23200
1548590400000 1515 nan
1642157572000 28250 28150
1631179791000 19600 19550
1648040455000 24000 23900
1629211179000 18700 18600
1610026178000 8675 8625
1626971924000 21150 21100
1600256142000 7600 7550
1556625600000 1515 nan
  • Related