#!/bin/bash # GifTag Medya Helper - macOS # Usage: chmod +x giftag_medya.command && ./giftag_medya.command set +e SID="mac_helper" BASE_URL="${GIFTAG_ESP_URL:-}" LAST_KEY="" normalize_url() { local value="$1" [ -z "$value" ] && return case "$value" in http://*|https://*) printf "%s" "${value%/}" ;; *) printf "http://%s" "${value%/}" ;; esac } test_giftag() { local url="$1" curl -fsS --max-time 2 "$url/api/now_playing_status" >/dev/null 2>&1 || \ curl -fsS --max-time 2 "$url/api/info" >/dev/null 2>&1 } resolve_giftag() { local candidates=() [ -n "$BASE_URL" ] && candidates+=("$(normalize_url "$BASE_URL")") candidates+=("http://giftag.local" "http://192.168.4.1") for candidate in "${candidates[@]}"; do if test_giftag "$candidate"; then printf "%s" "$candidate" return 0 fi done echo "GifTag ESP bulunamadi. ESP URL/IP girin (ornek: http://192.168.1.50):" >&2 read -r manual manual="$(normalize_url "$manual")" if [ -n "$manual" ] && test_giftag "$manual"; then printf "%s" "$manual" return 0 fi return 1 } json_string() { if command -v python3 >/dev/null 2>&1; then python3 -c 'import json,sys; print(json.dumps(sys.stdin.read().rstrip("\n")))' 2>/dev/null else sed 's/\\/\\\\/g; s/"/\\"/g; s/^/"/; s/$/"/' fi } get_spotify() { osascript <<'APPLESCRIPT' if application "Spotify" is running then tell application "Spotify" if player state is playing or player state is paused then set t to name of current track set a to artist of current track set al to album of current track set p to player position set d to duration of current track set s to player state as text return t & linefeed & a & linefeed & al & linefeed & p & linefeed & d & linefeed & s end if end tell end if APPLESCRIPT } get_music() { osascript <<'APPLESCRIPT' if application "Music" is running then tell application "Music" if player state is playing or player state is paused then set t to name of current track set a to artist of current track set al to album of current track set p to player position set d to duration of current track set s to player state as text return t & linefeed & a & linefeed & al & linefeed & p & linefeed & d & linefeed & s end if end tell end if APPLESCRIPT } BASE_URL="$(resolve_giftag)" || { echo "GifTag ESP'ye baglanilamadi." >&2 exit 1 } echo "GifTag Medya helper calisiyor: $BASE_URL" while true; do DATA="$(get_spotify)" [ -z "$DATA" ] && DATA="$(get_music)" if [ -n "$DATA" ]; then TITLE="$(printf "%s" "$DATA" | sed -n '1p')" ARTIST="$(printf "%s" "$DATA" | sed -n '2p')" ALBUM="$(printf "%s" "$DATA" | sed -n '3p')" POS="$(printf "%s" "$DATA" | sed -n '4p')" DUR="$(printf "%s" "$DATA" | sed -n '5p')" STATE="$(printf "%s" "$DATA" | sed -n '6p')" POS_MS="$(awk "BEGIN { printf \"%d\", ($POS) * 1000 }")" DUR_MS="$(awk "BEGIN { printf \"%d\", ($DUR) }")" PLAYING="false" [ "$STATE" = "playing" ] && PLAYING="true" KEY="$TITLE|$ARTIST|$ALBUM|$DUR_MS|$PLAYING" JSON="{\"source_id\":\"$SID\",\"source_name\":\"GifTag macOS Helper\",\"title\":$(printf "%s" "$TITLE" | json_string),\"artist\":$(printf "%s" "$ARTIST" | json_string),\"album\":$(printf "%s" "$ALBUM" | json_string),\"position_ms\":$POS_MS,\"duration_ms\":$DUR_MS,\"is_playing\":$PLAYING,\"media_key\":$(printf "%s" "$KEY" | json_string)}" ENDPOINT="/api/now_playing_tick" [ "$KEY" != "$LAST_KEY" ] && ENDPOINT="/api/now_playing" curl -fsS --max-time 3 -X POST "$BASE_URL$ENDPOINT" -H "Content-Type: application/json" --data "$JSON" >/dev/null 2>&1 LAST_KEY="$KEY" fi sleep 2 done