-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp0.py
More file actions
339 lines (298 loc) Β· 9.27 KB
/
Copy pathapp0.py
File metadata and controls
339 lines (298 loc) Β· 9.27 KB
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
# -*- coding: utf-8 -*-
# This simple app uses the '/translate' resource to translate text from
# one language to another.
# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid
# import lyricwikia
#gen.py
# class gen:
# GENIUS_API_TOKEN = client_access_token = 'q2iKacoJeJRs5q6UC1AnYWN4IriZnwgV4FhoXWxIERxORuOb9GxUdAbWDD_K1D5q'
# item = input('Enter the song to find its lyrics: ')
import os, requests, uuid, json, re
import requests
from youtube_title_parse import get_artist_title
from google_trans_new import google_translator
from bs4 import BeautifulSoup, UnicodeDammit, NavigableString
translator = google_translator()
# from googletrans import Translator
# translator = Translator()
# import datetime
# GCS_API_KEY = 'AIzaSyCHOkya9h3n7BWA10S_CQQkz-p9Wlreh44'
# GCS_ENGINE_ID = 'Danilator'
# from lyrics_extractor import SongLyrics
def getHTML(url):
headers_Get = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
searchPage = requests.get(url, headers=headers_Get)
html = BeautifulSoup(searchPage.text,'html.parser')
return html
def getLyrics(title):
# return lyricwikia.get_lyrics('Led Zeppelin', 'Stairway to heaven')
# search("")
# print("title",title)
url='https://www.google.com/search?q='
# s=input('Enter the song to find its lyrics: ')
s = '\"'+title+'\"'
#calculating time taken in searching lyrics
# a = datetime.datetime.now()
for i in s:
if i==' ':
url+='+'
else:
url+=i
url+='+lyrics'
html = getHTML(url)
# print("\n#####",html,"\n###########")
song_info = []
song_title = []
for paper in html.findAll("div",class_="auw0zb"):
for desc in paper.descendants:
print("DDDDDDDD",desc)
info = [desc.strip() for desc in paper.descendants if type(desc) == NavigableString]
for i in info:
song_title.append(i)
print("\n#####333333",song_title,"\n###########")
if len(song_title) > 1:
songtitle = song_title[1].split(" lyrics Β©")[0]
song_info.append(songtitle)
artists = []
for paper in html.findAll("div",class_="wx62f PZPZlf x7XAkb"):
for desc in paper.descendants:
print("DDDDDDDD",desc)
info = [desc.strip() for desc in paper.descendants if type(desc) == NavigableString]
for i in info:
artists.append(i)
print("\n#####",artists,"\n###########")
if len(artists) > 0:
artist = artists[0].replace("Song by ","")
song_info.append(artist)
if len(song_info) < 2:
print("wwwwwwwwwwww",song_info)
song_info = []
for paper in html.findAll("div",class_="SPZz6b"):
for desc in paper.descendants:
print("DDDDDDDD",desc)
info = [desc.strip() for desc in paper.descendants if type(desc) == NavigableString]
for i in info:
song_info.append(i)
lyrics=html.find_all("span", jsname="YS01Ge")
if lyrics==[]:
print('Couldn\'t get lyrics')
else:
nlyr = ""
nl = ["; ","\n"]
c = 0
for i in lyrics:
# print(i.get_text())
nlyr += i.get_text() + nl[c%2]
c+=1
return nlyr, song_info
def changes(txt,dict):
for k in dict:
txt = txt.replace(k,dict[k])
return txt
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def doit(item):
if item == "":
return "", ["",""]
# item = input('Enter the song to find its lyricsxx: ')
res = None
maxTries = 4
mc = 0
lyricsText, song_info = "sorry, lyrics not found, try again soon ",["Could not find lyrics",item]
while res is None and mc <= maxTries:
# try:
target = item
print("ITEM:"+target)
artist_title = get_artist_title(target)
if artist_title is not None:
print("@@@@@@@@@@@@@@@@@@@@",artist_title)
artist, title = artist_title
if mc==1:
target = title+" " + artist
item = item.replace("&"," and ")
if mc==2:
target = title+" " + artist.split(" ")[0]
if mc==3:
target = title
else:
print("!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!")
print("TTTTTTTTTTTTTTTTT")
print(target)
print("TTTTTTTTTTTTTTTTT")
# except:
# print("E: could not parse artist and title")
res = getLyrics(target)
mc+=1
if res is not None and len(res) > 1:
lyricsText, song_info = res
chars = [["-"," "],["(",""],[")",""]]
for c in chars:
lyricsText = lyricsText.replace(c[0],c[1])
# .replace().replace("(","").replace(")","")
lyrics = []
for l in lyricsText.split("\n"):
lyrics.append(l)
body = [{
'text' : lyricsText
}]
translated = []
n = 0
parts = []
print(lyricsText)
print(type(lyricsText), len(lyricsText),len(lyricsText.split("\n")))
maxChunks = 50
for chunk in chunks(lyricsText.split("\n"), maxChunks):
chunkT = "\n".join(chunk)
print("!!!!!!!!!!!!","\n",chunkT)
res = translator.translate(str(chunkT),lang_tgt="he")
print("$$$$$$$$$$$$$$$$$$$$$")
print(res)
parts.append(res+"\n")
# time.sleep(1)
# n+=1
# res = translator.translate(lyricsText,lang_tgt="he")
print("########################@@@")
print(parts)
# translated = res.__dict__()["text"]
translated = ("".join(parts)).split("\n")
print("############################")
print(translated)
print("XXXXXXXXXXXXXXXXXXXXXXXXXXX")
fullL = []
# fullt = "<h1>"+item+"</h1>"
for c in range(len(lyrics)):
fullL.append(lyrics[c])
if c < len(translated):
fullL.append(translated[c])
fullL.append("")
fullL.append("")
# fullt += "<pre>"+lyrics[c] + "</pre>"
# fullt += "<pre>"+translated[c][::-1] + "</pre>"
# print(lyrics[c])
# print(translated[c])
print("")
print(" @ @ @ @ @ @ @")
print(fullL)
return fullL, song_info
from flask import Flask, render_template, request, redirect # add
# from Translate import Danilator
# Danilator.search("new soul")
key_var_name = '436cb96eb2d94be29c81bfd3dd5fcd5e'
# if not key_var_name in os.environ:
# raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = '436cb96eb2d94be29c81bfd3dd5fcd5e'
endpoint = "https://api.cognitive.microsofttranslator.com/"
# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
path = '/translate?api-version=3.0'
params = '&to=he'
constructed_url = endpoint + path + params
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('base.html')
# loading = "aaaaaaaaa"
@app.route('/', methods=['POST'])
def my_form_post():
query = request.form['text']
processed_text = process_text(query)
print("!!!!!!!!!",processed_text)
return get_all(processed_text)
def process_text(query):
print("QQQQQQQQQQQQQQQQ",query)
if query is "":
return render_template('base.html',title = u"\U0001F49A")
urlChecks = ["http","youtu","spotify"]
url = False
for check in urlChecks:
if check.lower() in query.lower():
url = True
print("START")
processed_text = ""
if url:
print(query,"!!!!!!!!!!!!!!!!!!!")
query = str(re.search("(?P<url>https?://[^\s]+)", query).group("url"))
print(query,"!!!!!!!!!!!!!!!!!!!!")
html = getHTML(query)
try:
if "spotify" in query:
full = html.title.string
full = full.split(', a song by ')
full[1] = full[1].split(" ")[0].replace(","," ")
title, artist = full
title = str(re.sub(r" ?\([^)]+\)", "", title))
artist = str(re.sub(r" ?\([^)]+\)", "", artist))
processed_text = title +" "+artist
print(processed_text)
else:
processed_text = "-".join(html.title.string.split("-")[:-1]).replace("/"," ")
except Exception as e:
print("EEEEEEEEEEEEEEEEEE: Processing Text ",e)
processed_text = ""
return render_template('base.html')
print("UUUUUUUUUUUUUUUUUUUUUU",processed_text)
print("UUUUUUUUUUUUUUUUUUUUUU",processed_text)
print("UUUUUUUUUUUUUUUUUUUUUU",processed_text)
else:
processed_text = query
if processed_text == "":
return " "
return processed_text
def get_all(processed_text):
a, song_info = doit(processed_text)
# print(a)
# return redirect('/lyrics/'+text)
song_txt = ""
if song_info is None or len(song_info) < 1:
song_info.append(processed_text)
song_info.append("___")
if song_info[0] is not "":
song_txt = song_info[0]
for s in song_info[1:]:
song_txt += " - "+s
print("!!!!!!!!!!!!!!!!!!",song_info[0]+" - "+song_info[1])
return render_template('base.html',tasks = a , title = song_txt)
return render_template('base.html')
return a
#
# @app.route('/lyrics/<string:title>')
# def get_stairway(title):
# processed_text = process_text(title.replace("+"," "))
# print("!!!!!!!!!",processed_text)
# return get_all(processed_text)
#
@app.route('/<path:text>', methods=['GET', 'POST'])
def all_routes(text):
print("@@@@@@@@@")
print(text)
if "lyrics/" in text:
text = text.replace("lyrics/","")
processed_text = process_text(text.replace("+"," "))
print("!!!!!!!!!",processed_text)
return get_all(processed_text)
#
# if text in refs:
# return redirect(refs[text])
# else:
# return redirect("/")
if __name__ == "__main__":
app.run(debug=True)