File size: 1,042 Bytes
9580dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import logging
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))

import httpx  # noqa: E402

from chokepoint.ingestion.gdelt import DEFAULT_QUERY, fetch_gdelt  # noqa: E402
from chokepoint.ingestion.normalize import gdelt_article_to_raw_document  # noqa: E402
from chokepoint.ingestion.store import append_documents  # noqa: E402

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
logger = logging.getLogger(__name__)


def main() -> int:
    try:
        articles = fetch_gdelt(DEFAULT_QUERY)
    except (httpx.HTTPError, httpx.TimeoutException) as error:
        logger.error("GDELT fetch failed: %s", error)
        return 1
    documents = [
        document
        for article in articles
        if (document := gdelt_article_to_raw_document(article)) is not None
    ]
    written = append_documents(documents, ROOT / "data/raw")
    print(f"new_documents={written}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())