-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAlbumCover.py
224 lines (188 loc) · 8.69 KB
/
getAlbumCover.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#####################################################################
import os
from PyQt5.QtCore import (QDir, QSize, Qt)
from PyQt5.QtGui import QIcon, QKeySequence, QPixmap
from PyQt5.QtWidgets import (QAction, QApplication, QFileDialog, QMainWindow,
QLabel, QLineEdit, QPushButton, QMessageBox, QComboBox)
import musicbrainzngs
import requests
from shutil import copy
myApp = "Cover Finder"
#####################################################################
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowIcon(QIcon.fromTheme("applications-multimedia"))
self.setStyleSheet("QMainWindow {padding: 10px; margin: 10px}")
self.setContentsMargins(10, 10, 10, 10)
self.artist = ""
self.album = ""
self.img = ""
self.releasesList = []
self.releasesListID = []
self.releasesTitle = ""
self.releasesID = 0
self.artistEntry = QLineEdit("", placeholderText = "Artist")
self.artistEntry.setFixedWidth(250)
self.albumEntry = QLineEdit("", placeholderText = "Album Name")
self.albumEntry.setFixedWidth(250)
self.findButton = QPushButton("get Cover")
self.findButton.clicked.connect(self.getCover)
self.tracklistButton = QPushButton("get Tracklist")
self.tracklistButton.clicked.connect(self.getTracks)
self.releasesButton = QPushButton("get all Artist releases")
self.releasesButton.clicked.connect(self.getReleases)
self.releasesButton.setFixedWidth(150)
self.releasesCombo = QComboBox()
self.releasesCombo.setFixedWidth(250)
self.releasesCombo.currentIndexChanged.connect(self.setReleaseIndex)
self.imageLabel = QLabel()
self.imageLabel.setScaledContents(False)
self.imageLabel.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.setCentralWidget(self.imageLabel)
self.createActions()
self.createToolBars()
self.createStatusBar()
self.setGeometry(50, 50, 800, 700)
self.setWindowTitle(myApp + '[*]')
musicbrainzngs.set_useragent(
"python-musicbrainzngs-example",
"0.1",
"https://github.com/alastair/python-musicbrainzngs/",)
def setReleaseIndex(self):
self.releasesTitle = self.releasesCombo.currentText()
self.releasesID = self.releasesListID[self.releasesCombo.currentIndex()]
self.albumEntry.setText(self.releasesCombo.currentText())
print(self.releasesTitle, self.releasesID)
def getReleases(self):
self.artist = self.artistEntry.text()
print(f"getting releases of artist '{self.artist}'")
self.statusBar().showMessage(f"getting releases of artist '{self.artist}'", 4000)
if not self.artist == "":
self.getArtistReleases(self.artist)
else:
print("Artist missing")
self.msgbox("Artist missing")
def getArtistReleases(self, artist):
self.releasesCombo.clear()
self.releasesList = []
self.releasesListID = []
try:
result = musicbrainzngs.search_releases(artist=artist, limit=99, primarytype = 'Album', strict=True)
for releases in result["release-list"]:
self.releasesList.append(releases["title"])
self.releasesListID.append(releases["id"])
except Exception as e:
print(e)
self.releasesCombo.addItems(self.releasesList)
def getTracklist(self, artist, album):
tracklist = []
try:
result = musicbrainzngs.search_releases(artist=artist, release=album, limit=1, primarytype = 'Album', strict=True)
id = result["release-list"][0]["id"]
print(f'{artist.title()} - {album.title()}\nTracks:')
tracklist.append(f'{artist.title()} - {album.title()}\nTracks:')
#### get tracklist
new_result = musicbrainzngs.get_release_by_id(id, includes=["recordings"])
t = (new_result["release"]["medium-list"][0]["track-list"])
for x in range(len(t)):
line = (t[x])
tracknumber = line["number"]
title = line["recording"]["title"]
tracklist.append(f'{tracknumber}. {title}')
tracks = ('\n'.join(tracklist))
print(tracks)
self.imageLabel.setText(tracks)
except Exception as e:
print(e)
self.statusBar().showMessage("nothing found", 4000)
def get_albumCover(self, artist, album):
idList = []
result = musicbrainzngs.search_releases(artist=artist, release=album, limit=10, primarytype = 'Album', strict=True)
### get all album ID
for a in result["release-list"]:
idList.append(a["id"])
data = []
coverList = []
for x in range(len(idList)):
try:
id = idList[x]
data = musicbrainzngs.get_image_front(id)
qp = QPixmap()
qp.loadFromData(data)
h = self.imageLabel.height()
self.imageLabel.setPixmap(qp.scaledToHeight(h, 0))
break
except Exception as e:
print(e)
def getCover(self):
self.artist = self.artistEntry.text()
self.album = self.albumEntry.text()
print(f"getting cover of album '{self.album}' from '{self.artist}'")
self.statusBar().showMessage(f"getting cover of album '{self.album}' from '{self.artist}'", 4000)
if not self.artist == "" and not self.album == "":
self.get_albumCover(self.artist, self.album)
else:
print("Artist or Album missing")
self.msgbox("Artist or Album missing")
def getTracks(self):
self.artist = self.artistEntry.text()
self.album = self.albumEntry.text()
print(f"getting tracklist of album '{self.album}' from '{self.artist}'")
self.statusBar().showMessage(f"getting tracklist of album '{self.album}' from '{self.artist}'", 4000)
if not self.artist == "" and not self.album == "":
self.getTracklist(self.artist, self.album)
else:
print("Artist or Album missing")
self.msgbox("Artist or Album missing")
def closeEvent(self, event):
event.accept()
def saveAs(self):
fileName, _ = QFileDialog.getSaveFileName(self, "Save File", f"front.jpg", "Images (*.jpg)")
if fileName:
pix = QPixmap(self.imageLabel.pixmap())
if pix:
pix.save(fileName)
def createActions(self):
self.saveAsAct = QAction(QIcon.fromTheme('document-save-as'),"Save &As...", self,
shortcut=QKeySequence.SaveAs,
statusTip="Save Cover",
triggered=self.saveAs)
def createToolBars(self):
self.fileToolBar = self.addToolBar("File")
self.fileToolBar.setStyleSheet("QToolBar {border: 0px;}")
self.fileToolBar.setIconSize(QSize(16, 16))
self.fileToolBar.setContextMenuPolicy(Qt.PreventContextMenu)
self.fileToolBar.setMovable(False)
self.fileToolBar.addWidget(self.artistEntry)
self.fileToolBar.addSeparator()
self.fileToolBar.addWidget(self.albumEntry)
self.fileToolBar.addSeparator()
self.fileToolBar.addWidget(self.findButton)
self.fileToolBar.addSeparator()
self.fileToolBar.addWidget(self.tracklistButton)
self.fileToolBar.addSeparator()
self.fileToolBar.addAction(self.saveAsAct)
self.addToolBarBreak()
self.releasesToolBar = self.addToolBar("Releases")
self.releasesToolBar.setStyleSheet("QToolBar {border: 0px;}")
self.releasesToolBar.setIconSize(QSize(16, 16))
self.releasesToolBar.setContextMenuPolicy(Qt.PreventContextMenu)
self.releasesToolBar.setMovable(False)
self.releasesToolBar.addWidget(self.releasesCombo)
self.releasesToolBar.addSeparator()
self.releasesToolBar.addWidget(self.releasesButton)
def createStatusBar(self):
self.statusBar().setStyleSheet("font-size: 8pt; color: #888a85;")
self.statusBar().showMessage("Ready")
def msgbox(self, message):
msg = QMessageBox(2, "Information", message, QMessageBox.Ok)
msg.exec()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())