mirror of
https://github.com/NohamR/AM-Exporter.git
synced 2026-05-25 12:17:14 +00:00
changes
This commit is contained in:
16
user/export.applescript
Normal file
16
user/export.applescript
Normal file
@@ -0,0 +1,16 @@
|
||||
tell application "Music"
|
||||
if it is running then
|
||||
if player state is playing then
|
||||
set pState to player state
|
||||
set pPosition to player position
|
||||
set cTrack to current track
|
||||
|
||||
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
|
||||
104
user/export.py
Normal file
104
user/export.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
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")
|
||||
|
||||
stdout_file = 'logfile.log'
|
||||
stderr_file = 'error_logfile.log'
|
||||
|
||||
sys.stdout = open(stdout_file, 'a')
|
||||
sys.stderr = open(stderr_file, 'a')
|
||||
|
||||
def printout(content):
|
||||
print(f"{datetime.now().strftime('%H:%M:%S')} : {content}", file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
|
||||
def printerr(content):
|
||||
print(f"{datetime.now().strftime('%H:%M:%S')} : An error occurred: {str(content)}", file=sys.stderr)
|
||||
sys.stderr.flush()
|
||||
|
||||
def get_current_song():
|
||||
try:
|
||||
output = subprocess.check_output(['osascript', 'export.applescript']).decode('utf-8').strip()
|
||||
return output
|
||||
except subprocess.CalledProcessError as e:
|
||||
printerr(e)
|
||||
return e
|
||||
|
||||
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]
|
||||
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
|
||||
|
||||
return (artwork_url, itunes_url, artist_url)
|
||||
|
||||
def post(currentsong):
|
||||
currentsong['user'] = USER
|
||||
currentsong['password'] = PASSWORD
|
||||
data = json.dumps(currentsong)
|
||||
try:
|
||||
r = requests.post(url+'/music/set', data=data, headers=headers)
|
||||
if r.status_code != 200:
|
||||
return r.status_code
|
||||
else :
|
||||
return r.text
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {str(e)}", file=sys.stderr)
|
||||
|
||||
url = "https://api.noh.am" #RUN WEB
|
||||
url = "http://127.0.0.1:5000" #DEV
|
||||
url = "http://0.0.0.0:3005"
|
||||
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
def main():
|
||||
persistendId = ''
|
||||
prevstatus = ''
|
||||
while True:
|
||||
# print('getting data..', file=sys.stdout)
|
||||
# currentsong = json.loads(str(get_current_song()).replace("'''", '"'))
|
||||
currentsong = json.loads(get_current_song())
|
||||
|
||||
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'])
|
||||
printout(f"{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'
|
||||
printout(f"{post({'status' : 'not playing'})}")
|
||||
timets = 5*60
|
||||
elif currentsong['status'] == 'not running' and prevstatus != 'not running':
|
||||
prevstatus = 'not running'
|
||||
printout(f"{post({'status' : 'not running'})}")
|
||||
timets = 5*60
|
||||
else:
|
||||
timets = 5*60
|
||||
|
||||
time.sleep(timets)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
9
user/install.sh
Executable file
9
user/install.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh -xe
|
||||
|
||||
echo --- Copy launch agent plist
|
||||
mkdir ~/Library/LaunchAgents/ || true
|
||||
cp -f music-exp.plist ~/Library/LaunchAgents/
|
||||
|
||||
echo --- Load launch agent
|
||||
launchctl load ~/Library/LaunchAgents/music-exp.plist
|
||||
echo --- INSTALL SUCCESS
|
||||
24
user/music-exp.plist
Normal file
24
user/music-exp.plist
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>KeepAlive</key>
|
||||
<true/>
|
||||
<key>Label</key>
|
||||
<string>am.noh.music-exp</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<!-- <string>/usr/bin/python3</string> -->
|
||||
<string>/Users/noham/miniconda3/envs/310/bin/python</string>
|
||||
<string>export.py</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
<key>StandardOutPath</key>
|
||||
<string>logfile.log</string>
|
||||
<key>StandardErrorPath</key>
|
||||
<string>error_logfile.log</string>
|
||||
<key>WorkingDirectory</key>
|
||||
<string>/Users/noham/Documents/GitHub/AppleMusicApi/user</string>
|
||||
</dict>
|
||||
</plist>
|
||||
15
user/test.py
15
user/test.py
@@ -10,7 +10,11 @@ USER = os.getenv("USER")
|
||||
PASSWORD = os.getenv("PASSWORD")
|
||||
|
||||
def get_current_song():
|
||||
return subprocess.check_output(['osascript', 'test.applescript']).decode('utf-8').strip()
|
||||
try:
|
||||
output = subprocess.check_output(['osascript', 'export.applescript']).decode('utf-8').strip()
|
||||
return output
|
||||
except subprocess.CalledProcessError as e:
|
||||
return e
|
||||
|
||||
def get_track_extras(song, artist, album):
|
||||
query = f"{song} {artist} {album}"
|
||||
@@ -20,7 +24,6 @@ def get_track_extras(song, artist, album):
|
||||
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 :
|
||||
@@ -29,7 +32,6 @@ def get_track_extras(song, artist, album):
|
||||
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)
|
||||
|
||||
@@ -43,8 +45,10 @@ def post(currentsong):
|
||||
else :
|
||||
return r.text
|
||||
|
||||
url = "http://192.168.1.58:3005" #RUN
|
||||
url = "https://api.noh.am" #RUN WEB
|
||||
url = "http://127.0.0.1:5000" #DEV
|
||||
url = "http://0.0.0.0:3005"
|
||||
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
def main():
|
||||
@@ -52,7 +56,8 @@ def main():
|
||||
prevstatus = ''
|
||||
while True:
|
||||
print('getting data..')
|
||||
currentsong = json.loads(str(get_current_song()).replace("'''", '"'))
|
||||
# currentsong = json.loads(str(get_current_song()).replace("'''", '"'))
|
||||
currentsong = json.loads(get_current_song())
|
||||
|
||||
if currentsong['status'] == 'playing':
|
||||
if currentsong['persistent ID'] != persistendId:
|
||||
|
||||
6
user/uninstall.sh
Executable file
6
user/uninstall.sh
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh -xe
|
||||
echo --- Unload launch agent
|
||||
launchctl unload ~/Library/LaunchAgents/music-exp.plist
|
||||
echo --- Remove launch agent plist
|
||||
rm -f ~/Library/LaunchAgents/music-exp.plist || true
|
||||
echo --- UNINSTALL SUCCESS
|
||||
Reference in New Issue
Block a user