refactoring needed; add plex (DBus) support
This commit is contained in:
parent
0b42bd4e05
commit
8333218d99
76
catbox.py
Normal file
76
catbox.py
Normal file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3.11
|
||||
|
||||
# Catbox Uploader
|
||||
|
||||
import requests
|
||||
import traceback
|
||||
import random
|
||||
import os
|
||||
import json
|
||||
|
||||
CATBOX_API_PATH = "https://catbox.moe/user/api.php"
|
||||
HEADERS = {
|
||||
'accept': '*/*',
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53',
|
||||
'Accept-Language': 'en-US,en;q=0.9,it;q=0.8,es;q=0.7',
|
||||
'referer': 'https://www.google.com/',
|
||||
'cookie': 'DSID=AAO-7r4OSkS76zbHUkiOpnI0kk-X19BLDFF53G8gbnd21VZV2iehu-w_2v14cxvRvrkd_NjIdBWX7wUiQ66f-D8kOkTKD1BhLVlqrFAaqDP3LodRK2I0NfrObmhV9HsedGE7-mQeJpwJifSxdchqf524IMh9piBflGqP0Lg0_xjGmLKEQ0F4Na6THgC06VhtUG5infEdqMQ9otlJENe3PmOQTC_UeTH5DnENYwWC8KXs-M4fWmDADmG414V0_X0TfjrYu01nDH2Dcf3TIOFbRDb993g8nOCswLMi92LwjoqhYnFdf1jzgK0'
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Catbox():
|
||||
|
||||
def generateRandomFileName(self, fileExt=''):
|
||||
|
||||
if len(fileExt) < 1:
|
||||
fileExt = 'db'
|
||||
|
||||
return f"{random.getrandbits(32)}.{fileExt}"
|
||||
|
||||
def __init__(self):
|
||||
self.PROXIES = None
|
||||
|
||||
try:
|
||||
with open(os.path.join(os.path.expanduser("~"), ".catprox"), 'r') as proxfile:
|
||||
self.PROXIES = json.loads("".join(str(x) for x in proxfile.readlines()))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def upload(self, filePath=''):
|
||||
global CATBOX_API_PATH
|
||||
global HEADERS
|
||||
try:
|
||||
if len(filePath) == 0:
|
||||
return False
|
||||
|
||||
if not(os.path.exists(filePath)):
|
||||
print(f"Could not find {filePath}")
|
||||
|
||||
fileExt = ''
|
||||
|
||||
if filePath.find(".") > 0:
|
||||
fileExt = "".join(filePath.split(".")[-1:])
|
||||
|
||||
with open(filePath, 'rb') as fileContents:
|
||||
postData = {
|
||||
'reqtype': 'fileupload',
|
||||
'userhash': ''
|
||||
}
|
||||
|
||||
r = requests.post(CATBOX_API_PATH, headers=HEADERS, proxies=self.PROXIES, data=postData, timeout=10, files={'fileToUpload': (self.generateRandomFileName(fileExt), fileContents)})
|
||||
|
||||
if r.status_code in [200, 301, 302]:
|
||||
return r.text.strip()
|
||||
else:
|
||||
print(f"FAILED - Status Code: {r.status_code}")
|
||||
return False
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
return False
|
||||
finally:
|
||||
try:
|
||||
fileContents.close()
|
||||
except:
|
||||
pass
|
@ -3,20 +3,75 @@
|
||||
import time
|
||||
import traceback
|
||||
import requests
|
||||
import dbus
|
||||
import re
|
||||
import random
|
||||
import discord_presence_priv
|
||||
import catbox
|
||||
from setproctitle import setproctitle
|
||||
|
||||
from pypresence import Presence, ActivityType
|
||||
|
||||
setproctitle("disc-presence")
|
||||
|
||||
class DBus:
|
||||
def __init__(self) -> None:
|
||||
self.session_bus = dbus.SessionBus()
|
||||
self.catbox = catbox.Catbox()
|
||||
try:
|
||||
self.player_dbus_proxy = self.session_bus.get_object('org.mpris.MediaPlayer2.playerctld',
|
||||
'/org/mpris/MediaPlayer2')
|
||||
self.player_interface = dbus.Interface(self.player_dbus_proxy,
|
||||
'org.freedesktop.DBus.Properties')
|
||||
self.interface_details = None
|
||||
self.plex_last = None
|
||||
self.plex_np = {
|
||||
'name': None,
|
||||
'details': None,
|
||||
'art': None,
|
||||
}
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def is_plexing(self) -> bool:
|
||||
try:
|
||||
self.interface_details = self.player_interface.GetAll('org.mpris.MediaPlayer2.Player').get('Metadata')
|
||||
if "app.plex.tv" in self.interface_details.get('xesam:url'):
|
||||
return True
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
return False
|
||||
|
||||
def get_now_playing(self) -> dict | bool:
|
||||
try:
|
||||
self.interface_details = self.player_interface.GetAll('org.mpris.MediaPlayer2.Player').get('Metadata')
|
||||
original_art = str(self.interface_details.get('mpris:artUrl', ''))
|
||||
self.plex_np = {
|
||||
'name': str(" ".join(self.interface_details.get('xesam:artist'))),
|
||||
'details': f'{str(self.interface_details.get('xesam:album', ''))} {str(self.interface_details.get('xesam:title'))}',
|
||||
}
|
||||
if not self.plex_last or (self.plex_np.get('name') == self.plex_last.get('name') and self.plex_np.get('details') == self.plex_last.get('details')):
|
||||
self.plex_np['art'] = self.catbox.upload(original_art.split("file://", maxsplit=1)[1])
|
||||
print(f"Path: {original_art.split("file://", maxsplit=0)[0]}")
|
||||
return self.plex_np
|
||||
except:
|
||||
self.plex_np = {
|
||||
'name': None,
|
||||
'details': None,
|
||||
'art': None,
|
||||
}
|
||||
print(traceback.format_exc())
|
||||
return False
|
||||
|
||||
|
||||
class DiscordPresence:
|
||||
def __init__(self):
|
||||
self.dbus = DBus()
|
||||
self.last_updated = None
|
||||
self.last_track = None
|
||||
self.client_id = discord_presence_priv.CLIENT_ID
|
||||
self.client_id_plex = discord_presence_priv.CLIENT_ID_PLEX
|
||||
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'
|
||||
@ -33,20 +88,71 @@ class DiscordPresence:
|
||||
lyrics_content = None
|
||||
lyrics_current_iteration = None
|
||||
chosen_image = random.choice([
|
||||
{
|
||||
'image': "cat",
|
||||
'label': "Exorzist",
|
||||
},
|
||||
{
|
||||
'image': "qu",
|
||||
'label': "Quietscheentchen",
|
||||
}
|
||||
'label': "quietscheentchen",
|
||||
},
|
||||
# {
|
||||
# 'image': "rooster",
|
||||
# 'label': ":3",
|
||||
# }
|
||||
])
|
||||
RPC = Presence(self.client_id, pipe=0)
|
||||
RPC.connect()
|
||||
RPC.connect()
|
||||
while True:
|
||||
try:
|
||||
now = time.time()
|
||||
plex = self.dbus.is_plexing()
|
||||
if plex:
|
||||
RPC.client_id = self.client_id_plex
|
||||
# if RPC.client_id != self.client_id_plex:
|
||||
# RPC = Presence(self.client_id_plex, pipe=0)
|
||||
# print(f"Connecting")
|
||||
# RPC.connect()
|
||||
# print(f"Connected")
|
||||
plex_current = self.dbus.plex_np
|
||||
plex_new = self.dbus.get_now_playing()
|
||||
|
||||
print(f"new: {plex_new}")
|
||||
if plex_current == plex_new:
|
||||
continue
|
||||
|
||||
|
||||
if self.last_updated and (now - self.last_updated < 5):
|
||||
# print(f"Skip: {self.last_track} & {(now - self.last_updated)}")
|
||||
time.sleep(0.7)
|
||||
continue
|
||||
|
||||
RPC.client_id = self.client_id
|
||||
# if RPC.client_id != self.client_id:
|
||||
# try:
|
||||
# RPC.close()
|
||||
# except:
|
||||
# pass
|
||||
# RPC = Presence(self.client_id, pipe=0)
|
||||
# RPC.connect()
|
||||
|
||||
print(RPC.update(
|
||||
details=plex_new.get('name'),
|
||||
state=plex_new.get('details'),
|
||||
large_image=plex_new.get('art'),
|
||||
# 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'),
|
||||
activity_type=ActivityType.WATCHING,
|
||||
buttons=[
|
||||
{
|
||||
"label": "Listen",
|
||||
"url": "https://codey.lol/radio"
|
||||
},]
|
||||
))
|
||||
|
||||
continue
|
||||
|
||||
|
||||
|
||||
request = requests.post(f'{self.api_url}/xc/', json=self.api_req_data,
|
||||
headers={
|
||||
'content-type': 'application/json; charset=utf-8'
|
||||
@ -138,17 +244,17 @@ class DiscordPresence:
|
||||
},]
|
||||
))
|
||||
|
||||
match chosen_image.get('image'):
|
||||
case "cat":
|
||||
chosen_image = {
|
||||
'image': "qu",
|
||||
'label': "Quietscheentchen"
|
||||
}
|
||||
case "qu":
|
||||
chosen_image = {
|
||||
'image': "cat",
|
||||
'label': "Exorzist",
|
||||
}
|
||||
# match chosen_image.get('image'):
|
||||
# case "rooster":
|
||||
# chosen_image = {
|
||||
# 'image': "qu",
|
||||
# 'label': "rubber duck :3"
|
||||
# }
|
||||
# case "qu":
|
||||
# chosen_image = {
|
||||
# 'image': "rooster",
|
||||
# 'label': ":3",
|
||||
# }
|
||||
except:
|
||||
print(traceback.format_exc())
|
||||
time.sleep(5)
|
||||
|
Loading…
x
Reference in New Issue
Block a user