JayosChaos commited on
Commit
05cfef5
·
verified ·
1 Parent(s): 897529c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -40,34 +40,36 @@ def get_billboard_hot_100_last_countdown() -> list:
40
  from bs4 import BeautifulSoup
41
 
42
  url = "https://www.billboard.com/charts/hot-100/"
43
- response = requests.get(url)
 
 
 
 
44
 
45
- if response.status_code == 200:
46
- soup = BeautifulSoup(response.text, 'html.parser')
47
-
48
- top_songs = []
49
- chart_items = soup.select("li.o-chart-results-list__item")
 
 
 
50
 
51
- for item in chart_items[:10]:
52
- song_name_elem = item.select_one("h3")
53
- artist_name_elem = item.select("span.c-label")
54
 
55
- if song_name_elem and artist_name_elem:
56
- song_name = song_name_elem.get_text(strip=True)
57
-
58
- # The artist name is usually the second span.c-label element in Billboard's structure
59
- artist_name = None
60
- for span in artist_name_elem:
61
- if not span.get_text(strip=True).isdigit(): # Avoid selecting ranking numbers
62
- artist_name = span.get_text(strip=True)
63
- break
64
-
65
- if artist_name:
66
- top_songs.append({"song": song_name, "artist": artist_name})
67
-
68
- return top_songs
69
- else:
70
- raise ValueError(f"Failed to fetch Billboard Hot 100. HTTP Status: {response.status_code}")
71
 
72
 
73
 
 
40
  from bs4 import BeautifulSoup
41
 
42
  url = "https://www.billboard.com/charts/hot-100/"
43
+ try:
44
+ response = requests.get(url)
45
+ response.raise_for_status() # Raise an error for failed request
46
+ except requests.exceptions.RequestException as e:
47
+ raise ValueError(f"Error connecting to Billboard Hot 100: {e}")
48
 
49
+ soup = BeautifulSoup(response.text, 'html.parser')
50
+
51
+ top_songs = []
52
+ chart_items = soup.select("li.o-chart-results-list__item")
53
+
54
+ for item in chart_items[:10]:
55
+ song_name_elem = item.select_one("h3")
56
+ artist_name_elem = item.select("span.c-label")
57
 
58
+ if song_name_elem and artist_name_elem:
59
+ song_name = song_name_elem.get_text(strip=True)
 
60
 
61
+ # The artist name is usually the second span.c-label element in Billboard's structure
62
+ artist_name = None
63
+ for span in artist_name_elem:
64
+ if not span.get_text(strip=True).isdigit(): # Avoid selecting ranking numbers
65
+ artist_name = span.get_text(strip=True)
66
+ break
67
+
68
+ if artist_name:
69
+ top_songs.append({"song": song_name, "artist": artist_name})
70
+
71
+ return top_songs
72
+
 
 
 
 
73
 
74
 
75