Browse Source

[wip] Try to handle timezone information

... but the "closest" timestamp seems to be the same as before when it
should be shifted
jherve 1 year ago
parent
commit
4ceb2e98cd

+ 4 - 3
src/de_quoi_parle_le_monde/internet_archive.py

@@ -1,6 +1,6 @@
 from attrs import frozen
 from typing import Optional, ClassVar, NewType
-from datetime import date, datetime, timedelta
+from datetime import date, datetime, timedelta, timezone
 import cattrs
 
 from de_quoi_parle_le_monde.http import HttpSession
@@ -123,12 +123,13 @@ class InternetArchiveClient:
             # also allows to always send a new actual request and not
             # hit the cache, but this is obviously an implementation detail
             # of the HTTP layer that this client should not be aware of..
-            to_=min(dt + timedelta(hours=6.0), datetime.now()),
+            to_=min(dt + timedelta(hours=6.0), datetime.now(tz=timezone.utc)),
             filter="statuscode:200",
         )
 
         all_snaps = await self.search_snapshots(req)
         if all_snaps:
-            return min(all_snaps, key=lambda s: abs(s.timestamp - dt))
+            s = min(all_snaps, key=lambda s: abs(s.timestamp.astimezone(timezone.utc) - dt))
+            return s
         else:
             raise SnapshotNotYetAvailable(dt)

+ 8 - 4
src/de_quoi_parle_le_monde/main.py

@@ -1,6 +1,7 @@
-from datetime import date, datetime, time, timedelta
+from datetime import date, datetime, time, timedelta, timezone
 import asyncio
 from attrs import frozen
+from zoneinfo import ZoneInfo
 
 from de_quoi_parle_le_monde.http import HttpClient
 from de_quoi_parle_le_monde.internet_archive import (
@@ -17,9 +18,12 @@ class ArchiveDownloader:
 
     @staticmethod
     def last_n_days_at_hours(n: int, hours: list[int]) -> list[datetime]:
+        def local_time(h):
+            return time(hour=h, tzinfo=ZoneInfo("Europe/Paris"))
+
         return [
-            datetime.combine(date.today() - timedelta(days=i), time(hour=h))
-            for i in range(1, n)
+            datetime.combine(date.today() - timedelta(days=i), local_time(h)).astimezone(timezone.utc)
+            for i in range(0, n)
             for h in hours
         ]
 
@@ -60,7 +64,7 @@ class ArchiveDownloader:
 
 async def main(dler: ArchiveDownloader):
     storage = await Storage.create()
-    dts = ArchiveDownloader.last_n_days_at_hours(10, [18])
+    dts = ArchiveDownloader.last_n_days_at_hours(10, [8, 12, 18, 22])
 
     async with dler.client.session() as session:
         ia = InternetArchiveClient(session)