-
Notifications
You must be signed in to change notification settings - Fork 2
/
playing.py
executable file
·100 lines (89 loc) · 2.69 KB
/
playing.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
#!/usr/bin/python
import json
from os.path import expanduser
import sys
import argparse
home = expanduser("~")
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description="Parses and print Google Play Music Desktop Player song info")
def parseJson():
try:
with open(home + '/.config/Google Play Music Desktop Player/json_store/playback.json') as f:
data = f.read()
except:
with open(home + '/GPMDP_STORE/playback.json') as f:
data = f.read()
return json.loads(data)
def getSong(data):
return data['song']['title']
def getAlbum(data):
return data['song']['album']
def getArtist(data):
return data['song']['artist']
def convert_time(ms):
x = ms / 1000
x % 60
m, s = divmod(x, 60)
return "%d:%02d" % (m, s)
def getProgress(data):
cur = data['time']['current']
total = data['time']['total']
return convert_time(cur) + "/" + convert_time(total)
def parseLayout(layout):
displaystr = ""
for i in layout:
if i == 't':
displaystr += getSong(data)
elif i == 'a':
displaystr += getAlbum(data)
elif i == 'A':
displaystr += getArtist(data)
elif i == 'p':
displaystr += getProgress(data)
elif i == '-':
displaystr += " - "
return displaystr
def truncate(displaystr, trunclen):
if len(displaystr) > trunclen:
displaystr = displaystr[:trunclen]
displaystr += '...'
if ('(' in displaystr) and (')' not in displaystr):
displaystr += ')'
return displaystr
def run(data, layout, notplaying, trunclen):
displaystr = ""
if data['playing']:
displaystr = parseLayout(layout)
else:
sys.stdout.write("")
if sys.version[0] == '2':
displaystr = displaystr.encode('utf-8')
if not displaystr and notplaying:
print("Not Playing")
else:
if trunclen is None:
print(displaystr)
else:
print(truncate(displaystr, trunclen))
parser.add_argument("-l",
"--layout",
action="store",
metavar="layout",
help="t = Song Title\na = Song Album\nA = Artist Name\np = Track time progess\n- = Spacer\nExample: t-a-A-p",
)
parser.add_argument("-t",
"--trunclen",
metavar="trunclen",
type=int,
help="Truncate the output"
)
parser.add_argument("--not-playing",
action="store_true",
dest="notplaying",
help="Display the text 'Not Playing' when no music is playing",
)
args = parser.parse_args()
data = parseJson()
try:
run(data, args.layout, args.notplaying, args.trunclen)
except:
run(data, "t-a-A-p", False)