-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (83 loc) · 2.77 KB
/
main.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
import time
import pygetwindow as gw
from pypresence import Presence
import win32con
import win32process
import win32ui
import win32gui
import win32api
import psutil
import requests
from json import load
try:
config = load(open("config.json"))
except FileNotFoundError:
print("config.json not found")
except Exception as e:
print(f"An error occurred: {e}")
exit()
RPC = Presence(client_id=config["client_id"])
RPC.connect()
def get_active_window_title():
try:
active_app = gw.getActiveWindow().title
return active_app
except ImportError:
return None
def read_image_file(image_path):
try:
with open(image_path, "rb") as image_file:
image_data = image_file.read()
return image_data
except Exception as e:
print(f"An error occurred: {e}")
return None
try:
print("Initiated.")
while True:
active_program = get_active_window_title()
title = str(active_program).rfind("-")
sliced_title = active_program[title + 1 :].strip()
try:
time.sleep(0.5)
pid1 = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())
process1 = psutil.Process(pid1[-1]).exe()
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)
large, small = win32gui.ExtractIconEx(process1, 0)
win32gui.DestroyIcon(large[0])
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, ico_x, ico_y)
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject(hbmp)
hdc.DrawIcon((0, 0), small[0])
hbmp.SaveBitmapFile(hdc, "save.png")
except ValueError or psutil.NoSuchProcess or ProcessLookupError as error:
print(error)
r = requests.post(
f"https://api.imgbb.com/1/upload?expiration=600&key={config['imgbb_api_key']}",
files={"image": open("save.png", "rb")},
)
url = r.json()["data"]["url"]
time.sleep(2)
if active_program:
activity = {
"state": f"Using {sliced_title}",
"details": config["activity"]["details"],
"large_image": url,
"large_text": "Watching!",
"small_image": config["activity"]["small_image_url"],
"small_text": config["activity"]["small_image_text"],
}
RPC.update(
**activity,
buttons=config["activity"]["buttons"],
)
else:
RPC.clear()
time.sleep(15) # Check every 15 seconds
except KeyboardInterrupt:
pass
finally:
RPC.clear()