define __len__ on Url objects, if any method not used prior to len op then default to len of oldest archive.

This commit is contained in:
Akash Mahanty 2022-01-16 21:29:43 +05:30
parent c8d0ad493a
commit 2e487e88d3

View File

@ -3,6 +3,7 @@ from .availability_api import WaybackMachineAvailabilityAPI
from .cdx_api import WaybackMachineCDXServerAPI from .cdx_api import WaybackMachineCDXServerAPI
from .utils import DEFAULT_USER_AGENT from .utils import DEFAULT_USER_AGENT
from .exceptions import WaybackError from .exceptions import WaybackError
from datetime import datetime, timedelta
class Url: class Url:
@ -19,6 +20,19 @@ class Url:
self.newest() self.newest()
return self.archive_url return self.archive_url
def __len__(self):
td_max = timedelta(
days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999
)
if not self.timestamp:
self.oldest()
if self.timestamp == datetime.max:
return td_max.days
return (datetime.utcnow() - self.timestamp).days
def save(self): def save(self):
self.wayback_machine_save_api = WaybackMachineSaveAPI( self.wayback_machine_save_api = WaybackMachineSaveAPI(
self.url, user_agent=self.user_agent self.url, user_agent=self.user_agent