-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (67 loc) · 3.45 KB
/
main.py
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
from bs4 import BeautifulSoup
import requests
def get_billboard_hot_100():
""" Gets the data from the Billboard Hot 100 website, and then returns an object with all the relevant
information """
url = "https://www.billboard.com/charts/hot-100/"
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
soup.find("div", class_="lxml")
# stripping punctuation so spotify doesn't pack a sad. may need to add an if clause later so that I can keep
# punctuation for display on web page
song_list = [strip_punctuation(result.text.strip()) for result in soup.select(
"div.chart-results-list > div.o-chart-results-list-row-container > ul.o-chart-results-list-row > "
"li:nth-child(4) > ul > li:nth-child(1) h3")]
artist_list = [strip_punctuation(result.text.strip()) for result in soup.select(
"div.chart-results-list > div.o-chart-results-list-row-container> ul.o-chart-results-list-row > li:nth-child("
"4) > ul > li:nth-child(1) span")]
position_list = [i for i in range(1, 100 + 1)]
chart_data = [{'Position': positions, 'Artist': artists, 'Track': songs} for positions, artists, songs in
zip(position_list, artist_list, song_list)]
return chart_data
def get_NZ_top_40():
""" Gets the data from the NZTop40 website, and then returns an object with all the relevant
information """
url = "https://nztop40.co.nz/chart/singles"
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
song_list_raw = soup.findAll("h2", {"class": "title"})
song_list = []
for element in song_list_raw:
song_list.append(strip_punctuation(element.string))
artist_list_raw = soup.findAll("h3", {"class": "artist"})
artist_list = []
for element in artist_list_raw:
artist_list.append(strip_punctuation(element.string))
position_list = [i for i in range(1, 100 + 1)]
chart_data = [{'Position': positions, 'Artist': artists, 'Track': songs} for positions, artists, songs in
zip(position_list, artist_list, song_list)]
return chart_data
def get_aria_top_50():
""" Gets the data from the ARIA top 50 singles website, and then returns an object with all the relevant
information """
url = "https://www.aria.com.au/charts/singles-chart"
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
song_list_raw = soup.findAll("a", {"class": "c-chart-item__title"})
song_list = []
for element in song_list_raw:
song_list.append(strip_punctuation(element.string))
artist_list_raw = soup.findAll("a", {"class": "c-chart-item__artist"})
artist_list = []
for element in artist_list_raw:
artist_list.append(strip_punctuation(element.string))
position_list = [i for i in range(1, 100 + 1)]
chart_data = [{'Position': positions, 'Artist': artists, 'Track': songs} for positions, artists, songs in
zip(position_list, artist_list, song_list)]
return chart_data
def strip_punctuation(input_str):
""" Strips text of its punctuation as Spotify does not recognise it when searching """
punctuation = '''{};:'"\,<>/@#$%^&*_~'''
for element in input_str:
if element in punctuation:
if element == "&":
input_str = input_str.replace(element, "and")
else:
input_str = input_str.replace(element, "")
return input_str