File size: 1,392 Bytes
208266a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from src.db.vector_store import NewsVectorStore 
from newsapi import NewsApiClient
from src.config import NEWS_API_KEY


def fetch_news(topic="AI regulation", lang="en", page_size=10):
    if not NEWS_API_KEY:
        raise RuntimeError("NEWSAPI_KEY is not configured. Add it to .env before using /ingest.")

    news_instance = NewsApiClient(api_key=NEWS_API_KEY)
    try: 
        print("Fetching latest articles...")
        response = news_instance.get_everything(q=topic, language=lang, sort_by='relevancy', page_size=page_size)

        if response['status'] == 'ok':
            articles = response['articles']
            if not articles:
                print("No articles found.")
                return

            print(f"Successfully fetched {len(articles)} articles.")
            print("-" * 40)

            return articles
        else:
            print(f"API Error: {response.get('message', 'Unknown error')}")
            return []
    
    except Exception as e:
        print(f"Pipeline failed: {str(e)}")
        return []

def run_pipeline():
    print("Fetching articles...")
    articles = fetch_news()

    if not articles:
        print("No articles found.")
        return

    print(f"Fetched {len(articles)} articles.")

    db = NewsVectorStore()
    db.store_articles(articles)

    print("Pipeline complete.")

if __name__ == "__main__":
    run_pipeline()