rednotif.py
78 lines 2.7 kB view raw
1import requests 2from time import sleep 3from datetime import datetime 4from html import unescape 5 6def mksize(s): 7 suffixes = ["B", "KB", "MB", "GB", "TB"] 8 i = 0 9 while s > 1024: 10 s /= 1024 11 i += 1 12 return f"{s:.2f} {suffixes[i]}" 13 14 15def main(): 16 redacted = requests.Session() 17 with open("apikey", "r") as f: 18 redacted.headers.update({'Authorization': f.read().strip()}) 19 20 res = redacted.get("https://redacted.sh/ajax.php?action=notifications").json() 21 notifs = res["response"]["results"] 22 if not notifs: 23 return 24 25 for n in notifs: 26 n["notificationTime"] = datetime.fromisoformat(n["notificationTime"]) 27 28 with open("lastnotif", "r+") as f: 29 lastnotif = datetime.fromtimestamp(int(f.read().strip())) 30 f.seek(0) 31 f.write(str(int(notifs[0]["notificationTime"].timestamp()))) 32 f.truncate() 33 34 with open("webhook", "r") as f: 35 webhookurl = f.read().strip() 36 37 for n in notifs: 38 if n["notificationTime"] > lastnotif: 39 groupName = unescape(n["groupName"]) 40 groupYear = n["groupYear"] 41 groupId = n["groupId"] 42 torrentId = n["torrentId"] 43 torrentTags = "`" + n["torrentTags"].replace(" ", "` `") + "`" 44 fmt = n["format"] 45 encoding = n["encoding"] 46 media = n["media"] 47 if n["hasLog"]: 48 logScore = n["logScore"] 49 media += f" / Log ({logScore}%)" 50 if n["hasCue"]: 51 media += " / Cue" 52 if n["scene"]: 53 media += " / Scene" 54 size = n["size"] 55 remasterYear = n["remasterYear"] 56 remasterTitle = n["remasterTitle"] 57 if remasterTitle: 58 remasterTitle = " " + remasterTitle 59 wikiImage = n["wikiImage"] 60 61 # information not in the notification 62 groupInfo = redacted.get("https://redacted.sh/ajax.php?action=torrentgroup&id=" + str(groupId)).json()["response"]["group"] 63 artists = " & ".join(unescape(a["name"]) for a in groupInfo["musicInfo"]["artists"]) 64 65 requests.post(webhookurl, json={ 66 "embeds": [{ 67 "title": f"{artists} - {groupName} [{groupYear}]", 68 "url": f"https://redacted.sh/torrents.php?id={groupId}&torrentid={torrentId}#torrent{torrentId}", 69 "description": f"{fmt} / {encoding} / {media} / {remasterYear}{remasterTitle} - {mksize(size)}\n{torrentTags}", 70 "color": 0xff0000, 71 "thumbnail": {"url": wikiImage} 72 }] 73 }) 74 sleep(1) 75 76 77if __name__ == "__main__": 78 main()