-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch.py
More file actions
192 lines (171 loc) · 7.08 KB
/
patch.py
File metadata and controls
192 lines (171 loc) · 7.08 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
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
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
import pathlib, sys
if not pathlib.Path("package.json").exists():
sys.exit("Run from Musicanaz root")
# ── yt-client.ts: send cookies as base64 (no encryption needed) ──────────────
pathlib.Path("lib/yt-client.ts").write_text('''\
"use client"
import {
getEncryptedCookies, setEncryptedCookies,
clearEncryptedCookies, hasCookies,
} from "./storage"
import type { Song } from "./types"
const BASE = "/api/ytdata"
// Store cookies as base64 in localStorage (SafeStore prefix for privacy)
export function cookiesAreSet(): boolean {
return typeof window !== "undefined" && hasCookies()
}
export function saveCookies(raw: string): void {
const b64 = btoa(unescape(encodeURIComponent(raw)))
setEncryptedCookies(b64) // reuse the safe slot
}
export function removeCookies(): void {
clearEncryptedCookies()
}
function _payload(): object {
if (!hasCookies()) return {}
const c = getEncryptedCookies()
if (!c) return {}
return { cookies: c } // base64 string, server decodes it
}
async function post<T = any>(path: string): Promise<T> {
const r = await fetch(`${BASE}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(_payload()),
})
if (!r.ok) throw new Error(`ytdata ${path} → ${r.status}`)
return r.json()
}
export async function getYTHome(): Promise<any[]> { try { return (await post<any>("/home")).items ?? [] } catch { return [] } }
export async function getYTHistory(): Promise<any[]> { try { return (await post<any>("/history")).items ?? [] } catch { return [] } }
export async function getYTLiked(): Promise<any[]> { try { return (await post<any>("/liked")).items ?? [] } catch { return [] } }
export async function getYTTrending(): Promise<any[]> { try { return (await post<any>("/trending")).items ?? [] } catch { return [] } }
export async function getYTRelated(videoId: string): Promise<any[]> {
try { return (await post<any>(`/related?v=${videoId}`)).items ?? [] } catch { return [] }
}
export async function recordYTPlay(videoId: string): Promise<void> {
try { await post(`/record_play?v=${videoId}`) } catch {}
}
export function ytItemToSong(item: any): Song {
return {
id: item.videoId ?? item.id ?? "",
title: item.title ?? "Unknown",
artist: item.artist ?? item.artists?.[0]?.name ?? "YouTube",
thumbnail: item.thumbnail ?? item.thumbnails?.[0]?.url ?? "",
videoId: item.videoId ?? item.id ?? "",
type: "yt",
duration: item.duration ?? "",
album: item.album ?? "",
}
}
''')
print("✓ lib/yt-client.ts")
# ── YTCookiesPanel: update instructions for music.youtube.com ─────────────────
pathlib.Path("components/yt-cookies-panel.tsx").write_text('''\
"use client"
import { useState, useEffect } from "react"
import { saveCookies, removeCookies, cookiesAreSet } from "@/lib/yt-client"
import { Button } from "@/components/ui/button"
import { CheckCircle2, Trash2, Cookie, ChevronDown, ChevronUp } from "lucide-react"
export function YTCookiesPanel() {
const [connected, setConnected] = useState(false)
const [text, setText] = useState("")
const [saving, setSaving] = useState(false)
const [showHow, setShowHow] = useState(false)
const [msg, setMsg] = useState("")
useEffect(() => { setConnected(cookiesAreSet()) }, [])
function handleSave() {
const t = text.trim()
if (!t) { setMsg("Paste your cookies first."); return }
if (!t.includes("SAPISID") && !t.includes("music.youtube.com") && !t.includes("youtube.com")) {
setMsg("Doesn\'t look like YouTube Music cookies. Make sure you export from music.youtube.com.")
return
}
setSaving(true)
try {
saveCookies(t)
setConnected(true)
setText("")
setMsg("Connected! Go to Home to see your personalised feed.")
} catch (e: any) {
setMsg("Error: " + (e?.message ?? "unknown"))
} finally {
setSaving(false)
}
}
function handleRemove() {
removeCookies()
setConnected(false)
setText("")
setMsg("Disconnected.")
}
return (
<div className="rounded-2xl border border-border bg-card text-card-foreground overflow-hidden mb-3">
<div className="flex items-center gap-3 px-4 py-3">
<Cookie className="w-5 h-5 text-red-500 shrink-0" />
<div className="flex-1 min-w-0">
<p className="font-medium text-sm text-foreground">YouTube Music Account</p>
<p className="text-xs text-muted-foreground">
{connected ? "✓ Account connected — personalised feed active" : "Not connected"}
</p>
</div>
{connected && <CheckCircle2 className="w-5 h-5 text-green-500 shrink-0" />}
</div>
<div className="border-t border-border/40 px-4 py-3 space-y-3">
<button
onClick={() => setShowHow(v => !v)}
className="flex items-center gap-1 text-xs text-primary font-medium"
>
{showHow ? <ChevronUp className="w-3 h-3" /> : <ChevronDown className="w-3 h-3" />}
How to get your cookies
</button>
{showHow && (
<ol className="text-xs text-muted-foreground space-y-1.5 pl-4 list-decimal">
<li>Install <strong className="text-foreground">Get cookies.txt LOCALLY</strong> extension</li>
<li>Open <strong className="text-foreground">music.youtube.com</strong> and sign in</li>
<li>Click the extension → <strong className="text-foreground">Export as Netscape format</strong></li>
<li>Copy everything and paste below</li>
</ol>
)}
<textarea
value={text}
onChange={e => { setText(e.target.value); setMsg("") }}
placeholder="Paste music.youtube.com Netscape cookies here..."
rows={5}
className="w-full rounded-lg border border-border bg-background text-foreground text-xs p-2.5 resize-none focus:outline-none focus:ring-1 focus:ring-primary placeholder:text-muted-foreground font-mono"
/>
{msg && (
<p className={`text-xs leading-snug ${
msg.startsWith("Connected") ? "text-green-600 dark:text-green-400" : "text-destructive"
}`}>{msg}</p>
)}
<div className="flex gap-2">
<Button
size="sm"
className="rounded-full flex-1"
onClick={handleSave}
disabled={saving || !text.trim()}
>
{connected ? "Update Cookies" : "Connect Account"}
</Button>
{connected && (
<Button size="sm" variant="outline" className="rounded-full" onClick={handleRemove}>
<Trash2 className="w-3.5 h-3.5" />
</Button>
)}
</div>
</div>
</div>
)
}
''')
print("✓ components/yt-cookies-panel.tsx")
print("""
Done. Now:
1. Push the HF Space files (app.py, requirements.txt, Dockerfile) to the ytmlp repo
2. Run this in Musicanaz:
git add lib/yt-client.ts components/yt-cookies-panel.tsx
git commit -m "fix: ytmusicapi cookies, base64 transport"
git push
""")