-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
122 lines (106 loc) · 3.94 KB
/
Copy pathapp.py
File metadata and controls
122 lines (106 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# app.py
from flask import Flask, send_from_directory, request, jsonify, Response
import os
import json
from flask_cors import CORS
import yt_dlp
import requests
app = Flask(__name__)
CORS(app)
# File-based playlist storage (shared between music_app and NEXUS)
PLAYLISTS_FILE = os.path.join(os.path.dirname(__file__), 'playlists.json')
def load_playlists():
if os.path.exists(PLAYLISTS_FILE):
try:
with open(PLAYLISTS_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except:
return []
return []
def save_playlists(playlists):
with open(PLAYLISTS_FILE, 'w', encoding='utf-8') as f:
json.dump(playlists, f, ensure_ascii=False, indent=2)
# In-memory cache for audio URLs
audio_url_cache = {}
def search_youtube(query):
"""Searches YouTube for videos and playlists."""
ydl_opts = {
'quiet': True,
'skip_download': True,
'extract_flat': 'in_playlist',
'default_search': 'ytsearch20', # Search for 20 items
'match_filter': yt_dlp.utils.match_filter_func('duration > 60')
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(query, download=False)
entries = result.get('entries', [])
search_results = []
for entry in entries:
is_playlist = entry.get('_type') == 'playlist'
search_results.append({
'id': entry['id'],
'title': entry.get('title', 'Untitled'),
'is_playlist': is_playlist,
'thumbnail': entry.get('thumbnails', [{}])[-1].get('url') if entry.get('thumbnails') else f'https://i.ytimg.com/vi/{entry["id"]}/hqdefault.jpg'
})
return search_results
except Exception as e:
print(f"Error in search_youtube: {e}")
return []
def get_playlist_items(playlist_id):
"""Fetches all video entries from a given YouTube playlist ID."""
ydl_opts = {
'quiet': True,
'skip_download': True,
'extract_flat': True,
'playlist_items': '1-100' # Limit to first 100 items to prevent abuse
}
url = f'https://www.youtube.com/playlist?list={playlist_id}'
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(url, download=False)
entries = result.get('entries', [])
return [
{
'id': entry['id'],
'title': entry.get('title', 'Untitled'),
'thumbnail': f'https://i.ytimg.com/vi/{entry["id"]}/mqdefault.jpg'
}
for entry in entries if entry # Filter out potential None entries
]
except Exception as e:
print(f"Error in get_playlist_items: {e}")
return []
@app.route('/')
def serve_index():
return send_from_directory('dist', 'index.html')
@app.route('/<path:filename>')
def serve_static(filename):
return send_from_directory('dist', filename)
@app.route('/search')
def search():
query = request.args.get('q')
if not query:
return jsonify([])
results = search_youtube(query)
return jsonify(results)
@app.route('/get_playlist_items/<playlist_id>')
def fetch_playlist(playlist_id):
results = get_playlist_items(playlist_id)
return jsonify(results)
# === SHARED PLAYLIST STORAGE ===
@app.route('/playlists', methods=['GET'])
def get_playlists():
"""Returns all saved playlists (shared between music_app and NEXUS)"""
return jsonify(load_playlists())
@app.route('/playlists', methods=['POST'])
def update_playlists():
"""Saves playlists (called by music_app frontend when playlists change)"""
data = request.get_json()
if data is not None:
save_playlists(data)
return jsonify({'success': True})
return jsonify({'error': 'Invalid data'}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 10000)))