This commit is contained in:
codey 2024-10-23 20:40:04 -04:00
parent 7c3742cc30
commit 125074dc9f

View File

@ -4,10 +4,14 @@ import time
import traceback
import requests
import re
import random
import discord_presence_priv
from setproctitle import setproctitle
from pypresence import Presence, ActivityType
setproctitle("disc-presence")
class DiscordPresence:
def __init__(self):
self.last_updated = None
@ -23,16 +27,30 @@ class DiscordPresence:
}
def loop(self):
try:
lyr_start_idx = 0
lyr_end_idx = 1
rand_msg = None
lyrics_content = None
lyrics_current_iteration = None
chosen_image = random.choice([
{
'image': "cat",
'label': "Exorzist",
},
{
'image': "qu",
'label': "Quietscheentchen",
}
])
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))
}, timeout=(2, 10))
request.raise_for_status()
data = request.json().get('response')
track_artist = data.get('artist')
@ -40,33 +58,73 @@ class DiscordPresence:
start_time = data.get('start')
end_time = data.get('end')
track = f"{data.get('artist')} - {data.get('title')}"
if self.last_track == track or self.last_updated and (now - self.last_updated < 10):
if self.last_updated and (now - self.last_updated < 4):
# print(f"Skip: {self.last_track} & {(now - self.last_updated)}")
time.sleep(5)
time.sleep(0.7)
continue
self.last_updated = now
self.last_track = track
if self.last_track and self.last_track == track:
lyr_start_idx = lyr_end_idx
lyr_end_idx += 1
elif self.last_track:
lyr_start_idx = 0
lyr_end_idx = 1
try:
rand_msg_request = requests.post(f'{self.api_url}/randmsg/',
# rand_msg_request = requests.post(f'{self.api_url}/randmsg/',
# headers={
# 'content-type': 'application/json; charset=utf-8'
# }, json={
# 'short': True,
# }, timeout=(1, 2))
# rand_msg = rand_msg_request.json().get('msg').strip()
# rand_msg = re.sub(r'(<b>|</b>)', '', re.sub(r'(<br>|<br/>|<br />|\n|\r\n)', ' ', rand_msg))
# if len(rand_msg) > 126:
# rand_msg = None
if not self.last_track or not self.last_track == track:
lyrics = requests.post(f'{self.api_url}/lyric_search/',
headers={
'content-type': 'application/json; charset=utf-8'
}, json={
'short': True,
}, timeout=(1, 2))
rand_msg = rand_msg_request.json().get('msg').strip()
rand_msg = re.sub(r'(<b>|</b>)', '', re.sub(r'(<br>|<br/>|<br />|\n|\r\n)', ' ', rand_msg))
if len(rand_msg) > 126:
rand_msg = None
'content-type': 'application/json; charset=utf-8',
},
json={
'a': track_artist,
's': track_title,
'src': 'IRC-SHARED',
}, timeout=(2,10))
lyrics.raise_for_status()
lyrics_content = lyrics.json().get("lyrics")
lyrics_current_iteration = " / ".join(lyrics_content.split("<br>")[lyr_start_idx:lyr_end_idx]).strip()
if not lyrics_current_iteration.strip():
lyr_start_idx = 0
lyr_end_idx = 1
if len(lyrics_current_iteration) > 128:
lyrics_current_iteration = lyrics_current_iteration.split(" / ", maxsplit=1)[0].strip()
except:
print("FAILED TO GET RANDMSG")
lyrics_content = lyrics_current_iteration = None
print("FAILED TO GET RANDMSG/LYRICS")
print(traceback.format_exc())
self.last_updated = now
self.last_track = track
print(RPC.update(
details=track_title,
state=track_artist,
large_image=f"{self.album_art_url}?t={now}",
large_text=f"{rand_msg}" if rand_msg else None,
# large_image="https://codey.lol/images/cat.png",
# large_text=f"{rand_msg}" if rand_msg else None,k
large_text=lyrics_current_iteration if lyrics_current_iteration else None,
small_image=f"https://codey.lol/images/{chosen_image.get('image')}.png",
small_text=chosen_image.get('label'),
start=start_time,
end=end_time,
activity_type=ActivityType.LISTENING,
@ -76,6 +134,18 @@ class DiscordPresence:
"url": "https://codey.lol/radio"
},]
))
match chosen_image.get('image'):
case "cat":
chosen_image = {
'image': "qu",
'label': "Quietscheentchen"
}
case "qu":
chosen_image = {
'image': "cat",
'label': "Exorzist",
}
except:
print(traceback.format_exc())
time.sleep(5)