-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/music release tracker #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ac42fe1
feat(workflows): music-release-tracker init
Saigz 76772a2
feat(workflows): music-release-tracker trigger
Saigz 12c17f1
fix(music-release-tracker): yandex music script
Saigz 8abd001
fix(music-release-tracker): script exit code
Saigz 89b26e8
feat(music-release-tracker): youtube music init
Saigz 5999050
feat(music-release-tracker): action summary
Saigz 990d187
feat(music-release-tracker): enable yandex music
Saigz a83a2c5
feat(music-release-tracker): runs on self-hosted runner
Saigz a4ed399
fix(music-release-tracker): yandex music track version
Saigz f44d8d8
feat(music-release-tracker): release-date init
Saigz a396a89
feat(music-release-tracker): rm youtube-music
Saigz 5fc3d7c
feat(music-release-tracker): add traceback
Saigz 73eb263
feat(music-release-tracker): update action summary
Saigz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.