Repository navigation
Expand file tree
/
Copy pathservice.py
More file actions
191 lines (168 loc) · 8.7 KB
/
Copy pathservice.py
File metadata and controls
191 lines (168 loc) · 8.7 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
import json
import os
from datetime import date, datetime
from pathlib import Path
from time import sleep
import requests
import servicemanager
import spotipy
import spotipy.util as util
import win32event
import win32service
import win32serviceutil
import ssmi
config_dir = str(Path.home()) + '\\SSMI'
def writeLog(msg):
today = date.today()
if not os.path.exists(config_dir + '\\logs\\'):
os.mkdir(config_dir + '\\logs\\')
with open(config_dir + '\\logs\\' + today.strftime("%d-%m-%Y") + '.txt', 'a') as f:
f.write('[' + datetime.now().strftime('%d/%m/%Y %H:%M:%S') + '] ' + msg + '\n')
def load_creds():
if os.path.exists(config_dir + '\\creds.json'):
with open(config_dir + '\\creds.json', 'r') as f:
creds = json.load(f)
return creds
else:
with open(config_dir + '\\creds.json', 'w') as f:
json.dump({'username': '', 'client_id': '', 'client_secret': ''}, f)
return None
def steeelseries_load():
path = os.getenv('PROGRAMDATA') + '\\SteelSeries\\SteelSeries Engine 3\\coreProps.json'
tries = 0
while not os.path.exists(path) and tries < 5:
tries += 1
sleep(1)
if not os.path.exists(path) and tries == 5:
return None
with open(path, 'r') as f:
data = json.load(f)
return data
class SSMIService(win32serviceutil.ServiceFramework):
# Service control and SteelSeries
ss_login_status = False
running = False
# Counts how many times a Spotify check returned 'None' or is on the wrong device
invalid_count = 0
target = ''
_svc_name_ = 'SSMIService'
_svc_display_name_ = 'SteelSeries Media Integration Service'
_svc_description_ = 'Calls the SteelSeries Engine GameSense API to update current track info.'
# Spotify
# MAKE SURE THE CONFIG (creds.json) IS SET CORRECTLY
sp = None
@classmethod
def parse_command_line(cls):
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
super().__init__(args)
writeLog('Service init')
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.running = False
ssmi.remove_game(self.target, 'SSMI')
writeLog('SSMI shutdown')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.running = True
# SteelSeries init
ss_data = steeelseries_load()
if ss_data is None:
writeLog('Error: Couldn\'t load coreProps.json')
return
self.target = 'http://' + ss_data['address'] + '/'
try:
ssmi.game_metadata(self.target, 'SSMI', 'SteelSeries Media Integration', 'Jack Hogan')
except requests.exceptions.ConnectionError:
sleep(60)
try:
ssmi.game_metadata(self.target, 'SSMI', 'SteelSeries Media Integration', 'Jack Hogan')
except requests.exceptions.ConnectionError:
return
ssmi.bind_event(self.target, 'SSMI', 'UPDATE', 0, 100, 23)
self.ss_login_status = True
# Spotify init
creds = load_creds()
self.creds = creds
if creds is None:
writeLog('Error: Couldn\'t load creds.json')
return
self.sp = spotipy.Spotify(client_credentials_manager=spotipy.SpotifyClientCredentials(client_id=
creds['client_id'],
client_secret=
creds['client_secret']),
auth=util.prompt_for_user_token(creds['username'], scope='user-read-currently'
'-playing, '
'user-read-playback-state',
client_id=creds['client_id'],
client_secret=creds['client_secret'],
redirect_uri='http://localhost:8888/callback'))
writeLog('SSMI init complete')
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.loop()
def loop(self):
while self.running:
try:
current = self.sp.current_playback()
except spotipy.SpotifyException:
try:
self.sp = spotipy.Spotify(client_credentials_manager=spotipy.SpotifyClientCredentials(client_id=
self.creds[
'client_'
'id'],
client_secret=
self.creds[
'client'
'_secret']
),
auth=util.prompt_for_user_token(self.creds['username'],
scope='user-read-currently'
'-playing, '
'user-read-playback-state',
client_id=self.creds['client_id'],
client_secret=self.creds['client_secret'],
redirect_uri='http://localhost:8888'
'/callback'))
except spotipy.SpotifyException:
writeLog("Error: Couldn't refresh access token")
self.SvcStop()
return
else:
current = self.sp.current_playback()
if self.invalid_count < 5 and \
(current is None or (current is not None and current['device']['type'] != 'Computer')):
self.invalid_count += 1
elif current is not None and current['device']['type'] == 'Computer':
self.invalid_count = 0
# Remove event when Spotify not open
if self.invalid_count == 5:
self.ss_login_status = False
ssmi.remove_event(self.target, 'SSMI', 'UPDATE')
# Rebind event if not bound already and Spotify connection OK
elif not self.ss_login_status and self.invalid_count == 0:
self.ss_login_status = True
ssmi.bind_event(self.target, 'SSMI', 'UPDATE', 0, 100, 23)
# Update screen if everything is good
if self.ss_login_status and self.invalid_count == 0:
title = current['item']['name']
if len(title) > 14:
title = title[:11] + '...'
artist, coartist_count = current['item']['artists'][0]['name'], len(current['item']['artists']) - 1
if coartist_count > 0:
if len(artist) > 11:
artist = artist[:8] + '...'
artist += ' +' + str(coartist_count)
elif len(artist) > 14:
artist = artist[:11] + '...'
progress = int(current['progress_ms'] / current['item']['duration_ms'] * 100)
ssmi.update_event(self.target, 'SSMI', 'UPDATE', title, artist, progress)
else:
# Keep alive if can't update
ssmi.heartbeat(self.target, "SSMI")
sleep(0.5)
if __name__ == '__main__':
if not os.path.exists(config_dir):
os.mkdir(config_dir)
SSMIService.parse_command_line()