BitMidi
by RFour · Jul 19, 2026 ·javascript
Search BitMidi stuff, :p
SearchBitMidiMIDIMusic
| 1 | export async function midi(req) { |
| 2 | const { searchParams: sp } = new URL(req.url); |
| 3 | const q = sp.get('q'); |
| 4 | const page = sp.get('page') || 0; |
| 5 | const id = sp.get('id'); |
| 6 | const mode = sp.get('mode') || 'search'; |
| 7 | |
| 8 | if (mode === 'download' && id) { |
| 9 | return Response.redirect(`https://bitmidi.com/uploads/${id}.mid`, 302); |
| 10 | } |
| 11 | |
| 12 | if (!q) { |
| 13 | return { |
| 14 | error: "missing query", |
| 15 | usage: { |
| 16 | search: "/midi?q=tokyo", |
| 17 | page: "/midi?q=tokyo&page=1", |
| 18 | download: "/midi?mode=download&id=104639" |
| 19 | } |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | const res = await fetch(`https://bitmidi.com/api/midi/search?q=${encodeURIComponent(q)}&page=${page}`, { |
| 25 | headers: { "Accept": "application/json" } |
| 26 | }); |
| 27 | const data = await res.json(); |
| 28 | |
| 29 | const results = (data.result?.results || []).map(r => ({ |
| 30 | id: r.id, |
| 31 | name: r.name, |
| 32 | slug: r.slug, |
| 33 | views: r.views, |
| 34 | plays: r.plays, |
| 35 | url: `https://bitmidi.com${r.url}`, |
| 36 | download: `https://bitmidi.com${r.downloadUrl}` |
| 37 | })); |
| 38 | |
| 39 | return { |
| 40 | status: true, |
| 41 | query: q, |
| 42 | page: Number(page), |
| 43 | total: data.result?.total || 0, |
| 44 | results |
| 45 | }; |
| 46 | } catch (err) { |
| 47 | return { error: err.message }; |
| 48 | } |
| 49 | } |