File size: 4,974 Bytes
2d2e00c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const express = require('express');

const router = express.Router();

const ALLOWED_FILTERS = new Set([
  'songs',
  'videos',
  'albums',
  'artists',
  'playlists',
  'profiles',
  'podcasts',
  'episodes',
  'community_playlists'
]);

/**
 * @swagger
 * /api/search:
 *   get:
 *     summary: Search YouTube Music
 *     parameters:
 *       - in: query
 *         name: q
 *         required: true
 *         schema:
 *           type: string
 *         description: Search query string
 *       - in: query
 *         name: filter
 *         schema:
 *           type: string
 *           enum: [songs, videos, albums, artists, playlists, profiles, podcasts, episodes, community_playlists]
 *         description: Restrict results to a specific type
 *       - in: query
 *         name: continuationToken
 *         schema:
 *           type: string
 *         description: Continuation token for pagination
 *       - in: query
 *         name: ignore_spelling
 *         schema:
 *           type: boolean
 *           default: false
 *         description: Whether to ignore spelling corrections
 *     responses:
 *       200:
 *         description: Search results with continuation token
 *       400:
 *         description: Missing/invalid params
 */
router.get('/search', async (req, res) => {
  try {
    const { q: query, filter, continuationToken, ignore_spelling = false } = req.query;

    // FIXED: Don't require query if continuation token is provided
    if (!query && !continuationToken) {
      return res.status(400).json({ error: "Missing required query parameter 'q' or 'continuationToken'" });
    }

    if (filter && !ALLOWED_FILTERS.has(filter)) {
      return res.status(400).json({
        error: `Invalid filter. Allowed: ${Array.from(ALLOWED_FILTERS).sort()}`
      });
    }

    const ytmusic = req.app.locals.ytmusic;
    
    // FIXED: Pass query only if it's not a continuation request
    const searchResults = await ytmusic.search(
      query || null, 
      filter, 
      continuationToken, 
      ignore_spelling === 'true'
    );
    
    res.json({
      query: query || null,
      filter,
      results: searchResults.results,
      continuationToken: searchResults.continuationToken
    });
  } catch (error) {
    console.error('Search error:', error);
    res.status(500).json({ error: `Search failed: ${error.message}` });
  }
});

/**
 * @swagger
 * /api/search/suggestions:
 *   get:
 *     summary: Get search suggestions
 *     parameters:
 *       - in: query
 *         name: q
 *         required: true
 *         schema:
 *           type: string
 *         description: Partial query to get suggestions for
 *       - in: query
 *         name: music
 *         schema:
 *           type: integer
 *         description: If 1, get suggestions from YouTube Music. If not present or 0, get suggestions from YouTube
 *     responses:
 *       200:
 *         description: Suggestions
 *       400:
 *         description: Missing/invalid params
 */
router.get('/search/suggestions', async (req, res) => {
  try {
    const { q: query, music } = req.query;

    if (!query) {
      return res.status(400).json({ error: "Missing required query parameter 'q'" });
    }

    const ytmusic = req.app.locals.ytmusic;
    const youtubeSearch = req.app.locals.youtubeSearch;

    if (music === '1') {
      // Get suggestions from YouTube Music
      const suggestions = await ytmusic.getSearchSuggestions(query);
      res.json({ suggestions, source: 'youtube_music' });
    } else {
      // Get suggestions from YouTube
      const suggestions = await youtubeSearch.getSuggestions(query);
      res.json({ suggestions, source: 'youtube' });
    }
  } catch (error) {
    console.error('Suggestions error:', error);
    res.status(500).json({ error: `Suggestions failed: ${error.message}` });
  }
});

/**
 * @swagger
 * /api/search/suggestions/debug:
 *   get:
 *     summary: Debug endpoint to test YouTube suggestions API directly
 *     parameters:
 *       - in: query
 *         name: q
 *         required: true
 *         schema:
 *           type: string
 *         description: Query to test
 *     responses:
 *       200:
 *         description: Debug suggestions
 *       400:
 *         description: Missing query parameter
 */
router.get('/search/suggestions/debug', async (req, res) => {
  try {
    const { q: query } = req.query;

    if (!query) {
      return res.status(400).json({ error: "Missing required query parameter 'q'" });
    }

    const youtubeSearch = req.app.locals.youtubeSearch;
    const suggestions = await youtubeSearch.getSuggestions(query);

    res.json({
      query,
      suggestions,
      count: suggestions.length,
      source: 'youtube_debug'
    });
  } catch (error) {
    console.error('Debug suggestions error:', error);
    res.status(500).json({
      error: `Debug failed: ${error.message}`,
      query: req.query.q
    });
  }
});

module.exports = router;