Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/music-release-tracker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build music dict

on:
workflow_dispatch:
pull_request:
types:
- opened
- edited
- reopened
- synchronize

jobs:
build-music-dict:
runs-on: ubuntu-latest
env:
ARTIST: "Imagine Dragons"
TRACKS: |
Radioactive
Demons
Warriors
Bones

steps:
- name: Checkout
uses: actions/checkout@v5

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Yandex Music
run: |
python scripts/music-release-tracker/yandex-music/main.py
cat yandex_dict.yaml

- name: Spotify
env:
SPOTIFY_TOKEN: ${{ secrets.SPOTIFY_TOKEN }}
run: |
echo "TODO: get spotify token"
# python scripts/music-release-tracker/spotify/main.py
# cat spotify_dict.yaml

- name: Apple Music
run: |
python scripts/music-release-tracker/apple-music/main.py
cat apple_music_dict.yaml
105 changes: 105 additions & 0 deletions scripts/music-release-tracker/apple-music/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
import json
import os
import re
import sys
import urllib.parse
import urllib.request


def norm(s: str) -> str:
return " ".join(s.lower().split())


def fetch_search(country: str, artist: str, title: str) -> dict:
term = f"{artist} {title}"
q = urllib.parse.quote(term)
params = {
"term": term,
"media": "music",
"entity": "song",
"country": country,
"limit": "50",
}
qs = urllib.parse.urlencode(params)
url = f"https://itunes.apple.com/search?{qs}"

req = urllib.request.Request(
url,
headers={
"User-Agent": "music-dict-ci",
"Accept": "application/json",
},
)
with urllib.request.urlopen(req) as resp:
data = resp.read()
return json.loads(data)


def is_exact_match(artist: str, title: str, payload: dict) -> bool:
results = payload.get("results", [])

n_artist = norm(artist)
n_title = norm(title)

for item in results:
if norm(item.get("trackName", "")) != n_title:
continue
if norm(item.get("artistName", "")) != n_artist:
continue
return True

return False


def build_dict(country: str, artist: str, tracks: list[str]) -> dict:
result = {"apple_music": {}}

for title in tracks:
try:
payload = fetch_search(country, artist, title)
found = is_exact_match(artist, title, payload)
except Exception:
found = False
result["apple_music"][title] = [1 if found else 0]

return result


def to_yaml(data: dict) -> str:
lines = []
for platform, mapping in data.items():
lines.append(f"{platform}:")
for track_name, value in mapping.items():
safe = track_name.replace('"', '\\"')
lines.append(f' "{safe}": [{value[0]}]')
return "\n".join(lines)


def parse_tracks(arg: str) -> list[str]:
return [t.strip() for t in re.split(r"[;\n]", arg) if t.strip()]


def main() -> None:
artist = os.getenv("ARTIST", "").strip()
tracks_raw = os.getenv("TRACKS", "")
country = os.getenv("APPLE_COUNTRY", "US").strip() or "US"

if not artist or not tracks_raw.strip():
sys.stderr.write("ARTIST и TRACKS должны быть заданы в env\n")
sys.exit(1)

tracks = parse_tracks(tracks_raw)
if not tracks:
sys.stderr.write("TRACKS пустой после парсинга\n")
sys.exit(1)

data = build_dict(country, artist, tracks)
yaml_str = to_yaml(data)

with open("apple_music_dict.yaml", "w", encoding="utf-8") as f:
f.write(yaml_str)


if __name__ == "__main__":
main()
100 changes: 100 additions & 0 deletions scripts/music-release-tracker/spotify/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python3
import json
import os
import re
import sys
import urllib.parse
import urllib.request


def norm(s: str) -> str:
return " ".join(s.lower().split())


def fetch_search(token: str, artist: str, title: str) -> dict:
query = f'track:"{title}" artist:"{artist}"'
q = urllib.parse.quote(query)
url = f"https://api.spotify.com/v1/search?type=track&limit=50&q={q}"
req = urllib.request.Request(
url,
headers={
"User-Agent": "music-dict-ci",
"Accept": "application/json",
"Authorization": f"Bearer {token}",
},
)
with urllib.request.urlopen(req) as resp:
data = resp.read()
return json.loads(data)


def is_exact_match(artist: str, title: str, payload: dict) -> bool:
tracks = payload.get("tracks", {}).get("items", [])

n_artist = norm(artist)
n_title = norm(title)

for track in tracks:
if norm(track.get("name", "")) != n_title:
continue
artists = [norm(a.get("name", "")) for a in track.get("artists", [])]
if n_artist not in artists:
continue
return True

return False


def build_dict(token: str, artist: str, tracks: list[str]) -> dict:
result = {"spotify": {}}
for title in tracks:
try:
payload = fetch_search(token, artist, title)
found = is_exact_match(artist, title, payload)
except Exception:
found = False
result["spotify"][title] = [1 if found else 0]
return result


def to_yaml(data: dict) -> str:
lines = []
for platform, mapping in data.items():
lines.append(f"{platform}:")
for track_name, value in mapping.items():
safe = track_name.replace('"', '\\"')
lines.append(f' "{safe}": [{value[0]}]')
return "\n".join(lines)


def parse_tracks(arg: str) -> list[str]:
return [t.strip() for t in re.split(r"[;\n]", arg) if t.strip()]


def main() -> None:
token = os.getenv("SPOTIFY_TOKEN", "").strip()
artist = os.getenv("ARTIST", "").strip()
tracks_raw = os.getenv("TRACKS", "")

if not token:
sys.stderr.write("SPOTIFY_TOKEN должен быть задан в env\n")
sys.exit(1)

if not artist or not tracks_raw.strip():
sys.stderr.write("ARTIST и TRACKS должны быть заданы в env\n")
sys.exit(1)

tracks = parse_tracks(tracks_raw)
if not tracks:
sys.stderr.write("TRACKS пустой после парсинга\n")
sys.exit(1)

data = build_dict(token, artist, tracks)
yaml_str = to_yaml(data)

with open("spotify_dict.yaml", "w", encoding="utf-8") as f:
f.write(yaml_str)


if __name__ == "__main__":
main()
101 changes: 101 additions & 0 deletions scripts/music-release-tracker/yandex-music/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
import json
import os
import re
import sys
import urllib.parse
import urllib.request


def norm(s: str) -> str:
return " ".join(s.lower().split())


def fetch_search(artist: str, title: str) -> dict:
query = urllib.parse.quote_plus(f"{artist} {title}")
url = f"https://api.music.yandex.net/search?type=track&text={query}&page=0&nococrrect=false"
req = urllib.request.Request(
url,
headers={
"User-Agent": "Mozilla/5.0",
"Accept": "application/json",
},
)
with urllib.request.urlopen(req) as resp:
data = resp.read()
return json.loads(data)


def is_exact_match(artist: str, title: str, payload: dict) -> bool:
tracks = (
payload.get("result", {})
.get("tracks", {})
.get("results", [])
)

n_artist = norm(artist)
n_title = norm(title)

for track in tracks:
version = track.get("version") or ""
if version:
continue
if norm(track.get("title", "")) != n_title:
continue
artists = [norm(a.get("name", "")) for a in track.get("artists", [])]
if n_artist not in artists:
continue
return True

return False


def build_dict(artist: str, tracks: list[str]) -> dict:
result = {"yandex_music": {}}
for title in tracks:
try:
payload = fetch_search(artist, title)
found = is_exact_match(artist, title, payload)
except Exception:
found = False
Comment thread
Saigz marked this conversation as resolved.
Outdated
result["yandex_music"][title] = [1 if found else 0]
return result


def to_yaml(data: dict) -> str:
lines = []
for platform, mapping in data.items():
lines.append(f"{platform}:")
for track_name, value in mapping.items():
safe = track_name.replace('"', '\\"')
lines.append(f' "{safe}": [{value[0]}]')
return "\n".join(lines)


def parse_tracks_arg(arg: str) -> list[str]:
return [t.strip() for t in re.split(r"[;\n]", arg) if t.strip()]


def main() -> None:
artist = os.getenv("ARTIST", "").strip()
tracks_raw = os.getenv("TRACKS", "")

if not artist or not tracks_raw.strip():
sys.stderr.write("ARTIST и TRACKS должны быть заданы в env\n")
sys.exit(1)

tracks = parse_tracks_arg(tracks_raw)

if not tracks:
sys.stderr.write("TRACKS пустой после парсинга\n")
sys.exit(1)

data = build_dict(artist, tracks)
yaml_str = to_yaml(data)

with open("yandex_dict.yaml", "w", encoding="utf-8") as f:
f.write(yaml_str)


if __name__ == "__main__":
main()