Skip to content

Instantly share code, notes, and snippets.

@swanson
Created June 18, 2010 03:04
Show Gist options
  • Save swanson/443166 to your computer and use it in GitHub Desktop.
Save swanson/443166 to your computer and use it in GitHub Desktop.
albumartgenerator
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
import sys
import urllib2
from lxml.html import fromstring
import random, string
class AlbumCreator(QDialog):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.cover = AlbumCover(self)
self.cover.show()
self.create_button = QPushButton(self)
self.create_button.setText("Generate new album cover")
self.create_button.clicked.connect(self.getNewCover)
self.color_button = QPushButton(self)
self.color_button.setText("Change font color")
self.color_button.clicked.connect(self.setColor)
self.save_button = QPushButton(self)
self.save_button.setText("Save")
self.save_button.clicked.connect(self.saveCover)
self.picker = QColorDialog(self)
buttons = QHBoxLayout()
buttons.addWidget(self.create_button, 0, Qt.AlignTop)
buttons.addWidget(self.color_button, 0, Qt.AlignTop)
buttons.addWidget(self.save_button, 0, Qt.AlignTop)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.cover, 0, Qt.AlignTop)
self.layout.addLayout(buttons)
self.layout.setSizeConstraint(QLayout.SetFixedSize)
self.setLayout(self.layout)
def saveCover(self):
image = QPixmap.grabWidget(self.cover).toImage()
defaultname = self.cover.artist + ' - ' + self.cover.album
initialpath = QDir.currentPath() + "/saved/" + defaultname + ".png"
filename = QFileDialog.getSaveFileName(self, "Save Album Cover", initialpath)
if filename.isEmpty():
return
else:
image.save(filename)
def setColor(self):
self.cover.setTextColor(self.picker.getColor())
def getNewCover(self):
self.cover.createAlbum()
self.updateGeometry()
class AlbumCover(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.image = QImage()
self.parent = parent
self.netManager = QNetworkAccessManager()
self.text_color = QColor('white')
self.parent.setWindowTitle("Album Cover Generator")
QObject.connect (self.netManager, SIGNAL ("finished(QNetworkReply*)"), self.handleNetworkData)
def sizeHint(self):
if self.image.isNull():
return QSize(400, 400)
else:
return QSize(self.image.width(), self.image.height())
def setTextColor(self, color):
self.text_color = color
self.repaint()
def createAlbum(self):
try:
self.get_album_art()
self.get_artist_name()
self.get_album_name()
except:
print "Error, trying again..."
self.createAlbum()
def get_album_art(self):
flickr_url = "http://www.flickr.com/explore/interesting/7days"
tree = fromstring(urllib2.urlopen(flickr_url).read())
x = [a.get('href') for a in tree.cssselect("tr td span a")]
tree = fromstring(urllib2.urlopen("http://www.flickr.com" + x[-7]).read())
x = [a.get('src') for a in tree.cssselect('div[class="photoImgDiv"] img')]
self.bg_url = x[0]
def get_artist_name(self):
wiki_url = "http://en.wikipedia.org/wiki/Special:Random"
opener = urllib2.build_opener()
opener.addheaders = [('User-agent','Mozilla/3.5')]
tree = fromstring(opener.open(wiki_url).read())
x = [a.text for a in tree.cssselect("h1")]
name = x[0].split('(')[0]
self.artist = string.capwords(name)
def get_album_name(self):
quote_url = "http://www.quotationspage.com/random.php3"
tree = fromstring(urllib2.urlopen(quote_url).read())
x = [a.text for a in tree.cssselect("dt a")]
quote = x[-1]
n = random.randint(3,5)
quote = quote.split(' ')
last_bit = quote[len(quote) - n:]
self.album = " ".join(last_bit)[:-1]
self.downloadImage(QUrl(self.bg_url))
def loadImage(self, img):
self.image = img
if self.image.isNull():
self.setFixedSize (800, 600)
self.parent.setWindowTitle(QString ("Can't load %1.").arg (self.filename))
else:
if (self.image.width() > 800) | (self.image.height() > 600):
w = self.image.width()
self.image = self.image.scaled (800, 600, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.parent.setWindowTitle("%s :: %s" % (self.artist, self.album))
self.setFixedSize (self.image.width(), self.image.height ())
self.update ()
def handleNetworkData(self, netReply):
img = QImage()
url = netReply.url()
if netReply.error():
self.filename = QString()
else:
self.filename = url.toString()
if url.scheme() == 'file':
self.filename = url.toLocalFile()
img.load (netReply, None)
netReply.deleteLater()
self.loadImage(img)
def paintEvent(self, event):
painter = QPainter(self)
if not self.image.isNull():
painter.drawImage(0, 0, self.image)
font = QFont('Helvetica', 28)
font.setBold(True)
painter.setFont(font)
painter.setPen(self.text_color)
painter.drawText(QRect(25, 0, self.image.width() - 50, 100), \
Qt.TextWordWrap | Qt.AlignTop | Qt.AlignRight, self.artist)
font = QFont('Helvetica', 20)
font.setItalic(True)
painter.setFont(font)
painter.drawText(QRect(25, self.image.height() - 100, self.image.width() - 50, \
100), Qt.TextWordWrap | Qt.AlignBottom | Qt.AlignLeft, self.album)
def downloadImage(self, url):
self.netManager.get(QNetworkRequest(url))
self.setWindowTitle(QString("Loading..."))
if __name__ == "__main__":
app = QApplication(sys.argv)
viewer = AlbumCreator()
viewer.show()
app.exec_()
del viewer
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment