-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_sonos_meta_data.py
154 lines (121 loc) · 4.64 KB
/
get_sonos_meta_data.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
'''
This script is intended to collect metadata on Amazon Music Cloud songs and place it in a database amazon_music.db
'''
import os
#import argparse
import time
from time import sleep
#import datetime
import xml.etree.ElementTree as ET
import sys
import cgi
import urllib
import requests
home = os.path.split(os.getcwd())[0]
soco_dir = os.path.join(home,'SoCo')
sys.path = [soco_dir] + sys.path
print sys.path
import soco
from soco import config
from amazon_music_db import *
config.CACHE_ENABLED = False
n = 0
while 1:
n+=1
print "attempt "+str(n)
try:
speakers = list(soco.discover())
except TypeError:
sleep(1)
else:
break
sleep(0.1)
print speakers ################
# appears that the property coordinator of s.group is not getting set properly and so can't use s.group.coordinator[.player_name]
for s in speakers:
if s:
#print "speaker: {} - master: {}".format(s.player_name, s.group) #s.group.coordinator.player_name)
print s.player_name
for s in speakers:
if s.is_coordinator:
master = s
print "\nNOTE: found coordinator and master =",master.player_name
break
else:
master = speakers[0]
print "\nALERT: id not find coordinator so took speaker[0] =",master.player_name
# for s in speakers:
# if s != master:
# s.join(master)
print "\nprogram running ..."
# not in use in this script
def get_info():
info = master.avTransport.GetMediaInfo([('InstanceID', 0)])
uri = info['CurrentURI']
meta = info['CurrentURIMetaData']
print "uri = ", uri
print "meta = ", meta
# not in use in this script
def my_add_to_queue(uri, metadata):
response = master.avTransport.AddURIToQueue([
('InstanceID', 0),
('EnqueuedURI', uri),
('EnqueuedURIMetaData', metadata),
('DesiredFirstTrackNumberEnqueued', 0),
('EnqueueAsNext', 1)
])
qnumber = response['FirstTrackNumberEnqueued']
print response
return int(qnumber)
prev_title = -1 #this is -1 so if the initial song title is the empty string, it's not equal
tt = time.time()
rows = session.query(Song).count()
print "rows = ", rows
while 1:
try:
state = master.get_current_transport_info()['current_transport_state']
except (requests.exceptions.ConnectionError, soco.exceptions.SoCoUPnPException) as e:
state = 'ERROR'
print "Encountered error in state = master.get_current transport_info(): ", e
if state == 'PLAYING':
if time.time() - tt > 10:
#get_current_track_info() = {
#u'album': 'We Walked In Song',
#u'artist': 'The Innocence Mission',
#u'title': 'My Sisters Return From Ireland',
#u'uri': 'pndrradio-http://audio-sv5-t3-1.pandora.com/access/5459257820921908950?version=4&lid=86206018&token=...',
#u'playlist_position': '3',
#u'duration': '0:02:45',
#u'position': '0:02:38',
#u'album_art': 'http://cont-ch1-2.pandora.com/images/public/amz/3/2/9/3/655037093923_500W_500H.jpg'}
current_track = master.get_current_track_info()
title = current_track['title']
artist = current_track['artist'] # for lyrics
tt = time.time()
print str(tt), "checking to see if track has changed"
if prev_title != title:
track = dict(current_track)
if track.get('uri','').find(':amz') != -1:
try:
s = Song(artist=track.get('artist'),title=track.get('title'),album=track.get('album'), album_art=track.get('album_art'), uri=track.get('uri'))
session.add(s)
session.commit()
except (IntegrityError, OperationalError) as e:
session.rollback()
print "IntegrityError: ",e
else:
try:
print u"\n{} {} {} {} added to db\n".format(track.get('artist'), track.get('title'), track.get('album'), track.get('uri'))
except UnicodeEncodeError as e:
print "UnicodeEncodeError: ",e
prev_title = title
tt = time.time()
try:
master.next()
except:
sys.exit(1)
else:
print "state=",state
sleep(.1)
rows = session.query(Song).count()
print "rows = ", rows