-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_matches_gaana.py
342 lines (291 loc) · 11.8 KB
/
find_matches_gaana.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/python
from .helpers import *
session = create_session()
headers_gaana_path = 'creds_headers/headers_gaana.json'
with open(headers_gaana_path) as json_file:
headers_gaana = json.load(json_file)
def find_matches_gaana(albums_df, csv_name, min_matched, min_checked, min_combos):
albums_df_copy = albums_df.copy(deep=True)
for index,row in albums_df_copy.iterrows():
title = row['title']
artist = row['artist']
genre = row['genre']
csv_index = str(index).zfill(4)
keywords = artist + ' ' + title
time_now = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
print(time_now + '|' + 'gaana|' + csv_index + '|' + keywords)
string_combos = create_list_queries(title,artist)
albums_checked = set()
albums_matched = set()
for combo in string_combos[0:min_combos]:
if ((len(albums_checked) >= min_checked) or
(len(albums_matched) >= min_matched)):
break
query = ' '.join(combo)
query_encoded = urllib.parse.quote(query)
referer = 'https://gaana.com/search/albums/' + query_encoded
headers_gaana.update({"Referer": referer})
urlx = 'https://gaana.com/apiv2?country=IN&keyword=' +\
query_encoded + '&page=0&secType=album&type=search'
time_sleep(16,20)
try:
response = session.post(url=urlx,headers=headers_gaana)
results_dict = response.json()
# below is 'got response, but no results' condition
albums_list = results_dict['gr'][0]['gd']
except:
print('redo search|' + 'gaana|' + csv_index + '|' + keywords)
time_sleep(150,180)
continue
for album in albums_list:
if ((len(albums_checked) >= min_checked) or
(len(albums_matched) >= min_matched)):
break
search_title = album['ti']
search_title_url = 'https://gaana.com/album/' + album['seo']
if search_title_url in albums_checked: continue
albums_checked.add(search_title_url)
level_of_title_match = max(
get_elkan_score_jarowink(title, search_title),
get_elkan_score_jarowink(search_title, title))
# try:
# title_threshold =\
# 100*((len(tokenize(title))-1)/len(tokenize(title)))
# except:
# title_threshold = 85
title_threshold = 85
if level_of_title_match < title_threshold: continue
time_sleep(12,18)
# visiting webpage of album to get artist name
try:
album_page = session.get(search_title_url,
headers = headers_gaana)
except:
print('redo search|' + 'gaana|' + csv_index + '|' + keywords)
time_sleep(12,18)
continue
treex = lxml_html.fromstring(album_page.content)
album_active_list = treex.xpath("//li[contains(text(),'Album is inactive')]")
album_active_list_2 = treex.xpath("//li[contains(text(),'Sorry, this content is not available')]")
if (len(album_active_list) > 0) or (len(album_active_list_2) > 0):
continue
search_artist_list = [artist.xpath('@title')[0] for
artist in treex.xpath("//ul[@class='singers']/li/a")]
try:
search_artist = ', '.join(search_artist_list)
# use condition below if you just want first artist listed
# if genre == 'Western Classical':
# search_artist = ', '.join(search_artist_list)
# else:
# search_artist = search_artist_list[0]
except:
print('redo search|' + 'gaana|' + csv_index + '|' + keywords)
continue
track_list = treex.xpath("//ul[contains(@class,'list_data')]")
if ((genre not in ['Blues','Jazz','Western Classical',
'World Music']) and (len(track_list) <= 1)):
continue
level_of_artist_match = max(
get_elkan_score_jarowink(artist, search_artist),
get_elkan_score_jarowink(search_artist, artist))
# try:
# artist_threshold =\
# 100*((len(tokenize(artist))-1)/len(tokenize(artist)))
# except:
# artist_threshold = 85
artist_threshold = 85
if 'various' not in artist.lower():
if level_of_artist_match < artist_threshold:
continue
if search_title_url in albums_matched: continue
albums_matched.add(search_title_url)
suffix = '_' + str(len(albums_matched))
albums_df_copy.loc[index,'match_artist_gaana' + suffix] = search_artist
albums_df_copy.loc[index,'match_title_gaana' + suffix] = search_title
albums_df_copy.loc[index,'match_url_gaana' + suffix] = search_title_url
save_path = 'data/' + csv_name
albums_df_copy.to_csv(save_path,
encoding='utf-8')
def playable_on_gaana(album_url):
album_page = session.get(url=album_url, headers=headers_gaana)
treex = lxml_html.fromstring(album_page.content)
album_active_list = treex.xpath("//li[contains(text(),'Album is inactive')]")
album_active_list_2 = treex.xpath("//li[contains(text(),'Sorry, this content is not available')]")
if (len(album_active_list) > 0) or (len(album_active_list_2) > 0):
return False
total_duration_string =\
treex.xpath("//span[contains(@class,'_duration')]/text()")[0]
total_duration = 0
if 'hr' in total_duration_string:
num_hours_string = re.search(r"(\d+)\shr",
total_duration_string).group(1)
num_seconds = int(num_hours_string) * 60 * 60
total_duration += num_seconds
if 'min' in total_duration_string:
num_minutes_string = re.search(r"(\d+)\smin",
total_duration_string).group(1)
num_seconds_2 = int(num_minutes_string) * 60
total_duration += num_seconds_2
if 'sec' in total_duration_string:
num_seconds_string = re.search(r"(\d+)\ssec",
total_duration_string).group(1)
num_seconds_3 = int(num_seconds_string)
total_duration += num_seconds_3
# print(total_duration)
item_list = treex.\
xpath("//section[contains(@class,'song-list')]//ul[@class='_row list_data']")
# print('number of songs in list:' + str(len(item_list)))
# total_duration = 0
playable_duration = 0
for item in item_list:
item_duration_string =\
item.xpath("./descendant::li[contains(@class,'_dur')][1]/span/text()")[0]
# print(item_duration_string)
item_duration_parts = item_duration_string.split(':')
item_duration_seconds = int(item_duration_parts[-1])
try:
item_duration_minutes = int(item_duration_parts[-2])
item_duration_seconds_2 = item_duration_minutes * 60
except:
item_duration_seconds_2 = 0
try:
item_duration_hours = int(item_duration_parts[-3])
item_duration_seconds_3 = item_duration_hours * 60 * 60
except:
item_duration_seconds_3 = 0
item_duration = item_duration_seconds +\
item_duration_seconds_2 +\
item_duration_seconds_3
# total_duration += item_duration
play_button_list = item.xpath("./descendant::li[@title='Play'][1]")
if not play_button_list: continue
#above line says if there's no play button svg element for song, move on
#if it's an empty list, empty list is falsy value, so NOT False == True
playable_duration += item_duration
# print(total_duration)
# print(playable_duration)
if playable_duration/total_duration >= 0.95:
return True
else:
return False
def playable_on_gaana_details(album_url):
album_page = session.get(url=album_url, headers=headers_gaana)
treex = lxml_html.fromstring(album_page.content)
album_active_list = treex.xpath("//li[contains(text(),'Album is inactive')]")
if len(album_active_list) != 0:
return 'album inactive'
total_duration_string =\
treex.xpath("//span[contains(@class,'_duration')]/text()")[0]
total_duration = 0
if 'hr' in total_duration_string:
num_hours_string = re.search(r"(\d+)\shr",
total_duration_string).group(1)
num_seconds = int(num_hours_string) * 60 * 60
total_duration += num_seconds
if 'min' in total_duration_string:
num_minutes_string = re.search(r"(\d+)\smin",
total_duration_string).group(1)
num_seconds_2 = int(num_minutes_string) * 60
total_duration += num_seconds_2
if 'sec' in total_duration_string:
num_seconds_string = re.search(r"(\d+)\ssec",
total_duration_string).group(1)
num_seconds_3 = int(num_seconds_string)
total_duration += num_seconds_3
# print(total_duration)
item_list = treex.\
xpath("//section[contains(@class,'song-list')]//ul[@class='_row list_data']")
# print('number of songs in list:' + str(len(item_list)))
# total_duration = 0
playable_duration = 0
for item in item_list:
item_duration_string =\
item.xpath("./descendant::li[contains(@class,'_dur')][1]/span/text()")[0]
# print(item_duration_string)
item_duration_parts = item_duration_string.split(':')
item_duration_seconds = int(item_duration_parts[-1])
try:
item_duration_minutes = int(item_duration_parts[-2])
item_duration_seconds_2 = item_duration_minutes * 60
except:
item_duration_seconds_2 = 0
try:
item_duration_hours = int(item_duration_parts[-3])
item_duration_seconds_3 = item_duration_hours * 60 * 60
except:
item_duration_seconds_3 = 0
item_duration = item_duration_seconds +\
item_duration_seconds_2 +\
item_duration_seconds_3
# total_duration += item_duration
play_button_list = item.xpath("./descendant::li[@title='Play'][1]")
if not play_button_list: continue
#above line says if there's no play button svg element for song, move on
#if it's an empty list, empty list is falsy value, so NOT False == True
playable_duration += item_duration
# print(total_duration)
# print(playable_duration)
if playable_duration/total_duration >= 0.95:
return 'duration ok'
else:
return 'duration not ok'
def get_artist_title_gaana(album_url, genre):
album_page = session.get(url=album_url, headers=headers_gaana)
treex = lxml_html.fromstring(album_page.content)
artist_list = [artist.xpath('@title')[0] for
artist in treex.xpath("//ul[@class='singers']/li/a")]
if genre == 'Western Classical':
artist = ', '.join(artist_list)
else:
artist = artist_list[0]
title = treex.xpath("//h2[@class='title']/text()")[0]
return [artist, title]
def get_label_copyright_gaana(album_url):
album_page = session.get(url=album_url, headers=headers_gaana)
treex = lxml_html.fromstring(album_page.content)
label = ''
phono_copyright_raw = treex.xpath("//div[@class='copyright']/text()")
phono_copyright_raw = ' '.join(phono_copyright_raw)
phono_copyright = phono_copyright_raw.replace('©','').strip()
listx = [label, phono_copyright]
return listx
def get_activity_tracks_gaana(album_url):
album_page = session.get(url=album_url, headers=headers_gaana)
treex = soup_fromstring(album_page.content)
album_active_list_1 = treex.xpath("//li[contains(text(),'Album is inactive')]")
album_active_list_2 = treex.xpath("//li[contains(text(),'No tracks found in this album')]")
album_active_list_3 = treex.xpath("//li[contains(text(),'Sorry, this content is not available')]")
if ((len(album_active_list_1) > 0) or (len(album_active_list_2) > 0) or (len(album_active_list_3) > 0)):
inactive_dict = {
'activity_status': 'inactive without a doubt',
'tracks_count': 0,
'tracks_count_playable': 0,
}
return inactive_dict
tracks_count_string =\
treex.xpath("//span[@class='_date' and contains(text(),'Track')]/text()")[0]
tracks_count_raw = re.search(r"(\d+)\sTrack",
tracks_count_string).group(1)
tracks_count = int(tracks_count_raw)
tracks_count_playable = 0
item_list = treex.\
xpath("//section[contains(@class,'song-list')]//ul[@class='_row list_data']")
for item in item_list:
play_button_list = item.xpath("./descendant::li[@title='Play'][1]")
if not play_button_list: continue
#above line says if there's no play button svg element for song, move on
#if it's an empty list, empty list is falsy value, so NOT False == True
tracks_count_playable += 1
threshold = 0.8*tracks_count
if tracks_count_playable >= tracks_count:
activity_status = 'possibly active'
elif tracks_count_playable >= threshold:
activity_status = 'possibly inactive: but over internal threshold'
elif tracks_count_playable < threshold:
activity_status = 'possibly inactive: below internal threshold'
activity_tracks_dict = {
'activity_status': activity_status,
'tracks_count': tracks_count,
'tracks_count_playable': tracks_count_playable
}
return activity_tracks_dict