-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics.py
169 lines (132 loc) · 5.23 KB
/
lyrics.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
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
import requests
from bs4 import BeautifulSoup
import sqlite3
class Lyrics(QTabWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("PYRICS")
self.resize(600, 500)
self.setWindowIcon(QIcon('img/icon.png'))
self.tab1 = QWidget()
self.tab2 = QWidget()
self.addTab(self.tab1, "Tab 1")
self.addTab(self.tab2, "Tab 2")
self.tab1UI()
self.tab2UI()
self.login()
self.db()
self.updateFav()
def tab1UI(self):
self.SearchButon = QPushButton(self)
self.SearchButon.setText("Search")
self.FavButon = QPushButton(self)
self.FavButon.setText("Add a new favorite")
self.FavButon.setIcon(QIcon("img/star+.png"))
self.listwidget = QListWidget(self)
self.LineEdit = QLineEdit(self)
self.TextEdit = QTextEdit(self)
self.ErrorMessage = QAction(self)
self.ErrorMessage.setText("Uyarı")
self.ErrorMessage.triggered.connect(self.errorMessageShow)
self.InfoMessage = QAction(self)
self.InfoMessage.setText("Bilgi")
self.InfoMessage.triggered.connect(self.infoMessageShow)
layout = QGridLayout(self)
layout.addWidget(self.LineEdit, 0, 0)
layout.addWidget(self.SearchButon, 1, 0)
layout.addWidget(self.FavButon, 2, 0)
layout.addWidget(self.listwidget, 3, 0)
layout.addWidget(self.TextEdit, 0, 1, 4, 1)
self.setTabText(0, "Search")
self.setTabIcon(0, QIcon("img/search.png"))
self.tab1.setLayout(layout)
self.SearchButon.clicked.connect(self.printTitle)
self.listwidget.itemDoubleClicked.connect(self.printLycris)
self.FavButon.clicked.connect(self.addFav)
def tab2UI(self):
self.TextFav = QTextEdit(self)
self.ListFav = QListWidget(self)
self.DeleteButon = QPushButton(self)
self.DeleteButon.setText("Delete")
self.DeleteButon.setIcon(QIcon("img/star-.png"))
layout2 = QGridLayout(self)
layout2.addWidget(self.DeleteButon, 0, 0)
layout2.addWidget(self.ListFav, 1, 0, 2, 2)
layout2.addWidget(self.TextFav, 0, 2, 3, 2)
self.tab2.setLayout(layout2)
self.setTabText(1, "My Favorite")
self.setTabIcon(1, QIcon("img/star.png"))
self.ListFav.doubleClicked.connect(self.printFavLyrics)
self.DeleteButon.clicked.connect(self.deleteFav)
def login(self):
self.base_url = 'https://api.genius.com'
self.headers = {'Authorization': 'Bearer ' + 'YOUR CLIENT ACCESS TOKEN'}
self.search_url = self.base_url + '/search'
def printTitle(self):
self.listwidget.clear()
self.titles = []
data = {'q': self.LineEdit.text()}
response = requests.get(self.search_url, data=data, headers=self.headers)
json = response.json()
self.hits = json['response']['hits']
for hit in self.hits:
result = hit["result"]["full_title"]
self.titles.append(result)
self.listwidget.addItems(self.titles)
def getTitle(self):
self.title = self.hits[self.getIndex()]["result"]["full_title"]
return self.title
def getIndex(self):
self.index = self.listwidget.currentRow()
return self.index
def getUrl(self):
self.url = self.hits[self.getIndex()]["result"]["url"]
return self.url
def printLycris(self):
self.TextEdit.setText(self.getLycris(self.getUrl()))
def getLycris(self, url):
page = requests.get(url)
html = BeautifulSoup(page.content, 'html.parser')
lyrics = html.find('div', class_='lyrics').text
return lyrics
def errorMessageShow(self):
QMessageBox.warning(self, "Error", "Lütfen sıra seçin")
def infoMessageShow(self):
QMessageBox.information(self, "Info", "Successfully added")
def addFav(self):
if self.getIndex() == -1 :
self.errorMessageShow()
else :
title = self.getTitle()
url = self.getUrl()
self.im.execute("""INSERT INTO favorite(title, url) VALUES(?, ?)""", (title, url))
self.db.commit()
self.infoMessageShow()
self.updateFav()
def updateFav(self):
self.ListFav.clear()
self.im.execute("""SELECT title FROM favorite """)
for i in self.im.fetchall():
self.ListFav.addItems(i)
def printFavLyrics(self):
self.favTitle = self.ListFav.currentItem().text()
self.im.execute("""SELECT * FROM favorite WHERE title = ? """, (self.favTitle,))
url = self.im.fetchone()[1]
self.TextFav.setText(self.getLycris(url))
def db(self):
self.db = sqlite3.connect("favorite.db")
self.im = self.db.cursor()
self.im.execute("""CREATE TABLE IF NOT EXISTS favorite (title, url)""")
def deleteFav(self):
self.favTitle = self.ListFav.currentItem().text()
self.im.execute("""DELETE FROM favorite WHERE title = ? """, (self.favTitle,))
self.db.commit()
self.updateFav()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Lyrics()
window.show()
app.exec_()