Gaio / scripts /manage.py
OpenClaw
Migrate TGMedia (from akay12/movie) β€” full file replacement
301c4bf
Raw History Blame Contribute Delete
3.7 kB
#!/usr/bin/env python3
"""
TGMedia management CLI.
Usage:
python scripts/manage.py index # Index all configured channels
python scripts/manage.py index @chan # Index specific channel
python scripts/manage.py stats # Print DB statistics
python scripts/manage.py push # Push data to HuggingFace
python scripts/manage.py pull # Pull data from HuggingFace
python scripts/manage.py reset-channel @chan # Reset index state for a channel
python scripts/manage.py shell # Drop into async REPL
"""
import asyncio
import sys
from pathlib import Path
# Allow importing from project root
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from dotenv import load_dotenv
load_dotenv()
async def cmd_index(channel: str | None = None):
from app.core.database import init_db
from app.services.session_pool import get_pool
from app.services.indexer import Indexer
await init_db()
pool = get_pool()
await pool.start()
if not pool.is_ready:
print("❌ No authorized sessions. Run scripts/authorize_session.py first.")
return
idx = Indexer()
if channel:
from app.core.config import get_settings
settings = get_settings()
# Temporarily override channel list
result = await idx._index_channel(channel)
print(f"Result for {channel}: {result}")
else:
result = await idx.index_all_channels()
for ch, stats in result.items():
print(f" {ch}: {stats}")
await pool.stop()
async def cmd_stats():
from app.core.database import init_db
from app.services.search import get_stats
await init_db()
stats = await get_stats()
print("\nπŸ“Š TGMedia Statistics")
print("─" * 40)
for k, v in stats.items():
label = k.replace("_", " ").title()
if "bytes" in k and v:
v = f"{v / 1_073_741_824:.2f} GB"
print(f" {label:<25} {v}")
print()
async def cmd_push():
from app.services.hf_sync import push_to_hf
print("☁️ Pushing to HuggingFace...")
ok = await push_to_hf()
print("βœ… Done." if ok else "❌ Failed β€” check HF_TOKEN and HF_DATASET_REPO in .env")
async def cmd_pull():
from app.services.hf_sync import pull_from_hf
print("⬇️ Pulling from HuggingFace...")
ok = await pull_from_hf()
print("βœ… Done." if ok else "❌ Failed β€” check HF_TOKEN and HF_DATASET_REPO in .env")
async def cmd_reset_channel(channel: str):
import aiosqlite
from app.core.config import DB_PATH
from app.core.database import init_db
await init_db()
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"DELETE FROM index_state WHERE channel_id=?", (channel,)
)
await db.commit()
print(f"βœ… Index state reset for channel: {channel}")
print(" Run `index` again to re-index from scratch.")
def main():
args = sys.argv[1:]
if not args:
print(__doc__)
sys.exit(1)
cmd = args[0]
if cmd == "index":
channel = args[1] if len(args) > 1 else None
asyncio.run(cmd_index(channel))
elif cmd == "stats":
asyncio.run(cmd_stats())
elif cmd == "push":
asyncio.run(cmd_push())
elif cmd == "pull":
asyncio.run(cmd_pull())
elif cmd == "reset-channel":
if len(args) < 2:
print("Usage: manage.py reset-channel <channel_id>")
sys.exit(1)
asyncio.run(cmd_reset_channel(args[1]))
else:
print(f"Unknown command: {cmd}")
print(__doc__)
sys.exit(1)
if __name__ == "__main__":
main()