fix-cover-urls.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. """Fix broken cover URLs by prepending the Audiobookshelf server URL"""
  3. import asyncio
  4. import sqlite3
  5. from app.database import async_session
  6. from app.models import Book, User
  7. from sqlalchemy import select, update
  8. async def fix_cover_urls():
  9. """Update all book cover URLs to be full URLs"""
  10. async with async_session() as db:
  11. # Get all users
  12. result = await db.execute(select(User))
  13. users = result.scalars().all()
  14. if not users:
  15. print("No users found")
  16. return
  17. # For each user, get their ABS URL
  18. # Since books are shared, we'll use the first admin user's URL
  19. admin_user = next((u for u in users if u.is_admin), users[0])
  20. abs_url = admin_user.abs_url.rstrip('/')
  21. print(f"Using Audiobookshelf URL: {abs_url}")
  22. # Get all books with relative cover URLs
  23. result = await db.execute(
  24. select(Book).where(
  25. Book.cover_url.isnot(None),
  26. Book.cover_url != ''
  27. )
  28. )
  29. books = result.scalars().all()
  30. fixed_count = 0
  31. for book in books:
  32. if book.cover_url and not book.cover_url.startswith('http'):
  33. # This is a relative URL, fix it
  34. old_url = book.cover_url
  35. book.cover_url = f"{abs_url}{old_url}"
  36. print(f"Fixed: {book.title[:50]}")
  37. print(f" Old: {old_url}")
  38. print(f" New: {book.cover_url}")
  39. print()
  40. fixed_count += 1
  41. await db.commit()
  42. print(f"\nFixed {fixed_count} cover URLs")
  43. if __name__ == "__main__":
  44. print("Fixing book cover URLs...")
  45. print("-" * 60)
  46. asyncio.run(fix_cover_urls())