This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (75 loc) · 2.99 KB
/
main.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
import logging
from time import sleep
from getpass import getpass
import keyring
from raopy import RAOPPlayGroup, RAOPServiceListener, STATUS
from raopy.util import set_logs_enabled, set_loglevel, LOG
from raopy.exceptions import DeviceAuthenticationRequiresPasswordError, DeviceAuthenticationRequiresPinCodeError, \
DeviceAuthenticationWrongPasswordError
# the basic config needs to be enabled for the lowest required loglevel
#logging.basicConfig(level=logging.DEBUG)
#set_logs_enabled(LOG.RTSP | LOG.RECEIVER)# | LOG.CONTROL)
set_logs_enabled(LOG.GROUP | LOG.RTSP)
set_loglevel(LOG.ALL, logging.DEBUG)
set_loglevel(LOG.RECEIVER, logging.INFO)
SHAIRPORT_DEVICE3 = "JonasMacbookpro._raop._tcp.local."
SHAIRPORT_DEVICE1 = "MacBookPro._raop._tcp.local."
SHAIRPORT_DEVICE0 = "Wohnzimmer._raop._tcp.local."
SHAIRPORT_DEVICE2 = "ATV._raop._tcp.local."
group = RAOPPlayGroup()
def add_receiver(device, name, info):
if SHAIRPORT_DEVICE0 == name.split("@")[1] or SHAIRPORT_DEVICE1 == name.split("@")[1]:
try:
group.add_receiver(device)
except DeviceAuthenticationRequiresPasswordError:
# try to connect with a password
pwd = getpass("Enter password: ")
while True:
try:
group.add_receiver(device, password=pwd)
break
except DeviceAuthenticationWrongPasswordError:
pwd = getpass("Wrong password, try again: ")
except DeviceAuthenticationRequiresPinCodeError:
# connect with a pin code
keychain = "Pygroup"
# check if we already got credentials for this device
auth_data = keyring.get_password(keychain, device.name)
if auth_data:
auth_identifier, auth_secret = auth_data.split(":")
else:
# request a pin code an generate credentials for this device
group.request_pincode_for_device(device)
pin = getpass("Enter pin code: ")
auth_identifier, auth_secret = group.request_login_credentials_for_device_(device, pin)
# save password
keyring.set_password(keychain, device.name, "{0}:{1}".format(auth_identifier, auth_secret))
# try to connect with new credentials
group.add_receiver(device, credentials=(auth_identifier, auth_secret))
def remove_receiver(device, name, info):
group.remove_receiver(device)
listener = RAOPServiceListener()
listener.on_connect += add_receiver
listener.on_disconnect += remove_receiver
listener.start_listening()
# listen for new devices for 5 seconds
sleep(5)
group.play("sample.mp3")
# sleep(4)
# print("Pause it now.")
# group.stop()
#group.pause()
# group.set_progress(90000)
#sleep(4)
# print("Resume it now.")
#group.resume()
#sleep(14)
# print("Pause it now.")
#group.pause()
#sleep(6)
# print("Resume it now.")
#group.resume()
# play until the track is finished
while group.status != STATUS.STOPPED:
sleep(1)
group.close()