-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.py
40 lines (32 loc) · 1.14 KB
/
news.py
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
import requests
import json
from config import SERPER_API_KEY # Import API key securely
from aria import ask_aria # Import the ask_aria function from aria.py
from flask import Flask, render_template, jsonify
app = Flask(__name__)
def fetch_news(query):
url = 'https://google.serper.dev/news'
headers = {
'X-API-KEY': SERPER_API_KEY,
'Content-Type': 'application/json'
}
data = {
'q': query
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json() # Return the JSON response
else:
print(f"Error fetching news: {response.status_code} - {response.text}")
return None
def process_news(news_data):
messages = []
if "news" in news_data:
for item in news_data["news"]:
title = item.get("title")
snippet = item.get("snippet")
message = f"Title: \nSnippet: {snippet}\n"
messages.append(message)
return "\n".join(messages) # Return the combined messages
if __name__ == '__main__':
app.run(debug=True)