code refactoring

This commit is contained in:
sHa
2024-04-08 09:06:26 +03:00
parent 13475a7db4
commit f2a4978cf2
13 changed files with 269 additions and 183 deletions

View File

@@ -3,19 +3,67 @@ from appscript import app # type: ignore
class AppleMusic:
def __init__(self):
self.music_app = app("Music")
self.__music_app = app("Music")
self.__current_track = None
self.__player_position = None
self.get_current_track_info()
def get_current_track_info(self) -> tuple:
@property
def text(self) -> str:
return f"{self.name} - {self.artist}"
@property
def emoji(self) -> dict:
return {
"name": "headphones",
"name_with_colons": ":headphones:",
"icon": "🎧",
}
@property
def name(self) -> str:
if self.__current_track is not None:
return str(self.__current_track.name.get())
return ""
@property
def artist(self) -> str:
if self.__current_track is not None:
return str(self.__current_track.artist.get())
return ""
@property
def album(self) -> str:
if self.__current_track is not None:
return str(self.__current_track.album.get())
return ""
@property
def duration(self) -> int:
if self.__current_track is not None:
return int(self.__current_track.duration.get())
return 0
@property
def elapsed_time(self) -> float:
if self.__player_position is not None:
return self.__player_position
return 0
@property
def remaining_time(self) -> float:
return self.duration - self.elapsed_time
def get_current_track_info(self):
try:
current_track = self.music_app.current_track.get()
current_position = self.music_app.player_position.get()
track_duration = current_track.duration.get()
return (
current_track.name.get(),
current_track.artist.get(),
track_duration,
current_position,
)
self.__current_track = self.__music_app.current_track.get()
self.__player_position = self.__music_app.player_position.get()
except Exception as e:
print(f"Failed to get current track info: {e}")
return None, None, None, None
def get(self) -> dict:
return {
name: getattr(self, name)
for name in dir(self)
if not name.startswith("_") and not callable(getattr(self, name))
}

19
connectors/mattermost.py Normal file
View File

@@ -0,0 +1,19 @@
import requests
class MattermostConnector:
def __init__(self, connection_config) -> None:
self.url = connection_config.get("url", "https://mattermost.com")
self.token = connection_config.get("token", "")
self.connect(connection_config.get("user_id", "me"))
def connect(self, user_id):
self.headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json",
}
self.url = f"{self.url}/api/v4/users/{user_id}/status/custom"
def send(self, data):
response = requests.put(self.url, headers=self.headers, json=data)
if response.status_code != 200:
raise Exception(f"Failed to set Mattermost status: {response.content!r}")

View File

@@ -3,25 +3,35 @@ import random
import emoji
class Random:
class RandomConnector:
def __init__(self):
self.faker = Faker()
self.__faker = Faker()
def get_random_activity(self) -> tuple:
activity = self.faker.bs().capitalize() # Generate a random activity
emoji = self.get_random_emoji_name() # Get a random emoji
duration = random.randint(5, 60) # Duration in minutes
return activity, emoji, duration
@property
def text(self) -> str:
return self.__faker.bs().capitalize()
def get_random_emoji_name(self) -> str:
@property
def emoji(self) -> dict:
emoji_names = list(emoji.get_aliases_unicode_dict())
single_char_emoji_names = [
name for name in emoji_names if len(emoji.emojize(name)) == 1
]
random_emoji_name = random.choice(single_char_emoji_names)
return random_emoji_name
emoji_name = random.choice(single_char_emoji_names)
return {
"name": emoji_name.replace(":", ""),
"name_with_colons": emoji_name,
"icon": emoji.emojize(emoji_name),
}
def get_random_emoji(self) -> str:
random_emoji = emoji.emojize(self.get_random_emoji_name())
@property
def duration(self) -> int:
"""Return a random duration between 5 and 60 minutes in seconds"""
return random.randint(5, 60) * 60
return random_emoji
def get(self) -> dict:
return {
name: getattr(self, name)
for name in dir(self)
if not name.startswith("_") and not callable(getattr(self, name))
}

View File

@@ -2,29 +2,57 @@ import osascript # type: ignore
class Spotify:
def get_current_track_info(self) -> tuple:
def __init__(self):
self.__request_prefix = 'tell application "Spotify" to'
self.__request_current_track = "of current track as string"
self.name: str = ""
self.artist: str = ""
self.album: str = ""
self.duration: int = 0
self.elapsed_time: float = 0
self.get_current_track_info()
@property
def text(self) -> str:
return f"{self.name} - {self.artist}"
@property
def emoji(self) -> dict:
return {
"name": "headphones",
"name_with_colons": ":headphones:",
"icon": "🎧",
}
@property
def remaining_time(self) -> float:
return self.duration - self.elapsed_time
def get_current_track_info(self):
try:
name_code = 'tell application "Spotify" to name of current track as string'
name_code = f"{self.__request_prefix} name {self.__request_current_track}"
artist_code = (
'tell application "Spotify" to artist of current track as string'
f"{self.__request_prefix} artist {self.__request_current_track}"
)
album_code = f"{self.__request_prefix} album {self.__request_current_track}"
duration_code = (
'tell application "Spotify" to duration of current track as string'
f"{self.__request_prefix} duration {self.__request_current_track}"
)
elapsed_time_code = (
'tell application "Spotify" to player position as string'
elapsed_time_code = f"{self.__request_prefix} player position as string"
self.name = osascript.osascript(name_code)[1]
self.artist = osascript.osascript(artist_code)[1]
self.album = osascript.osascript(album_code)[1]
self.duration = round(int(osascript.osascript(duration_code)[1]) / 1000)
self.elapsed_time = float(
osascript.osascript(elapsed_time_code)[1].replace(",", ".")
)
name = osascript.osascript(name_code)[1]
artist = osascript.osascript(artist_code)[1]
duration = (
int(osascript.osascript(duration_code)[1]) / 1000
) # Convert duration from ms to s
elapsed_time = (
float(osascript.osascript(elapsed_time_code)[1].replace(',','.'))
) # Elapsed time in seconds
return name, artist, duration, elapsed_time
except Exception as e:
print(f"Failed to get current track info: {e}")
return None, None, None, None
def get(self) -> dict:
return {
name: getattr(self, name)
for name in dir(self)
if not name.startswith("_") and not callable(getattr(self, name))
}