Skip to content

Commit d274888

Browse files
committed
added error handling for idonate
1 parent 0a5f256 commit d274888

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

mps_site/scripts.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,26 @@ def get_most_popular_video_ids(channel_url, n=9):
182182
def get_donation_count_fm():
183183
URL = "https://www.idonate.ie/fundraiser/MediaProductionSociety12"
184184
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"}
185-
r = requests.get(url=URL, headers=headers)
186-
soup = BeautifulSoup(r.content, 'html5lib')
187-
current_donation = soup.find('div', attrs = {'class':'ifs-right-fundraisers-head'})
188-
donation_target = soup.find('div', attrs = {'class':'support-cause'})
189-
current_donation_amount = int(str(current_donation).split()[3].split("€")[1].split("<")[0].replace(",",""))
190-
goal_amount = int(str(donation_target).split("€")[1].split("<")[0].replace(",",""))
191-
return current_donation_amount, goal_amount
185+
186+
try:
187+
r = requests.get(url=URL, headers=headers, timeout=10)
188+
r.raise_for_status()
189+
190+
soup = BeautifulSoup(r.content, 'html5lib')
191+
192+
current_donation = soup.find('div', attrs={'class': 'ifs-right-fundraisers-head'})
193+
donation_target = soup.find('div', attrs={'class': 'support-cause'})
194+
195+
if current_donation is None or donation_target is None:
196+
raise ValueError("Necessary data not found on the page.")
197+
198+
current_donation_amount = int(str(current_donation).split()[3].split("€")[1].split("<")[0].replace(",", ""))
199+
goal_amount = int(str(donation_target).split("€")[1].split("<")[0].replace(",", ""))
200+
201+
return current_donation_amount, goal_amount
202+
203+
except (requests.RequestException, ValueError, IndexError, AttributeError) as e:
204+
return 0, 0
192205

193206
def get_event_data():
194207
data = {}

0 commit comments

Comments
 (0)