-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
browsermediacontrol
executable file
·79 lines (70 loc) · 2.11 KB
/
browsermediacontrol
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
#!/usr/bin/python3
from pydbus import SessionBus
from gi.repository import GLib
import sys
import os
import argparse
import re
ICON_PLAY = "\uf144"
ICON_PAUSE = "\uf28b"
ICON_NEXT = "\uf152"
ICON_PREV = "\uf191"
ICON = ""
PATH=os.path.realpath(__file__)
TITLE_LENGTH = 25
MEDIA_ICONS = {
'apple': '\uf302',
'spotify': '\uf1bc',
'youtube': '\uf16a'
}
PATTERN = '(' + '|'.join(MEDIA_ICONS.keys()) + ')'
MEDIA_ICONS_REGEX = re.compile(PATTERN, re.MULTILINE)
parser = argparse.ArgumentParser()
parser.add_argument('--limit', type=int)
parser.add_argument('--volume', type=int)
parser.add_argument('--playpause', action="store_true")
parser.add_argument('--next', action="store_true")
parser.add_argument('--prev', action="store_true")
args= parser.parse_args()
bus = SessionBus()
try:
Player = bus.get(
"org.kde.plasma.browser_integration",
"/org/mpris/MediaPlayer2"
)
except GLib.Error:
exit()
def action(command, text):
return "%{A1:" + PATH + " --" + command + ":}" + text + "%{A}"
def truncate(text, max_len):
return text[:max_len] + "..." if len(text) > max_len and max_len > 0 else text
def get_media_icon(url):
match = MEDIA_ICONS_REGEX.findall(url)
if match:
if len(match) == 1:
return MEDIA_ICONS[match[0]]
return None
if Player.PlaybackStatus != "Stopped":
if args.volume != None:
vol = Player.Volume
Player.Volume = vol + (args.volume * 0.1)
exit()
if args.playpause:
Player.PlayPause()
exit()
if Player.PlaybackStatus=="Playing":
ICON = ICON_PAUSE
elif Player.PlaybackStatus=="Paused":
ICON = ICON_PLAY
if args.next:
Player.Next()
elif args.prev:
Player.Previous()
if args.limit:
TITLE_LENGTH = args.limit
title = Player.Metadata["xesam:title"]
media_icon = get_media_icon(Player.Metadata["xesam:url"].lower())
output = action("prev", ICON_PREV) + " " + action("playpause", ICON) + " " + action("next", ICON_NEXT) + " | " + ( f"{media_icon} " if media_icon else "" ) + truncate(title, TITLE_LENGTH)
print(output)
else:
print("")