forked from lakshay761/hdf5-to-csv-MSD-python-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyricstomsd.py
89 lines (49 loc) · 2.33 KB
/
lyricstomsd.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
# coding: utf-8
# In[69]:
import pandas as pd
import numpy as np
import csv
import re
import urllib,json
from musixmatch import Musixmatch
musixmatch=Musixmatch('e5e7719b47f96c02f10ecc01d2025414') #Musixmatch authentication
# In[70]:
df = pd.read_csv('SongCSV.csv') #DataFrame
df.head()
songNameList = df['Title'].values.tolist() #Creating a list of the songName column
artistNameList = df['ArtistName'].values.tolist() #Creating a list of the artistName column
albumNameList = df['AlbumName'].values.tolist() #Creating a list of the albumName column
# In[49]:
songNameList[0] = re.sub(r'\([^)]*\)', '', songNameList[0]) #Removes anything that's in the parenthesis of song name. for example Mr. Brightside (Remix). The bracket part gets removed.
songNameList[0]
# In[68]:
lyricsList = []
# In[39]:
#Trying out how the loop would work for one song lyrics
# lyrics = musixmatch.matcher_lyrics_get(songNameList[0], artistNameList[0])
# print(lyrics)
# lyricsList.append(lyrics['message']['body']['lyrics']['lyrics_copyright'])
# lyricsList
# In[72]:
for i in range(0, 10000):
songNameList[i] = re.sub(r'\([^)]*\)', '', songNameList[i]) ##Removes anything that's in the parenthesis of song name. for example Mr. Brightside (Remix). The bracket part gets removed.
if(songNameList[i].startswith('b')): #Some song names have the letter 'b' in the starting of their names. This removes that.
songNameList[i] = songNameList[i][1:]
print(f'Song Name: {songNameList[i]} \nArtist Name: {artistNameList[i]}')
lyricsQ = musixmatch.matcher_lyrics_get(songNameList[i], artistNameList[i])
if(lyricsQ['message']['header']['status_code'] == 200): #status code 200 means successful query. And we will append the result to the end of our lyricsList
lyricsList.append(lyricsQ['message']['body']['lyrics']['lyrics_body'])
else: #In case of an unsuccessful query, append empty string
lyricsList.append('')
print('Lyrics not found!')
print(lyricsList)
# In[77]:
print(len(lyricsList)) #Tells us how many lyrics have we gotten.
# In[81]:
csv_input = pd.read_csv('SongCSV.csv') #Reads csv
csv_input['Lyrics']=lyricsList #Creates a new
csv_input.to_csv('output.csv') #New output file
# In[78]:
csv_input = pd.read_csv('SongCSV.csv')
csv_input['Lyrics'] = lyricsList
csv_input.to_csv('output.csv')