mirror of
https://github.com/shadoll/playing_now_2_mm.git
synced 2025-12-20 04:27:51 +00:00
Add autodetection music player
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
MATTERMOST_ACCESS_TOKEN=
|
||||
MATTERMOST_SERVER_URL=https://my-mattermost.host
|
||||
MUSIC_APP=apple_music # or spotify
|
||||
MUSIC_APP=autodetect # or apple_music # or spotify
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
.env
|
||||
.venv
|
||||
__pycache__
|
||||
.mypy_cache
|
||||
.venv
|
||||
.pytest_cache
|
||||
53
music.py
53
music.py
@@ -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:
|
||||
def __init__(self, connector):
|
||||
self.mattermost = Mattermost()
|
||||
self.connector = connector()
|
||||
def __init__(self):
|
||||
load_dotenv()
|
||||
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:
|
||||
return self.connector.get_current_track_info()
|
||||
if self.connector:
|
||||
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
|
||||
|
||||
13
music_app.py
13
music_app.py
@@ -2,28 +2,21 @@ from datetime import datetime
|
||||
import time
|
||||
from music import Music
|
||||
from mattermost import Mattermost
|
||||
from connectors.apple_music import AppleMusic
|
||||
from connectors.spotify import Spotify
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
SLEEP_TIME = 3
|
||||
|
||||
MUSIC_APP = (
|
||||
os.getenv("MUSIC_APP", "apple_music").replace("_", " ").title().replace(" ", "")
|
||||
)
|
||||
print(f"Using 📀 {MUSIC_APP} connector")
|
||||
|
||||
def playing_now() -> tuple:
|
||||
music = Music(connector=globals()[MUSIC_APP])
|
||||
music = Music()
|
||||
return music.get_current_track_info()
|
||||
|
||||
|
||||
def set_now_playing(name, artist, duration):
|
||||
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:
|
||||
Mattermost().set_now_playing(name, artist, duration)
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ This is a Python application that fetches the currently playing track from eithe
|
||||
- Mattermost
|
||||
|
||||
## 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_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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user