2024-10-05 08:59:12 -04:00
|
|
|
#!/usr/bin/env python3.12
|
|
|
|
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
import requests
|
|
|
|
import re
|
2024-10-23 20:40:04 -04:00
|
|
|
import random
|
2024-10-05 08:59:12 -04:00
|
|
|
import discord_presence_priv
|
2024-10-23 20:40:04 -04:00
|
|
|
from setproctitle import setproctitle
|
2024-10-05 08:59:12 -04:00
|
|
|
|
|
|
|
from pypresence import Presence, ActivityType
|
|
|
|
|
2024-10-23 20:40:04 -04:00
|
|
|
setproctitle("disc-presence")
|
|
|
|
|
2024-10-05 08:59:12 -04:00
|
|
|
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:
|
2024-10-23 20:40:04 -04:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
])
|
2024-10-05 08:59:12 -04:00
|
|
|
RPC = Presence(self.client_id, pipe=0)
|
2024-10-23 20:40:04 -04:00
|
|
|
RPC.connect()
|
2024-10-05 08:59:12 -04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
now = time.time()
|
|
|
|
request = requests.post(f'{self.api_url}/xc/', json=self.api_req_data,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/json; charset=utf-8'
|
2024-10-23 20:40:04 -04:00
|
|
|
}, timeout=(2, 10))
|
2024-10-05 08:59:12 -04:00
|
|
|
request.raise_for_status()
|
|
|
|
data = request.json().get('response')
|
|
|
|
track_artist = data.get('artist')
|
|
|
|
track_title = data.get('title')
|
2024-10-08 06:28:02 -04:00
|
|
|
start_time = data.get('start')
|
|
|
|
end_time = data.get('end')
|
2024-10-05 08:59:12 -04:00
|
|
|
track = f"{data.get('artist')} - {data.get('title')}"
|
2024-10-24 16:00:28 -04:00
|
|
|
if self.last_updated and (now - self.last_updated < 5):
|
2024-10-08 06:28:02 -04:00
|
|
|
# print(f"Skip: {self.last_track} & {(now - self.last_updated)}")
|
2024-10-23 20:40:04 -04:00
|
|
|
time.sleep(0.7)
|
2024-10-05 08:59:12 -04:00
|
|
|
continue
|
2024-10-23 20:40:04 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-05 08:59:12 -04:00
|
|
|
|
|
|
|
try:
|
2024-10-23 20:40:04 -04:00
|
|
|
# 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={
|
|
|
|
'a': track_artist,
|
|
|
|
's': track_title,
|
2024-10-24 16:00:28 -04:00
|
|
|
'src': 'WEB-RADIO',
|
2024-10-23 20:40:04 -04:00
|
|
|
}, 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-05 08:59:12 -04:00
|
|
|
except:
|
2024-10-24 16:00:28 -04:00
|
|
|
try:
|
|
|
|
lyrics_current_iteration = " / ".join(lyrics_content.split("<br>")[0]).strip()
|
|
|
|
except:
|
|
|
|
lyrics_content = lyrics_current_iteration = None
|
|
|
|
print("FAILED TO GET RANDMSG/LYRICS")
|
|
|
|
print(traceback.format_exc())
|
2024-10-05 08:59:12 -04:00
|
|
|
|
2024-10-23 20:40:04 -04:00
|
|
|
self.last_updated = now
|
|
|
|
self.last_track = track
|
|
|
|
|
2024-10-05 08:59:12 -04:00
|
|
|
print(RPC.update(
|
|
|
|
details=track_title,
|
2024-10-08 06:28:02 -04:00
|
|
|
state=track_artist,
|
|
|
|
large_image=f"{self.album_art_url}?t={now}",
|
2024-10-23 20:40:04 -04:00
|
|
|
# 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'),
|
2024-10-08 06:28:02 -04:00
|
|
|
start=start_time,
|
|
|
|
end=end_time,
|
2024-10-05 08:59:12 -04:00
|
|
|
activity_type=ActivityType.LISTENING,
|
|
|
|
buttons=[
|
|
|
|
{
|
2024-10-08 06:28:02 -04:00
|
|
|
"label": "Listen",
|
|
|
|
"url": "https://codey.lol/radio"
|
2024-10-05 08:59:12 -04:00
|
|
|
},]
|
|
|
|
))
|
2024-10-23 20:40:04 -04:00
|
|
|
|
|
|
|
match chosen_image.get('image'):
|
|
|
|
case "cat":
|
|
|
|
chosen_image = {
|
|
|
|
'image': "qu",
|
|
|
|
'label': "Quietscheentchen"
|
|
|
|
}
|
|
|
|
case "qu":
|
|
|
|
chosen_image = {
|
|
|
|
'image': "cat",
|
|
|
|
'label': "Exorzist",
|
|
|
|
}
|
2024-10-05 08:59:12 -04:00
|
|
|
except:
|
|
|
|
print(traceback.format_exc())
|
|
|
|
time.sleep(5)
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
print(traceback.format_exc())
|
2024-10-08 06:28:02 -04:00
|
|
|
try:
|
|
|
|
self.loop()
|
|
|
|
except:
|
|
|
|
pass
|
2024-10-05 08:59:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__():
|
|
|
|
discord_presence = DiscordPresence()
|
|
|
|
discord_presence.loop()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
__init__()
|
|
|
|
|