mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 04:16:24 -05:00
Apply the offset in a SingleFile saved date, and repair misread Archive dates (#862)
* Apply the offset in a SingleFile saved date
SingleFile writes the browser's local time with its offset ("... 18:36:23 GMT-0700"). The parser dropped
the offset and treated the local time as UTC, so an uploaded Archive's download_datetime was off by the
browser's UTC offset, and a Collection reorganize renamed its files with that shifted timestamp.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Repair Archive download datetimes misread from a SingleFile saved date
Archives parsed before the offset fix keep the shifted download_datetime, because the parser only fills
an empty value. A one-time background repair (gated by a wrolpi_kv version, after the first refresh)
re-reads each Archive's saved date and corrects a download_datetime that equals the offset-dropped value.
Any other download_datetime is left alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Read the whole SingleFile header; finish the date repair only when every Archive was read
The header was read in 1000 bytes, so a long URL cut the saved date's offset off, and the cut-off date was
stored as GMT. The header is now read through the end of its comment, and a malformed offset leaves the
date unset instead of guessing.
The repair is no longer marked done when an Archive could not be read (a missing file is a deleted Archive,
not a failure), and it waits for the media to be mounted, because `refresh_complete` is restored from the
database before the drives are checked.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1f3762ff20
commit
da3dadde07
5 changed files with 275 additions and 34 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import json
|
||||
import pathlib
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Iterable, List, Optional
|
||||
|
||||
import pytz
|
||||
|
|
@ -27,6 +28,41 @@ MATCH_URL = re.compile(r'^\s+?url:\s+?(http.*)', re.MULTILINE)
|
|||
MATCH_DATE = re.compile(r'^\s+?saved date:\s+?(.*)', re.MULTILINE)
|
||||
|
||||
|
||||
# A SingleFile header holds the page's URL, which may be very long.
|
||||
SINGLEFILE_HEADER_MAX_BYTES = 64 * 1024
|
||||
|
||||
|
||||
def read_singlefile_header(path: pathlib.Path) -> Optional[str]:
|
||||
"""Return the start of a SingleFile, through the end of its header comment, or None if the header comment
|
||||
does not end within SINGLEFILE_HEADER_MAX_BYTES. A partial header could cut off its URL or saved date."""
|
||||
head = b''
|
||||
with path.open('rb') as fh:
|
||||
# Read bytes because a compressed (SingleFileZ) singlefile has a binary ZIP tail.
|
||||
while len(head) < SINGLEFILE_HEADER_MAX_BYTES and (chunk := fh.read(1000)):
|
||||
head += chunk
|
||||
if b'-->' in head:
|
||||
return head.decode(errors='ignore')
|
||||
return None
|
||||
|
||||
|
||||
def parse_singlefile_saved_date(saved_date: str) -> Optional[datetime]:
|
||||
"""Parse SingleFile's "saved date", which is the browser's local time and its offset, into UTC.
|
||||
|
||||
Returns None if the offset is malformed; the instant is unknown.
|
||||
|
||||
>>> parse_singlefile_saved_date('Thu Nov 07 2024 18:36:23 GMT-0700 (Mountain Standard Time)')
|
||||
datetime.datetime(2024, 11, 8, 1, 36, 23, tzinfo=<UTC>)
|
||||
"""
|
||||
parts = saved_date.split(' ')
|
||||
if len(parts) < 6:
|
||||
# No offset; assume GMT.
|
||||
return dates.strpdate(' '.join(parts[:5])).replace(tzinfo=pytz.timezone('GMT'))
|
||||
try:
|
||||
return datetime.strptime(' '.join(parts[:6]), '%a %b %d %Y %H:%M:%S GMT%z').astimezone(pytz.UTC)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
class Archive(Base, ModelHelper):
|
||||
__tablename__ = 'archive'
|
||||
__table_args__ = (
|
||||
|
|
@ -314,30 +350,24 @@ class Archive(Base, ModelHelper):
|
|||
# This data has already been read.
|
||||
return
|
||||
|
||||
with path.open('rb') as fh:
|
||||
# Read bytes because a compressed (SingleFileZ) singlefile has a binary ZIP tail.
|
||||
head = fh.read(1000).decode(errors='ignore')
|
||||
if 'Page saved with SingleFile' not in head:
|
||||
logger.error(f'Could not find SingleFile header in {self}')
|
||||
return
|
||||
head = read_singlefile_header(path)
|
||||
if not head or 'Page saved with SingleFile' not in head:
|
||||
logger.error(f'Could not find SingleFile header in {self}')
|
||||
return
|
||||
|
||||
if not self.file_group.url:
|
||||
try:
|
||||
if match := MATCH_URL.findall(head):
|
||||
self.file_group.url = match[0].strip()
|
||||
except Exception as e:
|
||||
logger.error(f'Could not get URL from singlefile {path}', exc_info=e)
|
||||
if not self.file_group.url:
|
||||
try:
|
||||
if match := MATCH_URL.findall(head):
|
||||
self.file_group.url = match[0].strip()
|
||||
except Exception as e:
|
||||
logger.error(f'Could not get URL from singlefile {path}', exc_info=e)
|
||||
|
||||
if not self.file_group.download_datetime:
|
||||
try:
|
||||
if match := MATCH_DATE.findall(head):
|
||||
dt = match[0].strip()
|
||||
dt = ' '.join(dt.split(' ')[:5])
|
||||
# SingleFile uses GMT.
|
||||
dt = dates.strpdate(dt).replace(tzinfo=pytz.timezone('GMT'))
|
||||
self.file_group.download_datetime = dt
|
||||
except Exception as e:
|
||||
logger.error(f'Could not get archive date from singlefile {path}', exc_info=e)
|
||||
if not self.file_group.download_datetime:
|
||||
try:
|
||||
if match := MATCH_DATE.findall(head):
|
||||
self.file_group.download_datetime = parse_singlefile_saved_date(match[0].strip())
|
||||
except Exception as e:
|
||||
logger.error(f'Could not get archive date from singlefile {path}', exc_info=e)
|
||||
|
||||
def apply_domain(self):
|
||||
"""Get the domain collection from the URL."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue