File: discordrpc.py

package info (click to toggle)
darktable 5.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 64,644 kB
  • sloc: ansic: 367,957; cpp: 102,838; xml: 20,091; lisp: 15,092; sh: 3,767; javascript: 3,264; perl: 1,925; python: 1,551; ruby: 975; makefile: 543; asm: 46; sql: 38; awk: 21
file content (99 lines) | stat: -rw-r--r-- 3,007 bytes parent folder | download
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
99
#!/usr/bin/env python3

"""
This script automatically enables Discord Rich Presence whenever darktable is running and clears it when darktable closes.

How to Run:

Open a terminal where this script is

Install the required python package:
pip install psutil pypresence

Run the script:
python darktable_rpc.py

Keep the terminal running when you launch darktable, your Discord status will update automatically.
"""
import time
import psutil
from pypresence import Presence

# Discord Application ID
CLIENT_ID = '1437906050467631326'

PROCESS_NAMES = ['darktable', 'darktable.exe', 'darktable-bin']

def is_darktable_running():
    for proc in psutil.process_iter(['name']):
        try:
            if proc.info['name'] in PROCESS_NAMES:
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied):
            continue
    return False

def main():
    print("darktable Discord Rich Presence")
    print("Waiting for darktable to start...")
    
    rpc = None
    presence_active = False
    start_time = None
    
    try:
        while True:
            darktable_running = is_darktable_running()
            
            # darktable just started
            if darktable_running and not presence_active:
                print("darktable detected, activating Discord presence...")
                
                try:
                    rpc = Presence(CLIENT_ID)
                    rpc.connect()
                    
                    start_time = int(time.time())
                    
                    rpc.update(
                        state="Photo editing",
                        details="darktable",
                        large_image="darktable_icon",
                        large_text="darktable - photography workflow",
                        start=start_time
                    )
                    
                    presence_active = True
                    print("Discord presence active!")
                    
                except Exception as e:
                    print(f"Error connecting to Discord: {e}")
                    print("Make sure Discord is running.")
            
            # darktable just closed
            elif not darktable_running and presence_active:
                print("darktable closed. Clearing Discord presence...")
                
                try:
                    if rpc:
                        rpc.close()
                        rpc = None
                except Exception as e:
                    print(f"Error closing Discord connection: {e}")
                
                presence_active = False
                start_time = None
                print("Waiting for darktable to start again...")
            
            time.sleep(5)
            
    except KeyboardInterrupt:
        if rpc and presence_active:
            try:
                rpc.close()
            except:
                pass
        print("Discord Rich Presence stopped.")

if __name__ == "__main__":
    main()