forked from XtremePrime/RetroAchievements-Discord-Presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRARPC.py
89 lines (71 loc) · 3.07 KB
/
RARPC.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
import argparse
from colorama import Fore, Style, init
import configparser
from datetime import datetime
from pprint import pprint
from pypresence import Presence
import re
import requests
import time
init(autoreset=True)
def get_data(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print(Fore.RED + f"Failed to fetch data from {url}, status code: {response.status_code}")
return None
def sanitize_console_name(console_name):
sanitized_name = re.sub('[^0-9a-zA-Z]+', '', console_name)
return sanitized_name.lower()
def update_presence(RPC, data, game_data, start_time, username):
release_date = datetime.strptime(game_data['Released'], '%B %d, %Y')
year_of_release = release_date.year
details = f"{game_data['GameTitle']} ({year_of_release})"
RPC.update(
state=data["RichPresenceMsg"],
details=details,
start=start_time,
large_image="ra_logo",
large_text=f"Released {game_data['Released']}, Developed by {game_data['Developer']}, Published by {game_data['Publisher']}",
small_image=sanitize_console_name(game_data['ConsoleName']),
small_text=game_data['ConsoleName'],
buttons=[{"label": "View RA Profile", "url": f"https://retroachievements.org/user/{username}"}]
)
def main():
config = configparser.ConfigParser()
config.read('config.ini')
username = config.get('DISCORD', 'username')
api_key = config.get('DISCORD', 'api_key')
client_id = config.get('DISCORD', 'client_id') if config.has_option('DISCORD', 'client_id') else "1249693940299333642"
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true', help='Print debug information')
parser.add_argument('--fetch', type=int, default=30, help='Time to sleep before fetches in seconds')
args = parser.parse_args()
profile_url = f"https://retroachievements.org/API/API_GetUserProfile.php?u={username}&y={api_key}&z={username}"
start_time = int(time.time())
RPC = Presence(client_id)
print(Fore.CYAN + "Connecting to Discord App...")
RPC.connect()
print(Fore.MAGENTA + "Connected!")
while True:
print(Fore.CYAN + f"Fetching {username}'s RetroAchievements activity...")
data = get_data(profile_url)
if data is None:
break
print(Fore.MAGENTA + f"Result: {data['RichPresenceMsg']}")
game_params = f"?z={username}&y={api_key}&i={data['LastGameID']}"
game_url = f"https://retroachievements.org/API/API_GetGame.php{game_params}"
print(Fore.CYAN + "Fetching game data...")
game_data = get_data(game_url)
if game_data is None:
break
print(Fore.MAGENTA + f"Result: {game_data['GameTitle']}")
if args.debug:
print(Fore.YELLOW + "Debug game data:")
pprint(game_data)
update_presence(RPC, data, game_data, start_time, username)
print(Fore.CYAN + f"Sleeping for {args.fetch} seconds...")
time.sleep(args.fetch)
if __name__ == "__main__":
main()