forked from Ares358/Twitter_LyricBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
musixmatch_api_cleaner.py
135 lines (110 loc) · 4.77 KB
/
musixmatch_api_cleaner.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
import requests
import json
import random
import dotenv
import os
dotenv.load_dotenv()
# musixmatch api base url
base_url = "https://api.musixmatch.com/ws/1.1/"
# your api key
api_key = os.getenv('MUSIXMATCH_api_key')
sp_chars= [".","'","/","?"]
# api methods
lyrics_matcher = "matcher.lyrics.get"
track_matcher = "matcher.track.get"
# format url
format_url = "?format=json&callback=callback"
# parameters
artist_search_parameter = "&q_artist="
track_search_parameter = "&q_track="
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
def getLine(list):
no = (list.count('\n'))
no = int(no)
n=random.randint(1,no-1)
start = find_nth(list,'\n',n)
end = find_nth(list,'\n',n+1)
line = list[start:end].replace('\n','')
return line
def get_track_artist(track,artist):
api_call = base_url+track_matcher+format_url+track_search_parameter+track+artist_search_parameter+artist
request = requests.get(api_call+api_key)
data = request.json()
track = data['message']['body']['track']['track_name']
artist = data['message']['body']['track']['artist_name']
for i in sp_chars:
track=track.replace(i,"")
artist=artist.replace(i,"")
track=track.replace('&','And')
artist=artist.replace('&','And')
msg='#'+track.replace(' ','')+' by #'+artist.replace(' ','')
return msg
def lyric_matcher(track,artist,n):
api_call = base_url+lyrics_matcher+format_url+track_search_parameter+track+artist_search_parameter+artist
request = requests.get(api_call+api_key)
data = request.json()
if (data['message']['header']['status_code']==404):
lyrics='Lyric not found!!!\n'
return lyrics
else:
lyrics='\n\n'+data['message']['body']['lyrics']['lyrics_body']
lyrics=lyrics.replace('...','')
lyrics.replace('\n\n\n','\n\n')
lyrics=lyrics.replace("******* This Lyrics is NOT for Commercial use *******","")
#return lyrics
return snip(lyrics,n)
def snip(lyrics,n):
no=lyrics.count('\n\n')
flag=0
i=0
while(flag!=1):
flag=1
n=random.randint(1,no)
start = find_nth(lyrics,'\n\n',n)
end = find_nth(lyrics,'\n\n',n+1)
snippet = lyrics[start:end].replace('\n\n','')
if(i>=no):
if len(snippet)+n > 240:
end = start+ find_nth(lyrics[start:],'\n',6)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet)+n > 240:
end = start+ find_nth(lyrics[start:],'\n',5)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet)+n > 240:
end = start+ find_nth(lyrics[start:],'\n',4)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet)+n > 240:
end = start+ find_nth(lyrics[start:],'\n',3)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet)+n > 240:
end = start+ find_nth(lyrics[start:],'\n',2)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet)+n > 240:
snippet = 'Lyric too long to print. Consider requesting another song!!!\nPeace out puny Human!'
elif len(snippet) < 70:
end = start+ find_nth(lyrics[start:],'\n',1)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet) < 70:
end = start+ find_nth(lyrics[start:],'\n',2)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet) < 70:
end = start+ find_nth(lyrics[start:],'\n',3)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet) < 70:
end = start+ find_nth(lyrics[start:],'\n',4)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet) < 70:
end = start+ find_nth(lyrics[start:],'\n',5)
snippet = lyrics[start:end].replace('\n\n','')
if len(snippet) < 70:
snippet = 'Lyric too small to print. Consider requesting another song!!!\nPeace out puny Human!'
if len(snippet) < 70 or len(snippet)+n > 240:
flag=0
i+=1
return snippet
print(lyric_matcher("asdasdasd",'asdasdasdasdasd',9))