Erfan commited on
Commit
b5e074c
·
1 Parent(s): 8fd932e

fix: persistent data dir, cookies debug endpoint

Browse files
Files changed (4) hide show
  1. bot/youtube.py +5 -5
  2. config.py +6 -5
  3. db.py +3 -3
  4. web/routes.py +17 -0
bot/youtube.py CHANGED
@@ -1,16 +1,18 @@
1
  import os
2
  import yt_dlp
3
  import uuid
4
- from config import DOWNLOAD_DIR
5
 
6
  os.makedirs(DOWNLOAD_DIR, exist_ok=True)
7
 
8
- COOKIES_FILE = os.path.join(DOWNLOAD_DIR, "..", "cookies.txt")
9
 
10
 
11
  def _cookies_opts():
12
  if os.path.exists(COOKIES_FILE):
 
13
  return {"cookiefile": COOKIES_FILE}
 
14
  return {}
15
 
16
 
@@ -53,8 +55,6 @@ async def download_yt(url: str, quality: str = "audio"):
53
  else:
54
  opts = _get_ydl_opts("best", "mp4")
55
 
56
- opts["paths"] = {"home": DOWNLOAD_DIR}
57
-
58
  with yt_dlp.YoutubeDL(opts) as ydl:
59
  info = ydl.extract_info(url, download=True)
60
  file_path = ydl.prepare_filename(info)
@@ -67,6 +67,6 @@ async def download_yt(url: str, quality: str = "audio"):
67
  return file_path, info.get("title", "video")
68
  for f in os.listdir(DOWNLOAD_DIR):
69
  p = os.path.join(DOWNLOAD_DIR, f)
70
- if os.path.isfile(p) and f != "cookies.txt":
71
  return p, info.get("title", "video")
72
  return None, None
 
1
  import os
2
  import yt_dlp
3
  import uuid
4
+ from config import DOWNLOAD_DIR, DATA_DIR
5
 
6
  os.makedirs(DOWNLOAD_DIR, exist_ok=True)
7
 
8
+ COOKIES_FILE = os.path.join(DATA_DIR, "cookies.txt")
9
 
10
 
11
  def _cookies_opts():
12
  if os.path.exists(COOKIES_FILE):
13
+ print(f"[youtube] Using cookies from {COOKIES_FILE}")
14
  return {"cookiefile": COOKIES_FILE}
15
+ print(f"[youtube] No cookies file at {COOKIES_FILE}")
16
  return {}
17
 
18
 
 
55
  else:
56
  opts = _get_ydl_opts("best", "mp4")
57
 
 
 
58
  with yt_dlp.YoutubeDL(opts) as ydl:
59
  info = ydl.extract_info(url, download=True)
60
  file_path = ydl.prepare_filename(info)
 
67
  return file_path, info.get("title", "video")
68
  for f in os.listdir(DOWNLOAD_DIR):
69
  p = os.path.join(DOWNLOAD_DIR, f)
70
+ if os.path.isfile(p):
71
  return p, info.get("title", "video")
72
  return None, None
config.py CHANGED
@@ -2,11 +2,12 @@ import os
2
 
3
  BALE_TOKEN = os.getenv("BALE_TOKEN", "")
4
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
5
- WEBHOOK_URL = os.getenv("WEBHOOK_URL", "") # e.g. https://your-app.onrender.com/webhook/bot
6
  APP_URL = os.getenv("APP_URL", "http://localhost:8000")
7
- DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///data/bot.db")
8
- ADMIN_IDS = os.getenv("ADMIN_IDS", "") # comma-separated Bale user IDs
9
 
10
  BALE_API_BASE = "https://tapi.bale.ai/bot"
11
- MAX_FILE_SIZE = 50 * 1024 * 1024 # 50 MB
12
- DOWNLOAD_DIR = "/tmp/downloads"
 
 
 
2
 
3
  BALE_TOKEN = os.getenv("BALE_TOKEN", "")
4
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
5
+ WEBHOOK_URL = os.getenv("WEBHOOK_URL", "")
6
  APP_URL = os.getenv("APP_URL", "http://localhost:8000")
7
+ ADMIN_IDS = os.getenv("ADMIN_IDS", "")
 
8
 
9
  BALE_API_BASE = "https://tapi.bale.ai/bot"
10
+ MAX_FILE_SIZE = 50 * 1024 * 1024
11
+
12
+ DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
13
+ DOWNLOAD_DIR = os.path.join(DATA_DIR, "downloads")
db.py CHANGED
@@ -1,12 +1,12 @@
1
  import aiosqlite
2
  import os
 
3
 
4
- DB_DIR = "data"
5
- DB_PATH = os.path.join(DB_DIR, "bot.db")
6
 
7
 
8
  async def init_db():
9
- os.makedirs(DB_DIR, exist_ok=True)
10
  async with aiosqlite.connect(DB_PATH) as db:
11
  db.row_factory = aiosqlite.Row
12
  await db.executescript("""
 
1
  import aiosqlite
2
  import os
3
+ from config import DATA_DIR
4
 
5
+ DB_PATH = os.path.join(DATA_DIR, "bot.db")
 
6
 
7
 
8
  async def init_db():
9
+ os.makedirs(DATA_DIR, exist_ok=True)
10
  async with aiosqlite.connect(DB_PATH) as db:
11
  db.row_factory = aiosqlite.Row
12
  await db.executescript("""
web/routes.py CHANGED
@@ -125,6 +125,23 @@ async def admin_settings_save(
125
  return render("admin/settings.html", settings={"openai_key": openai_key, "webhook_url": webhook_url}, saved=True)
126
 
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  @router.post("/admin/upload-cookies")
129
  async def upload_cookies(cookies: UploadFile = File(...)):
130
  from bot.youtube import COOKIES_FILE
 
125
  return render("admin/settings.html", settings={"openai_key": openai_key, "webhook_url": webhook_url}, saved=True)
126
 
127
 
128
+ @router.get("/admin/cookies-status")
129
+ async def cookies_status():
130
+ from bot.youtube import COOKIES_FILE
131
+ exists = os.path.exists(COOKIES_FILE)
132
+ size = os.path.getsize(COOKIES_FILE) if exists else 0
133
+ first_line = ""
134
+ if exists:
135
+ with open(COOKIES_FILE) as f:
136
+ first_line = f.readline().strip()
137
+ return JSONResponse({
138
+ "exists": exists,
139
+ "size": size,
140
+ "path": COOKIES_FILE,
141
+ "first_line": first_line,
142
+ })
143
+
144
+
145
  @router.post("/admin/upload-cookies")
146
  async def upload_cookies(cookies: UploadFile = File(...)):
147
  from bot.youtube import COOKIES_FILE