ソースを参照

Move client instantiation into main

jherve 1 年間 前
コミット
5e6ec0355e

+ 4 - 6
src/de_quoi_parle_le_monde/internet_archive.py

@@ -68,20 +68,18 @@ class InternetArchiveSnapshot:
         return InternetArchiveSnapshot(timestamp=rec.timestamp, original=rec.original)
 
 
+@frozen
 class InternetArchiveClient:
     # https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server
-
-    def __init__(self):
-        self.client = HttpClient()
+    client: HttpClient
+    search_url: ClassVar[str] = "http://web.archive.org/cdx/search/cdx"
 
     async def search_snapshots(self, req: CdxRequest):
         def to_snapshot(line):
             record = CdxRecord.parse_line(line)
             return InternetArchiveSnapshot.from_record(record)
 
-        resp = await self.client.aget(
-            "http://web.archive.org/cdx/search/cdx?", req.into_params()
-        )
+        resp = await self.client.aget(self.search_url, req.into_params())
 
         return [to_snapshot(line) for line in resp.splitlines()]
 

+ 3 - 1
src/de_quoi_parle_le_monde/main.py

@@ -7,8 +7,10 @@ from de_quoi_parle_le_monde.le_monde import LeMondeArchive, LeMondeMainPage
 
 
 async def get_latest_snaps():
+    http_client = HttpClient()
+    ia = InternetArchiveClient(http_client)
+
     dates = [date.today() - timedelta(days=n) for n in range(0, 10)]
-    ia = InternetArchiveClient()
 
     async def req_and_parse_first_snap(date):
         req = CdxRequest(