-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
95 lines (73 loc) · 3.06 KB
/
app.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
import base64
from flask import Flask, request, send_from_directory
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager
app = Flask(__name__)
global_sessions = list()
def get_key(dict):
message = u' '.join((dict['artist'], dict['title'])).encode('utf-8').strip()
base64_bytes = base64.urlsafe_b64encode(message)
return base64_bytes.decode('ascii')
import subprocess
def get_proc_name(AppId):
if AppId.endswith('.exe'):
return AppId.replace('.exe', '')
cmd = f"get-StartApps | Where-Object {{$_.AppId -like '{AppId}'}} | Select Name | foreach {{ $_.Name }}"
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
if (completed.returncode != 0):
return ''
return completed.stdout.decode('ascii').replace('\r\n', '')
async def get_media_info():
sessions = await MediaManager.request_async()
for item in sessions.get_sessions():
if item:
obj = {}
info = await item.try_get_media_properties_async()
info_dict = {song_attr: info.__getattribute__(song_attr) for song_attr in dir(info) if song_attr[0] != '_'}
info1 = {}
info1['title'] = info_dict['title']
info1['artist'] = info_dict['artist']
id = get_key(info1)
info1['id'] = id
obj['id'] = id
info1['status'] = 'paused'
if item.get_playback_info().playback_status == item.get_playback_info().playback_status.PLAYING:
info1['status'] = 'playing'
obj['info'] = info1
obj['item'] = item
index = next((index for (index, item) in enumerate(global_sessions) if item["id"] == obj['id']), -1)
obj['info']['index'] = index
obj['info']['source_id'] = item.source_app_user_model_id
obj['info']['source_app'] = get_proc_name(item.source_app_user_model_id)
if (index == -1):
obj['info']['index'] = 0
global_sessions.insert(0, obj)
else:
global_sessions[index] = obj
return global_sessions
@app.route("/")
def root():
return send_from_directory('static', 'index.html')
@app.route("/sessions")
async def sessions():
data = await get_media_info()
return list(map(lambda x: x['info'], list(data)))
@app.route("/toggle")
async def toggle():
id = request.args.get("id")
action = request.args.get('action')
data = await get_media_info()
item = next((item for item in data if item["id"] == id), None)
if id is None:
return 400
if item == None:
return 404
handle = item['item']
if action:
if action == 'prev':
await handle.try_skip_previous_async()
if action == 'next':
await handle.try_skip_next_async()
else:
handle.try_toggle_play_pause_async()
data = await get_media_info()
return list(map(lambda x: x['info'], list(data)))