Add reverse to snapshots()

Add the option to get snapshots in reverse (from newest to oldest). This is especially useful since the snapshots() method returns a generator!
This commit is contained in:
Javier Maestro 2024-02-26 00:15:12 +00:00 committed by GitHub
parent 3b3e78d901
commit c1a39a3679
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -336,7 +336,7 @@ class WaybackMachineCDXServerAPI:
""" """
return self.near(year=1994, month=1, day=1) return self.near(year=1994, month=1, day=1)
def snapshots(self) -> Generator[CDXSnapshot, None, None]: def snapshots(self, reverse: bool = False) -> Generator[CDXSnapshot, None, None]:
""" """
This function yields the CDX data lines as snapshots. This function yields the CDX data lines as snapshots.
@ -373,6 +373,9 @@ class WaybackMachineCDXServerAPI:
# at once, thus there should be no issues of too much memory usage. # at once, thus there should be no issues of too much memory usage.
snapshot_list = entry.split("\n") snapshot_list = entry.split("\n")
if reverse:
snapshot_list.reverse()
for snapshot in snapshot_list: for snapshot in snapshot_list:
# 14 + 32 == 46 ( timestamp + digest ), ignore the invalid entries. # 14 + 32 == 46 ( timestamp + digest ), ignore the invalid entries.