#!/bin/bash # PUCKS Medya Yardımcısı 2.0 - macOS # Usage: chmod +x giftag_medya.command && ./giftag_medya.command set +e SID="desktop_macos" BASE_URL="${PUCKS_ESP_URL:-${GIFTAG_ESP_URL:-}}" LAST_IDENTITY="" LAST_PLAYING="" normalize_url() { local value="$1" [ -z "$value" ] && return case "$value" in http://*|https://*) printf "%s" "${value%/}" ;; *) printf "http://%s" "${value%/}" ;; esac } test_pucks() { 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_pucks() { local candidates=() [ -n "$BASE_URL" ] && candidates+=("$(normalize_url "$BASE_URL")") candidates+=("http://pucks.local" "http://giftag.local" "http://192.168.4.1") for candidate in "${candidates[@]}"; do if test_pucks "$candidate"; then printf "%s" "$candidate" return 0 fi done echo "PUCKS bulunamadi. Cihaz URL/IP adresini girin (ornek: http://192.168.1.50):" >&2 read -r manual manual="$(normalize_url "$manual")" if [ -n "$manual" ] && test_pucks "$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) * 1000 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_pucks)" || { echo "PUCKS cihazina baglanilamadi." >&2 exit 1 } echo "PUCKS Medya Yardimcisi 2.0 calisiyor: $BASE_URL" echo "Bu pencere acik kaldigi surece macOS medya bilgisi cihaza aktarilir." 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" IDENTITY="$TITLE|$ARTIST|$ALBUM|$DUR_MS" ACTION="tick" if [ "$IDENTITY" != "$LAST_IDENTITY" ]; then ACTION="track" elif [ -n "$LAST_PLAYING" ] && [ "$PLAYING" != "$LAST_PLAYING" ]; then [ "$PLAYING" = "true" ] && ACTION="play" || ACTION="pause" fi PAUSED="true" SPEED="0" [ "$PLAYING" = "true" ] && PAUSED="false" && SPEED="1" JSON="{\"source_id\":\"$SID\",\"source_name\":\"PUCKS macOS Yardimcisi\",\"action\":\"$ACTION\",\"title\":$(printf "%s" "$TITLE" | json_string),\"artist\":$(printf "%s" "$ARTIST" | json_string),\"album\":$(printf "%s" "$ALBUM" | json_string),\"position\":$POS_MS,\"duration\":$DUR_MS,\"paused\":$PAUSED,\"speed\":$SPEED,\"media_key\":$(printf "%s" "$IDENTITY" | json_string)}" curl -fsS --max-time 4 -X POST "$BASE_URL/api/now_playing" -H "Content-Type: application/json; charset=utf-8" --data "$JSON" >/dev/null 2>&1 LAST_IDENTITY="$IDENTITY" LAST_PLAYING="$PLAYING" fi sleep 2 done