-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
201 lines (159 loc) · 6.88 KB
/
main.py
File metadata and controls
201 lines (159 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import requests
from bs4 import BeautifulSoup
from urllib.parse import quote
from datetime import date
currencies = {"USD":"US+Dollar",
"EUR":"Euro",
"GBP":"Pound+Sterling",
"CHF":"Swiss+Franc",
"JPY":"Japanese+Yen",
"SAR":"Saudi+Riyal",
"KWD":"Kuwaiti+Dinar",
"AED":"UAE+Dirham",
"CNY":"Chinese+yuan"}
def generate_payload(s: requests.Session, from_date, to_date, ccy) -> str:
"""
Generate the data needed to send to the API for CBE.
This function first uses the requests session provided to scrape the
necessary information from the website then uses the user's input to generate
the paylod
Args:
s (requests.Session):
the session that contains the cookies
from_date
the starting date in isoformat for the query
to_ date
the ending date in isoformat for the query
Returns:
str:
a string containing the payload needed
"""
URL = "https://www.cbe.org.eg/en/economic-research/statistics/cbe-exchange-rates/historical-data"
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0"}
try:
from_date = quote(date.fromisoformat(from_date).strftime("%d/%m/%Y"),safe="")
to_date = quote(date.fromisoformat(to_date).strftime("%d/%m/%Y"),safe="")
except TypeError:
from_date = quote(from_date.strftime("%d/%m/%Y"),safe="")
to_date = quote(to_date.strftime("%d/%m/%Y"),safe="")
r = s.get(URL, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
form = soup.find("form", id="historicalDataForm")
hidden_inputs = form.select(":scope > input[type=hidden]")
structured_response = {item.get("name"):item.get("value") for item in hidden_inputs}
ccy_payload = generate_ccy(ccy)
payload = (
f"__RequestVerificationToken={structured_response['__RequestVerificationToken']}&"
f"uid={structured_response['uid']}&"
f"DataSourceId={structured_response['DataSourceId']}&"
f"FallbackUrl={quote(structured_response['FallbackUrl'], safe='')}&"
f"LanguageName={structured_response['LanguageName']}&"
f"FromDateRaw={from_date}&"
f"ToDateRaw={to_date}&"
f"{ccy_payload}&"
"SubmitAction=1"
)
return payload
def generate_ccy(inputs:list) -> str:
"""
generate the text that includes the currencies needed for the payload
"""
input_text = ""
for input in inputs:
input_text += f"&SelectedSelectOptions={currencies[input]}"
for input in inputs:
input_text += f"&multiselect_multipleSelectID={currencies[input]}"
return input_text
def get_cookie_string(s: requests.Session, url: str) -> str:
"""
Use an existing session to fetch the given URL
and return cookies as a header-ready string.
"""
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
)
}
# Initial request with provided session
response = s.get(url, headers=headers)
# Convert dict → string
cookie_dict = s.cookies.get_dict()
cookie_str = "; ".join([f"{k}={v}" for k, v in cookie_dict.items()])
return cookie_str
def get_cbe_rate(from_date = date.today(),to_date = date.today(),ccy:list = ["USD"]):
"""
Returns a dict with the user's request to return the exchange rate from Central Bank of Egypt
Args:
from_date (datetime.date)
the beginning date of the inquiry
to_date (datetime.date)
the end date of the inquiry
ccy (list)
a list of currencies requested
returns
List:
a a list of dictionaries with keys date, currency, buy_rate and sell_rate
"""
s = requests.Session()
cookie_URL = "https://www.cbe.org.eg/en/economic-research/statistics/cbe-exchange-rates/historical-data"
cookies = get_cookie_string(s=s, url=cookie_URL)
api_url = "https://www.cbe.org.eg/api/statistics/GetHistoricalData"
payload = generate_payload(s=s, from_date=from_date,to_date=to_date, ccy=ccy)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://www.cbe.org.eg',
'Referer': 'https://www.cbe.org.eg/en/economic-research/statistics/cbe-exchange-rates/historical-data',
'Cookie': cookies
}
r = requests.post(api_url,headers=headers,data=payload)
if r.status_code == 200:
soup = BeautifulSoup(r.text, 'html.parser')
table = soup.find('table', class_='table-comp')
if table:
rows = table.find_all('tr', class_='content-height')[1:] # Skip the header row
result = []
for row in rows:
columns = row.find_all('td', class_='column-width table-cell')
date = columns[0].text.strip()
currency = columns[1].text.strip()
buy_rate = float(columns[2].text.strip())
sell_rate = float(columns[3].text.strip())
data = {
'date': date,
'currency': currency,
'buy_rate': buy_rate,
'sell_rate': sell_rate
}
result.append(data)
return result
print(f"Error: {r.status_code} - {r.text}")
return None
if __name__ == "__main__":
# Example Use Cases
# Running without any arguments returns the USD/EGP rate today
call = get_cbe_rate()
print(call)
# [{'date': '31/08/2025', 'currency': 'US Dollar', 'buy_rate': 48.5201, 'sell_rate': 48.6539}]
# Run with a specific date in mind and currencies
specific_date = date(2022,10,25)
requested_currencies = ["USD","KWD","EUR"]
call = get_cbe_rate(from_date=specific_date,
to_date=specific_date,
ccy=requested_currencies)
print(call)
# [{'date': '25/10/2022', 'currency': 'US Dollar', 'buy_rate': 19.6409, 'sell_rate': 19.7478},
# {'date': '25/10/2022', 'currency': 'Euro', 'buy_rate': 19.3738, 'sell_rate': 19.4872},
# {'date': '25/10/2022', 'currency': 'Kuwaiti Dinar', 'buy_rate': 63.2883, 'sell_rate': 63.6534}]
# Run with a range from/to date and currencies
from_date = date(2022,10,25)
to_date = date(2022,10,28)
requested_currencies = ["USD","EUR"]
call = get_cbe_rate(from_date=specific_date,
to_date=specific_date,
ccy=requested_currencies)
print(call)
# [{'date': '25/10/2022', 'currency': 'US Dollar', 'buy_rate': 19.6409, 'sell_rate': 19.7478},
# {'date': '25/10/2022', 'currency': 'Euro', 'buy_rate': 19.3738, 'sell_rate': 19.4872}]