-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
136 lines (113 loc) · 3.62 KB
/
player.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
from . import connection as _connection
from . import nowplaying as _nowplaying
from . import properties as _properties
from . import rpc as _rpc
def get_active_player():
"""Returns all active players"""
try:
player = _rpc.request('Player.GetActivePlayers')
return player[0]
except:
print('WARNING: No active player')
return {}
def get_item():
"""Retrieves the currently played item"""
try:
# get active playerid + content type
player = get_active_player()
# get properties
item = _rpc.request('Player.GetItem', {'playerid': player['playerid'], 'properties': _properties.select()})
return item['item']
except:
print('WARNING: Could not get item')
return {}
def get_album_art_url():
try:
item = get_item()
album_art = _rpc.request('Files.PrepareDownload', {'path': item['thumbnail']})
album_art_url = 'http://' + _connection.host + ':' + str(_connection.port) + '/' + album_art['details']['path']
return album_art_url
except:
return ''
def play_pause():
"""Pauses or unpause playback and returns the new state"""
try:
playerid = get_active_player()['playerid']
status = _rpc.request('Player.PlayPause', {'playerid': playerid})
play = bool(status['speed'])
_nowplaying.update()
return play
except:
_nowplaying.reset()
return False
def stop():
"""Stops playback"""
try:
playerid = get_active_player()['playerid']
_rpc.request('Player.Stop', {'playerid': playerid})
_nowplaying.reset()
except:
_nowplaying.reset()
pass
def get_position():
try:
playerid = get_active_player()['playerid']
position = _rpc.request('Player.GetProperties', {'playerid': playerid, 'properties': ['percentage', 'time', 'totaltime', 'speed']})
return position
except:
return {}
def go_to_next():
try:
player = get_active_player()
_rpc.request('Player.GoTo', {'playerid': player['playerid'], 'to': 'next'})
_nowplaying.update()
except:
_nowplaying.reset()
pass
def go_to_previous():
try:
player = get_active_player()
_rpc.request('Player.GoTo', {'playerid': player['playerid'], 'to': 'previous'})
_nowplaying.update()
except:
_nowplaying.reset()
pass
def go_to_playlist_position(position):
try:
player = get_active_player()
_rpc.request('Player.GoTo', {'playerid': player['playerid'], 'to': position})
_nowplaying.update()
except:
pass
def fast_forward():
try:
player = get_active_player()
if player['type'] == 'audio':
go_to_next()
position = get_position()
else:
position = _rpc.request('Player.Seek', {'playerid': player['playerid'], 'value': 'smallforward'})
_nowplaying.update()
return position
except:
_nowplaying.reset()
return {}
def fast_rewind():
try:
player = get_active_player()
if player['type'] == 'audio':
go_to_previous()
position = get_position()
else:
position = _rpc.request('Player.Seek', {'playerid': player['playerid'], 'value': 'smallbackward'})
_nowplaying.update()
return position
except:
_nowplaying.reset()
return {}
def next_subtitle():
try:
player = get_active_player()
_rpc.request('Player.SetSubtitle', {'playerid': player['playerid'], 'subtitle': 'next', 'enable': True})
except:
pass