Spaces:
Sleeping
Sleeping
Create ingest/generic_public_foia.py
Browse files
adapters/ingest/generic_public_foia.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import abc
|
| 2 |
+
import asyncio
|
| 3 |
+
import time
|
| 4 |
+
from typing import List, Dict
|
| 5 |
+
|
| 6 |
+
class GenericFOIAAdapter(abc.ABC):
|
| 7 |
+
source_name: str = "UNKNOWN"
|
| 8 |
+
base_url: str = ""
|
| 9 |
+
is_live: bool = True
|
| 10 |
+
|
| 11 |
+
def __init__(self, rate_limit_seconds: float = 1.0):
|
| 12 |
+
self.rate_limit_seconds = rate_limit_seconds
|
| 13 |
+
self._last_call = 0.0
|
| 14 |
+
|
| 15 |
+
async def _rate_limit(self):
|
| 16 |
+
now = time.monotonic()
|
| 17 |
+
delta = now - self._last_call
|
| 18 |
+
if delta < self.rate_limit_seconds:
|
| 19 |
+
await asyncio.sleep(self.rate_limit_seconds - delta)
|
| 20 |
+
self._last_call = time.monotonic()
|
| 21 |
+
|
| 22 |
+
@abc.abstractmethod
|
| 23 |
+
async def search(self, query: str) -> List[Dict]:
|
| 24 |
+
"""
|
| 25 |
+
Must return:
|
| 26 |
+
{
|
| 27 |
+
source, title, url, snippet, agency
|
| 28 |
+
}
|
| 29 |
+
"""
|
| 30 |
+
raise NotImplementedError
|