sponsorblock.py (4263B)
1 import urllib.request 2 import urllib.parse 3 import sqlite3 4 import random 5 import string 6 import json 7 import sys 8 import os 9 10 if sys.argv[1] in ["submit", "stats", "username"]: 11 if not sys.argv[8]: 12 if os.path.isfile(sys.argv[7]): 13 with open(sys.argv[7]) as f: 14 uid = f.read() 15 else: 16 uid = "".join(random.choices(string.ascii_letters + string.digits, k=36)) 17 with open(sys.argv[7], "w") as f: 18 f.write(uid) 19 else: 20 uid = sys.argv[8] 21 22 opener = urllib.request.build_opener() 23 opener.addheaders = [("User-Agent", "mpv_sponsorblock/1.0 (https://github.com/po5/mpv_sponsorblock)")] 24 urllib.request.install_opener(opener) 25 26 if sys.argv[1] == "ranges" and not sys.argv[2]: 27 times = [] 28 try: 29 response = urllib.request.urlopen(sys.argv[3] + "/api/getVideoSponsorTimes?videoID=" + sys.argv[4]) 30 data = json.load(response) 31 for i, time in enumerate(data["sponsorTimes"]): 32 times.append(str(time[0]) + "," + str(time[1]) + "," + data["UUIDs"][i]) 33 print(":".join(times)) 34 except (TimeoutError, urllib.error.URLError) as e: 35 print("error") 36 except urllib.error.HTTPError as e: 37 if e.code == 404: 38 print("") 39 else: 40 print("error") 41 elif sys.argv[1] == "ranges": 42 conn = sqlite3.connect(sys.argv[2]) 43 conn.row_factory = sqlite3.Row 44 c = conn.cursor() 45 c.execute("SELECT startTime, endTime, votes, UUID FROM sponsorTimes WHERE videoID = ? AND shadowHidden = 0 AND votes > -1", (sys.argv[4],)) 46 times = [] 47 sponsors = c.fetchall() 48 best = list(sponsors) 49 dealtwith = [] 50 similar = [] 51 for sponsor_a in sponsors: 52 for sponsor_b in sponsors: 53 if sponsor_a is not sponsor_b and sponsor_a["startTime"] >= sponsor_b["startTime"] and sponsor_a["startTime"] <= sponsor_b["endTime"]: 54 similar.append([sponsor_a, sponsor_b]) 55 if sponsor_a in best: 56 best.remove(sponsor_a) 57 if sponsor_b in best: 58 best.remove(sponsor_b) 59 for sponsors_a in similar: 60 if sponsors_a in dealtwith: 61 continue 62 group = set(sponsors_a) 63 for sponsors_b in similar: 64 if sponsors_b[0] in group or sponsors_b[1] in group: 65 group.add(sponsors_b[0]) 66 group.add(sponsors_b[1]) 67 dealtwith.append(sponsors_b) 68 best.append(max(group, key=lambda x:x["votes"])) 69 for time in best: 70 times.append(str(time["startTime"]) + "," + str(time["endTime"]) + "," + time["UUID"]) 71 print(":".join(times)) 72 elif sys.argv[1] == "update": 73 try: 74 urllib.request.urlretrieve(sys.argv[3] + "/database.db", sys.argv[2] + ".tmp") 75 os.replace(sys.argv[2] + ".tmp", sys.argv[2]) 76 except PermissionError: 77 print("database update failed, file currently in use", file=sys.stderr) 78 exit(1) 79 except ConnectionResetError: 80 print("database update failed, connection reset", file=sys.stderr) 81 exit(1) 82 except TimeoutError: 83 print("database update failed, timed out", file=sys.stderr) 84 exit(1) 85 except urllib.error.URLError: 86 print("database update failed", file=sys.stderr) 87 exit(1) 88 elif sys.argv[1] == "submit": 89 try: 90 response = urllib.request.urlopen(sys.argv[3] + "/api/postVideoSponsorTimes?videoID=" + sys.argv[4] + "&startTime=" + sys.argv[5] + "&endTime=" + sys.argv[6] + "&userID=" + uid) 91 print("success") 92 except urllib.error.HTTPError as e: 93 print(e.code) 94 except: 95 print("error") 96 elif sys.argv[1] == "stats": 97 try: 98 if sys.argv[6]: 99 urllib.request.urlopen(sys.argv[3] + "/api/viewedVideoSponsorTime?UUID=" + sys.argv[5]) 100 if sys.argv[9]: 101 urllib.request.urlopen(sys.argv[3] + "/api/voteOnSponsorTime?UUID=" + sys.argv[5] + "&userID=" + uid + "&type=" + sys.argv[9]) 102 except: 103 pass 104 elif sys.argv[1] == "username": 105 try: 106 data = urllib.parse.urlencode({"userID": uid, "userName": sys.argv[9]}).encode() 107 req = urllib.request.Request(sys.argv[3] + "/api/setUsername", data=data) 108 urllib.request.urlopen(req) 109 except: 110 pass