Add autodetection music player

This commit is contained in:
sHa
2024-04-05 16:13:39 +03:00
parent 7b04f96fcf
commit 051b1b4e40
5 changed files with 55 additions and 18 deletions

View File

@@ -1,3 +1,3 @@
MATTERMOST_ACCESS_TOKEN= MATTERMOST_ACCESS_TOKEN=
MATTERMOST_SERVER_URL=https://my-mattermost.host MATTERMOST_SERVER_URL=https://my-mattermost.host
MUSIC_APP=apple_music # or spotify MUSIC_APP=autodetect # or apple_music # or spotify

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.env .env
.venv
__pycache__ __pycache__
.mypy_cache .mypy_cache
.venv .pytest_cache

View File

@@ -1,9 +1,52 @@
from mattermost import Mattermost import subprocess
from connectors.apple_music import AppleMusic
from connectors.spotify import Spotify
import os
from dotenv import load_dotenv
class Music: class Music:
def __init__(self, connector): def __init__(self):
self.mattermost = Mattermost() load_dotenv()
self.connector = connector() self.music_app = os.getenv('MUSIC_APP', 'autodetect')
if self.music_app == 'autodetect':
self.music_app = self.get_current_music_player()
self.connector = self.get_connector()
def get_connector(self):
if self.music_app == 'spotify':
return Spotify()
elif self.music_app == 'apple_music':
return AppleMusic()
else:
raise ValueError(f'Invalid music app: {self.music_app}')
def get_current_track_info(self) -> tuple: def get_current_track_info(self) -> tuple:
if self.connector:
return self.connector.get_current_track_info() return self.connector.get_current_track_info()
return None, None, None
@staticmethod
def get_current_music_player():
spotify_status = (
subprocess.check_output(
"osascript -e 'application \"Spotify\" is running'", shell=True
)
.decode()
.strip()
)
apple_music_status = (
subprocess.check_output(
"osascript -e 'application \"Music\" is running'", shell=True
)
.decode()
.strip()
)
if spotify_status == "true":
player = "Spotify"
elif apple_music_status == "true":
player = "Apple Music"
else:
player = None
# print(f"Detected 📀 player: {player}")
return player

View File

@@ -2,28 +2,21 @@ from datetime import datetime
import time import time
from music import Music from music import Music
from mattermost import Mattermost from mattermost import Mattermost
from connectors.apple_music import AppleMusic
from connectors.spotify import Spotify
from dotenv import load_dotenv from dotenv import load_dotenv
import os
load_dotenv() load_dotenv()
SLEEP_TIME = 3 SLEEP_TIME = 3
MUSIC_APP = (
os.getenv("MUSIC_APP", "apple_music").replace("_", " ").title().replace(" ", "")
)
print(f"Using 📀 {MUSIC_APP} connector")
def playing_now() -> tuple: def playing_now() -> tuple:
music = Music(connector=globals()[MUSIC_APP]) music = Music()
return music.get_current_track_info() return music.get_current_track_info()
def set_now_playing(name, artist, duration): def set_now_playing(name, artist, duration):
now = datetime.now().strftime("%H:%M:%S") now = datetime.now().strftime("%H:%M:%S")
print(f"{now} 🎧 {name} - {artist} ⏱️ {duration}") duration = int(duration) if duration else 0
print(f"{now} 🎧 {name} - {artist} ⏱️ {duration} seconds")
if name and artist and duration: if name and artist and duration:
Mattermost().set_now_playing(name, artist, duration) Mattermost().set_now_playing(name, artist, duration)

View File

@@ -14,7 +14,7 @@ This is a Python application that fetches the currently playing track from eithe
- Mattermost - Mattermost
## Environment Variables ## Environment Variables
`MUSIC_APP` - This variable determines which music service the application will fetch the currently playing track from. It can be either `apple_music` or `spotify`. `MUSIC_APP` - This variable determines which music service the application will fetch the currently playing track from. It can be set to `autodetect` to automatically detect the running music application.
`MATTERMOST_SERVER_URL` variable represents the URL of the Mattermost server. `MATTERMOST_SERVER_URL` variable represents the URL of the Mattermost server.
`MATTERMOST_ACCESS_TOKEN` the access token for the Mattermost API, which is obtained by generating a personal access token from the Mattermost user settings and is used to authenticate and authorize API requests to the Mattermost server. `MATTERMOST_ACCESS_TOKEN` the access token for the Mattermost API, which is obtained by generating a personal access token from the Mattermost user settings and is used to authenticate and authorize API requests to the Mattermost server.