mirror of
https://github.com/NohamR/AM-Exporter.git
synced 2026-05-25 12:17:14 +00:00
push
This commit is contained in:
57
user/test.applescript
Normal file
57
user/test.applescript
Normal file
@@ -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''', '''persistent ID''': '''" & persistent ID of cTrack & "''', '''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
|
||||
80
user/test.py
Normal file
80
user/test.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import subprocess
|
||||
import time
|
||||
import json
|
||||
import requests
|
||||
from pprint import pprint
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
USER = os.getenv("USER")
|
||||
PASSWORD = os.getenv("PASSWORD")
|
||||
|
||||
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()
|
||||
if json_data["resultCount"] == 1:
|
||||
result = json_data["results"][0]
|
||||
pprint(result)
|
||||
elif json_data["resultCount"] > 1:
|
||||
result = json_data["results"][0]
|
||||
else :
|
||||
result = ''
|
||||
|
||||
artwork_url = result["artworkUrl100"] if result else None
|
||||
itunes_url = result["trackViewUrl"] if result else None
|
||||
artist_url = result["artistViewUrl"] if result else None
|
||||
# album_url = result["collectionViewUrl"] if result else None
|
||||
|
||||
return (artwork_url, itunes_url, artist_url)
|
||||
|
||||
def post(currentsong):
|
||||
currentsong['user'] = USER
|
||||
currentsong['password'] = PASSWORD
|
||||
data = json.dumps(currentsong)
|
||||
r = requests.post(url+'/music/set', data=data, headers=headers)
|
||||
if r.status_code != 200:
|
||||
return r.status_code
|
||||
else :
|
||||
return r.text
|
||||
|
||||
url = "http://127.0.0.1:5000"
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
def main():
|
||||
persistendId = ''
|
||||
prevstatus = ''
|
||||
while True:
|
||||
print('getting data..')
|
||||
currentsong = json.loads(str(get_current_song()).replace("'''", '"'))
|
||||
|
||||
if currentsong['status'] == 'playing':
|
||||
if currentsong['persistent ID'] != persistendId:
|
||||
persistendId = currentsong['persistent ID']
|
||||
currentsong['timestamp'] = time.time()
|
||||
(currentsong['artwork_url'], currentsong['itunes_url'], currentsong['artist_url']) = get_track_extras(currentsong['name'], currentsong['artist'], currentsong['album'])
|
||||
print(post(currentsong))
|
||||
timets = float(currentsong['duration'].replace(",", "."))-float(currentsong['pPosition'].replace(",", ".")) + 3
|
||||
prevstatus = 'playing'
|
||||
elif currentsong['status'] == 'not playing' and prevstatus != 'not playing':
|
||||
prevstatus = 'not playing'
|
||||
print('not playing')
|
||||
print(post({'status' : 'not playing'}))
|
||||
timets = 5*60
|
||||
elif currentsong['status'] == 'not running' and prevstatus != 'not running':
|
||||
prevstatus = 'not running'
|
||||
print('not running')
|
||||
print(post({'status' : 'not running'}))
|
||||
timets = 5*60
|
||||
else:
|
||||
timets = 5*60
|
||||
|
||||
time.sleep(timets)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
17
user/testpost.py
Normal file
17
user/testpost.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
url = "http://127.0.0.1:5000"
|
||||
|
||||
payload = {
|
||||
'key1': 'value1',
|
||||
'key2': 'value2'
|
||||
}
|
||||
|
||||
json_payload = json.dumps(payload)
|
||||
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
r = requests.post(url+'/set', data=json_payload, headers=headers)
|
||||
r = requests.get(url+'/get')
|
||||
print(r.text)
|
||||
Reference in New Issue
Block a user