forked from odiroot/Unofficial-Google-Music-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_play.py
105 lines (86 loc) · 2.58 KB
/
example_play.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
import sys
import signal
from getpass import getpass
from gmusicapi.api import Api
try:
from PyQt4.QtGui import QApplication
from PyQt4.phonon import Phonon
PLAYER = "phonon"
except ImportError:
try:
import glib
import pygst
pygst.require("0.10")
import gst
PLAYER = "gst"
except ImportError:
PLAYER = None
def init(email=None, password=None):
api = Api()
logged_in = False
attempts = 0
while not logged_in and attempts < 3:
if email is None:
email = raw_input("Email: ")
if password is None or attempts > 0:
password = getpass()
logged_in = api.login(email, password)
attempts += 1
return api, logged_in
def play(url):
if PLAYER == "phonon":
app = QApplication(sys.argv,
applicationName="Google Music playing test")
media = Phonon.createPlayer(Phonon.MusicCategory,
Phonon.MediaSource(url))
media.play()
# Trick to allow Ctrl+C to exit.
signal.signal(signal.SIGINT, signal.SIG_DFL)
sys.exit(app.exec_())
elif PLAYER == "gst":
player = gst.element_factory_make("playbin2", "player")
player.set_property("uri", url)
player.set_state(gst.STATE_PLAYING)
glib.MainLoop().run()
else:
import subprocess
subprocess.call(["ffplay", "-vn", "-nodisp", "%s" % url])
return
def main():
import optparse
parser = optparse.OptionParser()
parser.add_option("-e", "--email", dest="email",
help="Your GMail address.")
parser.add_option("-p", "--password", dest="password",
help="You GMail password or application specific password.")
(options, args) = parser.parse_args()
api, success = init(email=options.email, password=options.password)
if success:
print "Success: logged in."
else:
print "Couldn't log in :("
return
query = raw_input("Search Query: ")
response = api.search(query)
songs = response["song_hits"]
if not len(songs):
print "Nothing found :("
return
for i, song in enumerate(songs[:10]):
print "%d." % i, "%(artist)s - %(title)s" % song
num = None
while num is None:
num = raw_input("Choose song: ")
try:
num = int(num)
except ValueError:
num = None
else:
try:
song = songs[num]
except IndexError:
num = None
url = api.get_stream_url(song["id"])
play(url)
if __name__ == '__main__':
main()