beep
This commit is contained in:
parent
78b5cc40c2
commit
b9c6f14273
92
discord_presence.py
Normal file
92
discord_presence.py
Normal file
@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
import time
|
||||
import traceback
|
||||
import requests
|
||||
import re
|
||||
import discord_presence_priv
|
||||
|
||||
from pypresence import Presence, ActivityType
|
||||
|
||||
class DiscordPresence:
|
||||
def __init__(self):
|
||||
self.last_updated = None
|
||||
self.last_track = None
|
||||
self.client_id = discord_presence_priv.CLIENT_ID
|
||||
self.api_key = discord_presence_priv.API_KEY
|
||||
self.api_url = 'https://api.codey.lol'
|
||||
self.album_art_url = 'https://codey.lol/images/radio_art.jpg'
|
||||
self.api_req_data = {
|
||||
'bid': 0,
|
||||
'cmd': 'radio_metadata',
|
||||
'key': f'Bearer {self.api_key}',
|
||||
}
|
||||
def loop(self):
|
||||
try:
|
||||
RPC = Presence(self.client_id, pipe=0)
|
||||
RPC.connect()
|
||||
while True:
|
||||
try:
|
||||
rand_msg = None
|
||||
now = time.time()
|
||||
request = requests.post(f'{self.api_url}/xc/', json=self.api_req_data,
|
||||
headers={
|
||||
'content-type': 'application/json; charset=utf-8'
|
||||
}, timeout=(2, 5))
|
||||
request.raise_for_status()
|
||||
data = request.json().get('response')
|
||||
track_artist = data.get('artist')
|
||||
track_title = data.get('title')
|
||||
track = f"{data.get('artist')} - {data.get('title')}"
|
||||
if self.last_track == track or self.last_updated and (now - self.last_updated < 10):
|
||||
print(f"Skip: {self.last_track} & {(now - self.last_updated)}")
|
||||
time.sleep(5)
|
||||
continue
|
||||
self.last_updated = now
|
||||
self.last_track = track
|
||||
|
||||
try:
|
||||
rand_msg_request = requests.post(f'{self.api_url}/randmsg/',
|
||||
headers={
|
||||
'content-type': 'application/json; charset=utf-8'
|
||||
}, json={
|
||||
'short': True,
|
||||
}, timeout=(1, 2))
|
||||
print(f"Rand msg text: {rand_msg_request.text}")
|
||||
rand_msg = rand_msg_request.json().get('msg').strip()
|
||||
rand_msg = re.sub(r'(<b>|</b>)', '', re.sub(r'(<br>|<br/>|<br />)', ' ', rand_msg))
|
||||
if len(rand_msg) > 126:
|
||||
rand_msg = None
|
||||
except:
|
||||
print("FAILED TO GET RANDMSG")
|
||||
print(traceback.format_exc())
|
||||
|
||||
print(RPC.update(
|
||||
details=track_title,
|
||||
state=f"by {track_artist}",
|
||||
large_image=f"{self.album_art_url}?{now}",
|
||||
large_text=f"~ {rand_msg}" if rand_msg else None,
|
||||
activity_type=ActivityType.LISTENING,
|
||||
buttons=[
|
||||
{
|
||||
"label": "Site",
|
||||
"url": "https://codey.lol"
|
||||
},]
|
||||
))
|
||||
print(f"Track: {track}")
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
time.sleep(5)
|
||||
continue
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
def __init__():
|
||||
discord_presence = DiscordPresence()
|
||||
discord_presence.loop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
__init__()
|
||||
|
Loading…
x
Reference in New Issue
Block a user