|
6 | 6 | # See CONTRIBUTORS for full list of authors. |
7 | 7 |
|
8 | 8 | import json |
| 9 | +import os |
9 | 10 | from typing import Optional, List, Dict |
10 | 11 |
|
11 | | -from cps import logger, calibre_db, db |
| 12 | +from cps import logger, calibre_db, db, constants |
12 | 13 | from cps.search_metadata import cl as metadata_providers |
13 | 14 | import sys |
14 | 15 | sys.path.insert(1, '/app/calibre-web-automated/scripts/') |
@@ -297,75 +298,6 @@ def _apply_metadata_to_book(book, metadata, calibre_db_instance) -> bool: |
297 | 298 | return updated |
298 | 299 |
|
299 | 300 | except Exception as e: |
300 | | - log.error(f"Error applying metadata to book: {e}") |
301 | | - return False |
302 | | - if metadata.publisher and metadata.publisher.strip(): |
303 | | - publisher = calibre_db_instance.get_publisher_by_name(metadata.publisher.strip()) |
304 | | - if not publisher: |
305 | | - publisher = db.Publishers(name=metadata.publisher.strip()) |
306 | | - calibre_db_instance.session.add(publisher) |
307 | | - book.publishers.clear() |
308 | | - book.publishers.append(publisher) |
309 | | - updated = True |
310 | | - |
311 | | - # Update publication date if available |
312 | | - if metadata.publishedDate and metadata.publishedDate.strip(): |
313 | | - try: |
314 | | - from datetime import datetime |
315 | | - pub_date = datetime.strptime(metadata.publishedDate.strip(), "%Y-%m-%d") |
316 | | - book.pubdate = pub_date |
317 | | - updated = True |
318 | | - except ValueError: |
319 | | - pass # Invalid date format |
320 | | - |
321 | | - # Update series if available |
322 | | - if metadata.series and metadata.series.strip(): |
323 | | - series = calibre_db_instance.get_series_by_name(metadata.series.strip()) |
324 | | - if not series: |
325 | | - series = db.Series(name=metadata.series.strip()) |
326 | | - calibre_db_instance.session.add(series) |
327 | | - book.series.clear() |
328 | | - book.series.append(series) |
329 | | - if metadata.series_index: |
330 | | - try: |
331 | | - book.series_index = float(metadata.series_index) |
332 | | - except (ValueError, TypeError): |
333 | | - book.series_index = 1.0 |
334 | | - updated = True |
335 | | - |
336 | | - # Update tags if available |
337 | | - if metadata.tags and len(metadata.tags) > 0: |
338 | | - book.tags.clear() |
339 | | - for tag_name in metadata.tags: |
340 | | - if tag_name and tag_name.strip(): |
341 | | - tag = calibre_db_instance.get_tag_by_name(tag_name.strip()) |
342 | | - if not tag: |
343 | | - tag = db.Tags(name=tag_name.strip()) |
344 | | - calibre_db_instance.session.add(tag) |
345 | | - book.tags.append(tag) |
346 | | - updated = True |
347 | | - |
348 | | - # Update rating if available |
349 | | - if metadata.rating and float(metadata.rating) > 0: |
350 | | - rating = calibre_db_instance.get_rating_by_name(int(float(metadata.rating) * 2)) # Convert to 10-point scale |
351 | | - if rating: |
352 | | - book.ratings.clear() |
353 | | - book.ratings.append(rating) |
354 | | - updated = True |
355 | | - |
356 | | - # TODO: Handle cover image download and update |
357 | | - # This would require downloading the cover from metadata.cover URL |
358 | | - # and saving it to the book's directory |
359 | | - |
360 | | - if updated: |
361 | | - calibre_db_instance.session.commit() |
362 | | - log.info(f"Metadata successfully applied to book: {book.title}") |
363 | | - return True |
364 | | - else: |
365 | | - log.debug(f"No metadata improvements found for book: {book.title}") |
366 | | - return False |
367 | | - |
368 | | - except Exception as e: |
369 | | - log.error(f"Error applying metadata to book {book.id}: {e}") |
| 301 | + log.error(f"Error applying metadata to book {getattr(book, 'id', 'unknown')}: {e}") |
370 | 302 | calibre_db_instance.session.rollback() |
371 | 303 | return False |
0 commit comments