Skip to content

Commit 2b3f278

Browse files
committed
Major changes: Use argparse for arguments
Add: Bilibili subscriber counter w/ formats
1 parent 156a2d8 commit 2b3f278

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

file_watcher.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys, os
22
import json
3+
import argparse as argp
4+
import urllib.request as ul2
35
from threading import Thread
46
from time import sleep
57

@@ -16,31 +18,45 @@
1618
from geventwebsocket.handler import WebSocketHandler
1719
# monkey.patch_all()
1820

21+
arguments = argp.ArgumentParser()
22+
mutex = arguments.add_mutually_exclusive_group(required=True)
23+
mutex.add_argument("-m", "--mode", type=str, help="Choose from UWP or DESKTOP version of Netease Cloud music")
24+
mutex.add_argument("-H", "--history", type=str, help="Play history file path")
25+
# Bilibili subcount arguments
26+
group = arguments.add_argument_group()
27+
group.add_argument("--vmid", type=str, help="Track bilibili account subscriber count(uses uid, not live channel id)")
28+
group.add_argument("-o", "--out-dir", type=str, help="Text output for obs to load", default="./out.txt")
29+
group.add_argument("-f", "--format", type=str, help="Format for output, use $c for current subscriber count", default="Subscriber count: $c\nGoal: 1000")
30+
1931
# C:\Users\[User]\AppData\Local\Packages\[PACKAGE ID]\LocalCache\Local\Netease\CloudMusic\webdata\file\history
2032
# %appdata%/../local/netease/cloudmusic/webdata/file/
33+
args = arguments.parse_args()
2134
terminated = False
22-
history_dir = None if len(sys.argv) == 1 else sys.argv[1]
23-
if 'UWP' in history_dir.upper():
35+
history_dir = None
36+
if args.mode and 'UWP' in args.mode:
2437
for p, d, f in os.walk(os.path.join(os.getenv("appdata"),"../Local/Packages/")):
2538
if "CloudMusic" in d:
26-
# print( os.path.join(p,d[0]))
2739
history_dir = os.path.join(p, d[0]) + "\\webdata\\file\\"
2840
break
29-
if 'DESKTOP' in history_dir.upper():
41+
if args.mode and 'DESKTOP' in args.mode:
3042
if os.path.exists(os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")):
3143
history_dir = os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")
44+
if not args.mode:
45+
if args.history:
46+
history_dir = args.history
3247
if not history_dir:
3348
for p, d, f in os.walk(os.path.join(os.getenv("appdata"),"../Local/Packages/")):
3449
if "CloudMusic" in d:
35-
# print( os.path.join(p,d[0]))
50+
print("Found UWP version history file")
3651
history_dir = os.path.join(p, d[0]) + "\\webdata\\file\\"
3752
break
3853
if not history_dir:
3954
if os.path.exists(os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")):
55+
print("Found DESKTOP version history file")
4056
history_dir = os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")
4157

4258
if not history_dir or history_dir.upper() in ['UWP', 'DESKTOP']:
43-
print("Cannot find UWP Netease Cloudmusic history json directory, please specify by using", sys.argv[0], "[history json directory]")
59+
print("Cannot find UWP Netease Cloudmusic history json directory, please specify by using", sys.argv[0], "[-H history json directory]")
4460
os.system("pause")
4561
exit(-1)
4662

@@ -133,12 +149,34 @@ def conn(ws):
133149
ws.send("PING")
134150
print("user disconnected")
135151

152+
def timedUpdate():
153+
global args
154+
format_str = args.format.replace("\\n", '\n') if args.format else None
155+
while True and args.vmid and args.format:
156+
try:
157+
resp = ul2.urlopen("https://api.bilibili.com/x/relation/stat?vmid=" + args.vmid)
158+
stat = resp.read().decode("utf-8", "ignore")
159+
stat = json.loads(stat)
160+
resp.close()
161+
stat = stat["data"]["follower"]
162+
if os.path.exists(args.out_dir):
163+
os.remove(args.out_dir)
164+
with open(args.out_dir, "w", encoding="utf8") as out:
165+
out.write(format_str.replace("$c", str(stat)))
166+
except Exception as e:
167+
print(e)
168+
finally:
169+
sleep(20)
170+
pass
171+
136172
if __name__ == "__main__":
137173
on_file_change()
138174
monitor = Thread(target=fileMonitor)
139175
monitor.setDaemon(True)
140176
monitor.start()
177+
subcounter = Thread(target=timedUpdate)
178+
subcounter.setDaemon(True)
179+
subcounter.start()
141180
server = pywsgi.WSGIServer(('0.0.0.0', 9999), app, handler_class=WebSocketHandler)
142181
server.serve_forever()
143182
# app.run(host="0.0.0.0", debug=False, port="9999")
144-

0 commit comments

Comments
 (0)