-
Notifications
You must be signed in to change notification settings - Fork 1
/
top_chart.py
148 lines (113 loc) · 4.26 KB
/
top_chart.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
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
import time
import requests
from lxml import html
from constants import TRACK_IMAGE_PLACEHOLDER, ARTIST_IMAGE_PLACEHOLDER
'''
**Broken Pipe error when refreshed continuously**
Error due to Unix SIGPIPE signal , happens when utilities receive enough
data , so the head blocks further connections
Can be seen using
import sys
using sys.stdout.write instead of print statement
Fix : Switch from flask server ,to production ready server
OR : Temp fix - flush the sigpipe repeatedly
But shouldn't be an issue when hosted on cloud engines.
'''
HEADER = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0'
}
MAX_RETRIES = 5
def get_page(url, message, xpaths):
try:
print(message)
page = requests.get(url)
tree = html.fromstring(page.text)
result_array = []
for path in xpaths:
result_array.append(tree.xpath(path))
return result_array
except (requests.ConnectionError, requests.ConnectTimeout) as error_message:
print(error_message)
return []
def top_chart():
url = 'http://www.billboard.com/charts/hot-100'
xpaths = ['//div/h2[@class="chart-row__song"]/text()',
'//div/a[@class="chart-row__artist"]/text()']
xpath_content = get_page(url, 'Getting Top 100 Tracks', xpaths)
try:
results = []
song_title = xpath_content[0]
song_artist = xpath_content[1]
for i in range(len(song_artist)):
data_set = {
'track_name': song_title[i].strip().title(),
'artist_name': song_artist[i].strip().title(),
'image': TRACK_IMAGE_PLACEHOLDER
}
results.append(data_set)
return results
except IndexError:
return None
def trending_chart():
url = 'http://www.billboard.com/charts/radio-songs'
xpaths = ['//div/h2[@class="chart-row__song"]/text()', '//div/a[@class="chart-row__artist"]/text()',
'//div[@class="chart-row__image"]/@style']
xpath_content = get_page(url, 'Getting Trending Songs', xpaths)
try:
song_title = xpath_content[0]
song_artist = xpath_content[1]
artist_image = xpath_content[2]
results = []
for i in range(len(song_artist)):
data_set = {
'track_name': song_title[i].strip().title(),
'artist_name': song_artist[i].strip().title(),
'artist_image': artist_image[i].strip()[22: artist_image[i].find(')')] if i < len(
artist_image) else ARTIST_IMAGE_PLACEHOLDER
}
results.append(data_set)
return results
except IndexError:
return None
def emerging_artist():
url = 'http://www.billboard.com/charts/emerging-artists'
xpaths = ['//div/a[@class="chart-row__artist"]/text()',
'//div[@class="chart-row__image"]/@style']
xpath_content = get_page(url, 'Getting Emerging Artists', xpaths)
try:
song_artist = xpath_content[0]
artist_image = xpath_content[1]
results = []
for i in range(len(song_artist)):
data_set = {
'artist_name': song_artist[i].strip().title(),
'artist_image': artist_image[i].strip()[22: artist_image[i].find(')')] if i < len(
artist_image) else ARTIST_IMAGE_PLACEHOLDER
}
results.append(data_set)
return results
except IndexError:
return None
def top_artist():
url = 'http://www.billboard.com/charts/artist-100'
xpaths = ['//div[@class="chart-row__title"]/a/text()',
'//div[@class="chart-row__image"]/@style']
xpath_content = get_page(url, 'Getting Top Artists', xpaths)
try:
artist_name = xpath_content[0]
artist_image = xpath_content[1]
results = []
for i in range(0, len(artist_image)):
current_image = artist_image[i][22: artist_image[i].find(')')]
current_name = artist_name[i].strip()
data_set = {
'artist_name': current_name,
'image': current_image
}
results.append(data_set)
return results
except IndexError:
return None
if __name__ == '__main__':
DATA = trending_chart()
print(DATA)