diff --git a/test.applescript b/test.applescript new file mode 100644 index 0000000..6abb653 --- /dev/null +++ b/test.applescript @@ -0,0 +1,57 @@ +tell application "Music" + if it is running then + if player state is playing then + -- return name of current track & " by " & artist of current track + + -- working : + -- return raw data of artwork 1 of current track + + -- return properties of sources + + -- return properties of current playlist + + -- set currentPlaylist to container of current track + -- set currentPlaylistID to persistent ID of currentPlaylist + + -- return properties of currentPlaylist + + -- return properties of current track + -- name + -- time + -- duration + -- artist + -- album artist + -- composer + -- album + -- genre + -- played count + + set pState to player state + set pPosition to player position + + set cTrack to current track + -- return raw data of artwork 1 of current track + -- set trackInfo to "{'''name''': '''" & name of cTrack & "''', + -- '''time''': '''" & time of cTrack & "''', + -- '''duration''': '''" & duration of cTrack & "''', + -- '''artist''': '''" & artist of cTrack & "''', + -- '''album artist''': '''" & album artist of cTrack & "''', + -- '''composer''': '''" & composer of cTrack & "''', + -- '''album''': '''" & album of cTrack & "''', + -- '''genre''': '''" & genre of cTrack & "''', + -- '''played count''': '''" & played count of cTrack & "''', + -- '''pState''' = '''" & pState & "''', + -- '''pPosition''' = '''" & pPosition & "''' + -- }" + + -- set trackInfo to "{'''name''': '''" & name of cTrack & "''', '''time''': '''" & time of cTrack & "''', '''duration''': '''" & duration of cTrack & "''', '''artist''': '''" & artist of cTrack & "''', '''album artist''': '''" & album artist of cTrack & "''', '''composer''': '''" & composer of cTrack & "''', '''album''': '''" & album of cTrack & "''', '''genre''': '''" & genre of cTrack & "''', '''played count''': '''" & played count of cTrack & "''' , '''pState''' = '''" & pState & "''', '''pPosition''' = '''" & pPosition & "'''}" + + set trackInfo to "{'''status''': '''playing''', '''name''': '''" & name of cTrack & "''', '''time''': '''" & time of cTrack & "''', '''duration''': '''" & duration of cTrack & "''', '''artist''': '''" & artist of cTrack & "''', '''album artist''': '''" & album artist of cTrack & "''', '''composer''': '''" & composer of cTrack & "''', '''album''': '''" & album of cTrack & "''', '''genre''': '''" & genre of cTrack & "''', '''played count''': '''" & played count of cTrack & "''', '''pState''' : '''" & pState & "''', '''pPosition''' : '''" & pPosition & "''' }" + return trackInfo + else + return "{'''status''' : '''not playing'''}" + end if + else + return "{'''status''' : '''not running'''}" + end if +end tell \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..68e182c --- /dev/null +++ b/test.py @@ -0,0 +1,56 @@ +import subprocess +import time +import json +import requests +from pprint import pprint + +def get_current_song(): + return subprocess.check_output(['osascript', 'test.applescript']).decode('utf-8').strip() + +def get_track_extras(song, artist, album): + query = f"{song} {artist} {album}" + params = {"media": "music", "entity": "song", "term": query} + + r = requests.get("https://itunes.apple.com/search", params=params) + json_data = r.json() + # print('json_data: ', json_data) + if json_data["resultCount"] == 1: + result = json_data["results"][0] + elif json_data["resultCount"] > 1: + pass + else : + pass + + artwork_url = result["artworkUrl100"] if result else None + itunes_url = result["trackViewUrl"] if result else None + artist_url = result["artistViewUrl"] if result else None + + return (artwork_url, itunes_url, artist_url) + +currentsong = json.loads(str(get_current_song()).replace("'''", '"')) + +if currentsong['status'] == 'playing': + (artwork_url, itunes_url, artist_url) = get_track_extras(currentsong['name'], currentsong['artist'], currentsong['album']) + currentsong['artwork_url'] = artwork_url + currentsong['itunes_url'] = itunes_url + currentsong['artist_url'] = artist_url + + pprint(currentsong) +elif currentsong['status'] == 'not playing': + print('not playing') +elif currentsong['status'] == 'not running': + print('not running') +else: + pass + +# def main(): +# while True: +# current_song = get_current_song() +# if current_song: +# print("Currently listening to:", current_song) +# else: +# print("No music is playing.") +# time.sleep(5) # Check every 5 seconds + +# if __name__ == "__main__": +# main()