|
1 | 1 | import sys, os |
2 | 2 | import json |
| 3 | +import argparse as argp |
| 4 | +import urllib.request as ul2 |
3 | 5 | from threading import Thread |
4 | 6 | from time import sleep |
5 | 7 |
|
|
16 | 18 | from geventwebsocket.handler import WebSocketHandler |
17 | 19 | # monkey.patch_all() |
18 | 20 |
|
| 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 | + |
19 | 31 | # C:\Users\[User]\AppData\Local\Packages\[PACKAGE ID]\LocalCache\Local\Netease\CloudMusic\webdata\file\history |
20 | 32 | # %appdata%/../local/netease/cloudmusic/webdata/file/ |
| 33 | +args = arguments.parse_args() |
21 | 34 | 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: |
24 | 37 | for p, d, f in os.walk(os.path.join(os.getenv("appdata"),"../Local/Packages/")): |
25 | 38 | if "CloudMusic" in d: |
26 | | - # print( os.path.join(p,d[0])) |
27 | 39 | history_dir = os.path.join(p, d[0]) + "\\webdata\\file\\" |
28 | 40 | break |
29 | | -if 'DESKTOP' in history_dir.upper(): |
| 41 | +if args.mode and 'DESKTOP' in args.mode: |
30 | 42 | if os.path.exists(os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")): |
31 | 43 | 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 |
32 | 47 | if not history_dir: |
33 | 48 | for p, d, f in os.walk(os.path.join(os.getenv("appdata"),"../Local/Packages/")): |
34 | 49 | if "CloudMusic" in d: |
35 | | - # print( os.path.join(p,d[0])) |
| 50 | + print("Found UWP version history file") |
36 | 51 | history_dir = os.path.join(p, d[0]) + "\\webdata\\file\\" |
37 | 52 | break |
38 | 53 | if not history_dir: |
39 | 54 | if os.path.exists(os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/")): |
| 55 | + print("Found DESKTOP version history file") |
40 | 56 | history_dir = os.path.join(os.getenv("appdata"),"../local/netease/cloudmusic/webdata/file/") |
41 | 57 |
|
42 | 58 | 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]") |
44 | 60 | os.system("pause") |
45 | 61 | exit(-1) |
46 | 62 |
|
@@ -133,12 +149,34 @@ def conn(ws): |
133 | 149 | ws.send("PING") |
134 | 150 | print("user disconnected") |
135 | 151 |
|
| 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 | + |
136 | 172 | if __name__ == "__main__": |
137 | 173 | on_file_change() |
138 | 174 | monitor = Thread(target=fileMonitor) |
139 | 175 | monitor.setDaemon(True) |
140 | 176 | monitor.start() |
| 177 | + subcounter = Thread(target=timedUpdate) |
| 178 | + subcounter.setDaemon(True) |
| 179 | + subcounter.start() |
141 | 180 | server = pywsgi.WSGIServer(('0.0.0.0', 9999), app, handler_class=WebSocketHandler) |
142 | 181 | server.serve_forever() |
143 | 182 | # app.run(host="0.0.0.0", debug=False, port="9999") |
144 | | - |
|
0 commit comments