veltrixcode commited on
Commit
7a11778
·
verified ·
1 Parent(s): 794a1a3

Upload 8 files

Browse files
Files changed (8) hide show
  1. Dockerfile +29 -0
  2. app.py +41 -0
  3. docs.md +45 -0
  4. requirements.txt +4 -0
  5. routes.py +93 -0
  6. routes_entities.py +81 -0
  7. routes_explore.py +99 -0
  8. swagger.py +30 -0
Dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ ENV PYTHONUNBUFFERED=1 \
4
+ PIP_NO_CACHE_DIR=1 \
5
+ PORT=7860
6
+
7
+ # System deps
8
+ RUN apt-get update && apt-get install -y --no-install-recommends \
9
+ build-essential \
10
+ curl \
11
+ && rm -rf /var/lib/apt/lists/*
12
+
13
+ WORKDIR /app
14
+
15
+ # Copy only requirements first for caching
16
+ COPY requirements.txt /app/requirements.txt
17
+ RUN pip install --upgrade pip && pip install -r /app/requirements.txt
18
+
19
+ # Copy app code
20
+ COPY . /app
21
+
22
+ # Non-root user
23
+ RUN useradd -m -u 1000 appuser
24
+ USER appuser
25
+
26
+ EXPOSE 7860
27
+
28
+ # Hugging Face Spaces sets $PORT; default to 7860
29
+ CMD ["sh", "-c", "waitress-serve --host=0.0.0.0 --port=${PORT:-7860} app:app"]
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify
2
+ from ytmusicapi import YTMusic
3
+ from routes import bp as api_bp
4
+ from routes_entities import bp_entities
5
+ from routes_explore import bp_explore
6
+ from swagger import init_swagger
7
+ import os
8
+
9
+
10
+ def create_app() -> Flask:
11
+ app = Flask(__name__)
12
+
13
+ # Unauthenticated client (no OAuth or cookies)
14
+ # See: https://ytmusicapi.readthedocs.io/en/stable/
15
+ ytmusic = YTMusic()
16
+
17
+ @app.get("/health")
18
+ def health() -> tuple:
19
+ return jsonify({"status": "ok"}), 200
20
+
21
+ # Expose ytmusic instance via app context for use in other routes
22
+ app.config["YTMUSIC_CLIENT"] = ytmusic
23
+
24
+ # Docs
25
+ init_swagger(app)
26
+
27
+ # Register API blueprints under /api
28
+ app.register_blueprint(api_bp, url_prefix="/api")
29
+ app.register_blueprint(bp_entities, url_prefix="/api")
30
+ app.register_blueprint(bp_explore, url_prefix="/api")
31
+ return app
32
+
33
+
34
+ # Module-level app for WSGI servers
35
+ app = create_app()
36
+
37
+
38
+ if __name__ == "__main__":
39
+ # Local dev server
40
+ port = int(os.environ.get("PORT", 8000))
41
+ app.run(host="0.0.0.0", port=port, debug=True)
docs.md ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # YouTube Music API (Unofficial)
2
+
3
+ Flask wrapper around `ytmusicapi` exposing simple unauthenticated endpoints.
4
+
5
+ Docs: `/docs/` (Swagger UI) after running the server.
6
+
7
+ ## Quick start
8
+
9
+ 1. Create a virtualenv (optional)
10
+ 2. Install deps:
11
+ ```bash
12
+ pip install -r requirements.txt
13
+ ```
14
+ 3. Run the server (dev):
15
+ ```bash
16
+ python app.py
17
+ ```
18
+ - Server: http://localhost:8000
19
+ - Docs: http://localhost:8000/docs/
20
+
21
+ For production, you can run with `waitress`:
22
+ ```bash
23
+ waitress-serve --host=0.0.0.0 --port=8000 app:app
24
+ ```
25
+
26
+ ## Endpoints (prefix: `/api`)
27
+
28
+ - GET `/health` – health check
29
+ - GET `/search?q=...&filter=...&limit=...&official=...`
30
+ - GET `/search/suggestions?q=...`
31
+ - GET `/songs/{video_id}`
32
+ - GET `/albums/{browse_id}`
33
+ - GET `/artists/{browse_id}`
34
+ - GET `/playlists/{playlist_id}?limit=...`
35
+ - GET `/charts?country=..`
36
+ - GET `/moods`
37
+ - GET `/moods/{category_id}`
38
+ - GET `/watch_playlist?videoId=...|playlistId=...&radio=true|false&shuffle=true|false&limit=...`
39
+
40
+ Notes:
41
+ - This is unauthenticated; some actions are limited compared to logged-in YT Music.
42
+ - Refer to `ytmusicapi` docs for parameter semantics and response shapes.
43
+
44
+ ## References
45
+ - ytmusicapi documentation: https://ytmusicapi.readthedocs.io/en/stable/
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Flask==3.0.3
2
+ flasgger==0.9.7.1
3
+ ytmusicapi==1.9.0
4
+ waitress==3.0.0
routes.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Blueprint, current_app, jsonify, request
2
+ from typing import Optional
3
+
4
+ bp = Blueprint("api", __name__)
5
+
6
+
7
+ ALLOWED_FILTERS = {
8
+ "songs",
9
+ "videos",
10
+ "albums",
11
+ "artists",
12
+ "playlists",
13
+ "profiles",
14
+ "podcasts",
15
+ "episodes",
16
+ "community_playlists",
17
+ }
18
+
19
+
20
+ def _client():
21
+ return current_app.config["YTMUSIC_CLIENT"]
22
+
23
+
24
+ @bp.get("/search")
25
+ def search():
26
+ """Search YouTube Music
27
+ ---
28
+ parameters:
29
+ - name: q
30
+ in: query
31
+ type: string
32
+ required: true
33
+ description: Search query string
34
+ - name: filter
35
+ in: query
36
+ type: string
37
+ required: false
38
+ enum: [songs, videos, albums, artists, playlists, profiles, podcasts, episodes, community_playlists]
39
+ description: Restrict results to a specific type
40
+ - name: limit
41
+ in: query
42
+ type: integer
43
+ required: false
44
+ default: 25
45
+ description: Maximum number of results
46
+ - name: official
47
+ in: query
48
+ type: boolean
49
+ required: false
50
+ description: Prefer official music videos when filter=videos
51
+ responses:
52
+ 200:
53
+ description: Search results
54
+ 400:
55
+ description: Missing/invalid params
56
+ """
57
+ query = request.args.get("q", type=str)
58
+ if not query:
59
+ return jsonify({"error": "Missing required query parameter 'q'"}), 400
60
+
61
+ flt: Optional[str] = request.args.get("filter", type=str)
62
+ limit: int = request.args.get("limit", default=25, type=int)
63
+ official: Optional[bool] = request.args.get("official", default=None, type=lambda v: v.lower() == "true")
64
+
65
+ if flt and flt not in ALLOWED_FILTERS:
66
+ return jsonify({"error": f"Invalid filter. Allowed: {sorted(list(ALLOWED_FILTERS))}"}), 400
67
+
68
+ results = _client().search(query, filter=flt, limit=limit, ignore_spelling=False, official_music_video=official)
69
+ return jsonify({"results": results})
70
+
71
+
72
+ @bp.get("/search/suggestions")
73
+ def suggestions():
74
+ """Get search suggestions
75
+ ---
76
+ parameters:
77
+ - name: q
78
+ in: query
79
+ type: string
80
+ required: true
81
+ description: Partial query to get suggestions for
82
+ responses:
83
+ 200:
84
+ description: Suggestions
85
+ 400:
86
+ description: Missing/invalid params
87
+ """
88
+ query = request.args.get("q", type=str)
89
+ if not query:
90
+ return jsonify({"error": "Missing required query parameter 'q'"}), 400
91
+
92
+ sugs = _client().get_search_suggestions(query)
93
+ return jsonify({"suggestions": sugs})
routes_entities.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Blueprint, current_app, jsonify, request
2
+
3
+ bp_entities = Blueprint("entities", __name__)
4
+
5
+
6
+ def _client():
7
+ return current_app.config["YTMUSIC_CLIENT"]
8
+
9
+
10
+ @bp_entities.get("/songs/<video_id>")
11
+ def get_song(video_id: str):
12
+ """Get song details
13
+ ---
14
+ parameters:
15
+ - name: video_id
16
+ in: path
17
+ type: string
18
+ required: true
19
+ responses:
20
+ 200:
21
+ description: Song metadata
22
+ """
23
+ data = _client().get_song(video_id)
24
+ return jsonify(data)
25
+
26
+
27
+ @bp_entities.get("/albums/<browse_id>")
28
+ def get_album(browse_id: str):
29
+ """Get album details
30
+ ---
31
+ parameters:
32
+ - name: browse_id
33
+ in: path
34
+ type: string
35
+ required: true
36
+ responses:
37
+ 200:
38
+ description: Album metadata and tracks
39
+ """
40
+ data = _client().get_album(browse_id)
41
+ return jsonify(data)
42
+
43
+
44
+ @bp_entities.get("/artists/<browse_id>")
45
+ def get_artist(browse_id: str):
46
+ """Get artist details
47
+ ---
48
+ parameters:
49
+ - name: browse_id
50
+ in: path
51
+ type: string
52
+ required: true
53
+ responses:
54
+ 200:
55
+ description: Artist info and releases
56
+ """
57
+ data = _client().get_artist(browse_id)
58
+ return jsonify(data)
59
+
60
+
61
+ @bp_entities.get("/playlists/<playlist_id>")
62
+ def get_playlist(playlist_id: str):
63
+ """Get playlist details
64
+ ---
65
+ parameters:
66
+ - name: playlist_id
67
+ in: path
68
+ type: string
69
+ required: true
70
+ - name: limit
71
+ in: query
72
+ type: integer
73
+ required: false
74
+ default: 100
75
+ responses:
76
+ 200:
77
+ description: Playlist metadata and items
78
+ """
79
+ limit = request.args.get("limit", default=100, type=int)
80
+ data = _client().get_playlist(playlist_id, limit=limit)
81
+ return jsonify(data)
routes_explore.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Blueprint, current_app, jsonify, request
2
+
3
+ bp_explore = Blueprint("explore", __name__)
4
+
5
+
6
+ def _client():
7
+ return current_app.config["YTMUSIC_CLIENT"]
8
+
9
+
10
+ @bp_explore.get("/charts")
11
+ def charts():
12
+ """Get charts (global or by country)
13
+ ---
14
+ parameters:
15
+ - name: country
16
+ in: query
17
+ type: string
18
+ required: false
19
+ responses:
20
+ 200:
21
+ description: Charts data
22
+ """
23
+ country = request.args.get("country", default=None, type=str)
24
+ data = _client().get_charts(country=country)
25
+ return jsonify(data)
26
+
27
+
28
+ @bp_explore.get("/moods")
29
+ def moods():
30
+ """Get mood/genre categories
31
+ ---
32
+ responses:
33
+ 200:
34
+ description: Mood categories
35
+ """
36
+ data = _client().get_mood_categories()
37
+ return jsonify(data)
38
+
39
+
40
+ @bp_explore.get("/moods/<category_id>")
41
+ def moods_playlists(category_id: str):
42
+ """Get playlists for a mood/genre category
43
+ ---
44
+ parameters:
45
+ - name: category_id
46
+ in: path
47
+ type: string
48
+ required: true
49
+ responses:
50
+ 200:
51
+ description: Mood playlists
52
+ """
53
+ data = _client().get_mood_playlists(category_id)
54
+ return jsonify(data)
55
+
56
+
57
+ @bp_explore.get("/watch_playlist")
58
+ def watch_playlist():
59
+ """Get watch playlist (radio/shuffle)
60
+ ---
61
+ parameters:
62
+ - name: videoId
63
+ in: query
64
+ type: string
65
+ required: false
66
+ - name: playlistId
67
+ in: query
68
+ type: string
69
+ required: false
70
+ - name: radio
71
+ in: query
72
+ type: boolean
73
+ required: false
74
+ default: false
75
+ - name: shuffle
76
+ in: query
77
+ type: boolean
78
+ required: false
79
+ default: false
80
+ - name: limit
81
+ in: query
82
+ type: integer
83
+ required: false
84
+ default: 25
85
+ responses:
86
+ 200:
87
+ description: Watch playlist data
88
+ 400:
89
+ description: Missing params
90
+ """
91
+ video_id = request.args.get("videoId")
92
+ playlist_id = request.args.get("playlistId")
93
+ radio = request.args.get("radio", default=False, type=lambda v: v.lower() == "true")
94
+ shuffle = request.args.get("shuffle", default=False, type=lambda v: v.lower() == "true")
95
+ limit = request.args.get("limit", default=25, type=int)
96
+ if not video_id and not playlist_id:
97
+ return jsonify({"error": "Provide either videoId or playlistId"}), 400
98
+ data = _client().get_watch_playlist(videoId=video_id, playlistId=playlist_id, radio=radio, shuffle=shuffle, limit=limit)
99
+ return jsonify(data)
swagger.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flasgger import Swagger
2
+
3
+ swagger_template = {
4
+ "swagger": "2.0",
5
+ "info": {
6
+ "title": "YouTube Music API (Unofficial)",
7
+ "description": "Flask wrapper around ytmusicapi (unauthenticated). See docs: https://ytmusicapi.readthedocs.io/en/stable/",
8
+ "version": "1.0.0",
9
+ },
10
+ "basePath": "/",
11
+ }
12
+
13
+ swagger_config = {
14
+ "headers": [],
15
+ "specs": [
16
+ {
17
+ "endpoint": "apispec",
18
+ "route": "/apispec.json",
19
+ "rule_filter": lambda rule: True,
20
+ "model_filter": lambda tag: True,
21
+ }
22
+ ],
23
+ "static_url_path": "/flasgger_static",
24
+ "swagger_ui": True,
25
+ "specs_route": "/docs/",
26
+ }
27
+
28
+
29
+ def init_swagger(app):
30
+ Swagger(app, template=swagger_template, config=swagger_config)