Fix problem of invalid datetime metadata coming from a device.

1) Catch errors in the smart device driver
2) Return None in the json_codec if a date cannot be parsed.
This commit is contained in:
Charles Haley 2014-12-15 18:47:06 +01:00
parent 4f6ca8d7d2
commit f98e92ab74
2 changed files with 22 additions and 15 deletions

View File

@ -1308,18 +1308,22 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self._debug('getting book metadata. Done', i, 'of', count) self._debug('getting book metadata. Done', i, 'of', count)
opcode, result = self._receive_from_client(print_debug_info=False) opcode, result = self._receive_from_client(print_debug_info=False)
if opcode == 'OK': if opcode == 'OK':
if '_series_sort_' in result: try:
del result['_series_sort_'] if '_series_sort_' in result:
book = self.json_codec.raw_to_book(result, SDBook, self.PREFIX) del result['_series_sort_']
book.set('_is_read_', result.get('_is_read_', None)) book = self.json_codec.raw_to_book(result, SDBook, self.PREFIX)
book.set('_sync_type_', result.get('_sync_type_', None)) book.set('_is_read_', result.get('_is_read_', None))
book.set('_last_read_date_', result.get('_last_read_date_', None)) book.set('_sync_type_', result.get('_sync_type_', None))
bl.add_book_extended(book, replace_metadata=True, book.set('_last_read_date_', result.get('_last_read_date_', None))
check_for_duplicates=not self.client_cache_uses_lpaths) bl.add_book_extended(book, replace_metadata=True,
if '_new_book_' in result: check_for_duplicates=not self.client_cache_uses_lpaths)
book.set('_new_book_', True) if '_new_book_' in result:
else: book.set('_new_book_', True)
self._set_known_metadata(book) else:
self._set_known_metadata(book)
except:
self._debug('exception retrieving metadata for book', result.get('title', 'Unknown'))
traceback.print_exc()
else: else:
raise ControlError(desc='book metadata not returned') raise ControlError(desc='book metadata not returned')

View File

@ -17,9 +17,12 @@ from calibre import isbytestring
# UTC. The returned date is also UTC # UTC. The returned date is also UTC
def string_to_datetime(src): def string_to_datetime(src):
from calibre.utils.date import parse_date from calibre.utils.date import parse_date
if src == "None": if src != "None":
return None try:
return parse_date(src) return parse_date(src)
except:
pass
return None
def datetime_to_string(dateval): def datetime_to_string(dateval):
from calibre.utils.date import isoformat, UNDEFINED_DATE, local_tz from calibre.utils.date import isoformat, UNDEFINED_DATE, local_tz