-
Notifications
You must be signed in to change notification settings - Fork 0
/
serenade.py
executable file
·178 lines (136 loc) · 5.17 KB
/
serenade.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Mike Sheldon <elleo@gnu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PySide import QtCore, QtGui, QtDeclarative
from QtMobility import MultimediaKit
from gmusicapi.api import Api
from artistmodel import *
from songmodel import *
import sys, datetime, os, os.path, urllib2, gconf, signal, threading
class Signals(QtCore.QObject):
onDoneWorking = QtCore.Signal()
onError = QtCore.Signal(str, str)
def __init__(self, parent=None):
super(Signals, self).__init__(parent)
class Serenade():
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.app.setApplicationName("Serenade")
signal.signal(signal.SIGINT, signal.SIG_DFL)
self.position = 0
self.player = MultimediaKit.QMediaPlayer(self.app)
self.player.positionChanged.connect(self.positionChanged)
self.signals = Signals()
self.client = gconf.client_get_default()
self.api = Api()
self.artists = []
self.library = {}
self.cacheDir = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.CacheLocation)
if not os.path.exists(self.cacheDir):
os.mkdir(self.cacheDir)
self.artistModel = ArtistModel()
self.songModel = SongModel()
self.signals.onDoneWorking.connect(self.doneWorking)
self.signals.onError.connect(self.error)
self.view = QtDeclarative.QDeclarativeView()
self.view.setSource("/opt/serenade/qml/Main.qml")
self.rootObject = self.view.rootObject()
self.context = self.view.rootContext()
self.context.setContextProperty('songModel', self.songModel)
self.context.setContextProperty('artistModel', self.artistModel)
self.rootObject.openFile("MenuPage.qml")
self.rootObject.refresh.connect(self.updateSongs)
self.rootObject.newLogin.connect(self.newLogin)
self.rootObject.play.connect(self.play)
self.rootObject.togglePause.connect(self.togglePause)
self.login()
self.view.showFullScreen()
sys.exit(self.app.exec_())
def login(self):
email = self.client.get_string('/apps/serenade/email')
passwd = self.client.get_string('/apps/serenade/passwd')
if not email or not passwd:
self.rootObject.openFile("LoginPage.qml")
else:
logged_in = self.api.login(email, passwd)
if not logged_in:
self.rootObject.openFile("LoginPage.qml")
self.rootObject.showMessage("Login failed", "Please check your email and password and try again. If you're using two-factor authentication you may need to set an application specific password in your Google account.")
else:
self.rootObject.openFile("MenuPage.qml")
self.updateSongs()
def newLogin(self, email, passwd):
self.client.set_string('/apps/serenade/email', email)
self.client.set_string('/apps/serenade/passwd', passwd)
self.login()
def updateSongs(self):
self.rootObject.startWorking()
thread = threading.Thread(target=self._updateSongs)
thread.start()
def _updateSongs(self):
self.library = sorted(self.api.get_all_songs(), key=lambda x: x['title'])
self.library.reverse()
for s in self.library:
if "albumArtUrl" in s:
art = self.getImage(s['albumArtUrl'])
else:
art = "/opt/serenade/no-art.png"
if s['artist'] not in self.artists:
artist = Artist(s['artist'], art)
self.artists.append(s['artist'])
self.artistModel.add(artist)
song = Song(s['id'], s['title'], s['album'], s['artist'], False, art)
self.songModel.add(song)
self.artistModel.sort()
self.signals.onDoneWorking.emit()
def doneWorking(self):
self.rootObject.stopWorking()
def error(self, title, message):
self.rootObject.showMessage(title, message)
def getImage(self, url):
filename = url.split("/")[-1]
imagePath = os.path.join(self.cacheDir, filename)
imagePath = imagePath.replace("?", "")
if not os.path.exists(imagePath):
try:
out = open(imagePath, 'wb')
out.write(urllib2.urlopen("http:" + url).read())
out.close()
except Exception, err:
return "/opt/serenade/no-art.png"
return imagePath
def play(self, songid):
url = self.api.get_stream_url(songid)
self.playlist = MultimediaKit.QMediaPlaylist()
self.playlist.addMedia(MultimediaKit.QMediaContent(QtCore.QUrl(url)))
self.player.setPlaylist(self.playlist)
self.player.play()
print url
def togglePause(self):
if self.player.state() == MultimediaKit.QMediaPlayer.PlayingState:
self.player.pause()
else:
self.player.play()
def positionChanged(self, pos):
print pos, self.player.duration()
self.position = pos / float(self.player.duration())
self.rootObject.setPosition(self.position)
print self.position
def openLink(self, link):
QtGui.QDesktopServices.openUrl(link)
if __name__ == "__main__":
Serenade()