Reformatting
This commit is contained in:
@@ -1,32 +1,32 @@
|
||||
{
|
||||
"status": "ok",
|
||||
"totalResults": 1,
|
||||
"articles": [
|
||||
{
|
||||
"source": {
|
||||
"id": "the-verge",
|
||||
"name": "The Verge"
|
||||
},
|
||||
"author": "Justine Calma",
|
||||
"title": "EU Parliament backs off plans to phase out energy-hungry cryptocurrencies",
|
||||
"description": "EU Parliament abandoned a measure in its proposed legislative framework for regulating cryptocurrencies that would have amounted to a de facto ban on energy-hungry networks like Bitcoin.",
|
||||
"url": "https://www.theverge.com/2022/3/14/22977132/bitcoin-european-union-parliament-ban-proof-of-work-cryptocurrencies",
|
||||
"urlToImage": "https://cdn.vox-cdn.com/thumbor/8bE-uBwwu-eXg-CcB6cOqcAGVDw=/0x286:4000x2380/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/23315944/834392892.jpg",
|
||||
"publishedAt": "2022-03-14T23:40:25Z",
|
||||
"content": "But Bitcoin is still under scrutiny \r\nPower cords for bitcoin mining machines are plugged into electrical outlets at a mining facility operated by Bitmain Technologies Ltd. in Ordos, Inner Mongolia, \u2026 [+5797 chars]"
|
||||
},
|
||||
{
|
||||
"source": {
|
||||
"id": "the-verge",
|
||||
"name": "The Verge"
|
||||
},
|
||||
"author": "Justine Calma",
|
||||
"title": "EU Parliament backs off plans to phase out energy-hungry cryptocurrencies",
|
||||
"description": "EU Parliament abandoned a measure in its proposed legislative framework for regulating cryptocurrencies that would have amounted to a de facto ban on energy-hungry networks like Bitcoin.",
|
||||
"url": "https://www.theverge.com/2022/3/14/22977132/bitcoin-european-union-parliament-ban-proof-of-work-cryptocurrencies",
|
||||
"urlToImage": "https://cdn.vox-cdn.com/thumbor/8bE-uBwwu-eXg-CcB6cOqcAGVDw=/0x286:4000x2380/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/23315944/834392892.jpg",
|
||||
"publishedAt": "2022-03-14T23:40:25Z",
|
||||
"content": "But Bitcoin is still under scrutiny \r\nPower cords for bitcoin mining machines are plugged into electrical outlets at a mining facility operated by Bitmain Technologies Ltd. in Ordos, Inner Mongolia, \u2026 [+5797 chars]"
|
||||
}
|
||||
]
|
||||
"status": "ok",
|
||||
"totalResults": 1,
|
||||
"articles": [
|
||||
{
|
||||
"source": {
|
||||
"id": "the-verge",
|
||||
"name": "The Verge"
|
||||
},
|
||||
"author": "Justine Calma",
|
||||
"title": "EU Parliament backs off plans to phase out energy-hungry cryptocurrencies",
|
||||
"description": "EU Parliament abandoned a measure in its proposed legislative framework for regulating cryptocurrencies that would have amounted to a de facto ban on energy-hungry networks like Bitcoin.",
|
||||
"url": "https://www.theverge.com/2022/3/14/22977132/bitcoin-european-union-parliament-ban-proof-of-work-cryptocurrencies",
|
||||
"urlToImage": "https://cdn.vox-cdn.com/thumbor/8bE-uBwwu-eXg-CcB6cOqcAGVDw=/0x286:4000x2380/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/23315944/834392892.jpg",
|
||||
"publishedAt": "2022-03-14T23:40:25Z",
|
||||
"content": "But Bitcoin is still under scrutiny \r\nPower cords for bitcoin mining machines are plugged into electrical outlets at a mining facility operated by Bitmain Technologies Ltd. in Ordos, Inner Mongolia, \u2026 [+5797 chars]"
|
||||
},
|
||||
{
|
||||
"source": {
|
||||
"id": "the-verge",
|
||||
"name": "The Verge"
|
||||
},
|
||||
"author": "Justine Calma",
|
||||
"title": "EU Parliament backs off plans to phase out energy-hungry cryptocurrencies",
|
||||
"description": "EU Parliament abandoned a measure in its proposed legislative framework for regulating cryptocurrencies that would have amounted to a de facto ban on energy-hungry networks like Bitcoin.",
|
||||
"url": "https://www.theverge.com/2022/3/14/22977132/bitcoin-european-union-parliament-ban-proof-of-work-cryptocurrencies",
|
||||
"urlToImage": "https://cdn.vox-cdn.com/thumbor/8bE-uBwwu-eXg-CcB6cOqcAGVDw=/0x286:4000x2380/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/23315944/834392892.jpg",
|
||||
"publishedAt": "2022-03-14T23:40:25Z",
|
||||
"content": "But Bitcoin is still under scrutiny \r\nPower cords for bitcoin mining machines are plugged into electrical outlets at a mining facility operated by Bitmain Technologies Ltd. in Ordos, Inner Mongolia, \u2026 [+5797 chars]"
|
||||
}
|
||||
]
|
||||
}
|
@@ -3,23 +3,22 @@ script for news fetching (by keywords)
|
||||
"""
|
||||
__author__ = "Florian Kellermann, Linus Eickhoff"
|
||||
__date__ = "26.04.2022"
|
||||
__version__ = "1.0.0"
|
||||
__version__ = "1.0.0"
|
||||
__license__ = "None"
|
||||
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
import sys
|
||||
|
||||
import helper_functions as hf
|
||||
|
||||
from newsapi import NewsApiClient
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
from newsapi import NewsApiClient
|
||||
|
||||
load_dotenv() # loads environment vars
|
||||
load_dotenv() # loads environment vars
|
||||
|
||||
# Init
|
||||
api_key = os.getenv('NEWS_API_KEY') # get API Key from .env file
|
||||
newsapi = NewsApiClient(api_key=api_key) # news api from https://newsapi.org/
|
||||
api_key = os.getenv('NEWS_API_KEY') # get API Key from .env file
|
||||
newsapi = NewsApiClient(api_key=api_key) # news api from https://newsapi.org/
|
||||
|
||||
try:
|
||||
# get all available news sources (e.g BBC, New York Times, etc.)
|
||||
@@ -29,8 +28,9 @@ try:
|
||||
|
||||
except KeyError:
|
||||
print("Error: Could not get sources, may be blocked because of too many requests (free newsapi is limited to 100 reqs per day)")
|
||||
str_sources = str("Reuters, bbc, cnn, fox-news, google-news, hacker-news, nytimes, the-huffington-post, the-new-york-times, business-insider, bbc-news, cbc-news, ESPN, fox-sports, google-news-uk, independent, the-wall-street-journal, the-washington-times, time, usa-today")
|
||||
|
||||
str_sources = str(
|
||||
"Reuters, bbc, cnn, fox-news, google-news, hacker-news, nytimes, the-huffington-post, the-new-york-times, business-insider, bbc-news, cbc-news, ESPN, fox-sports, google-news-uk, independent, the-wall-street-journal, the-washington-times, time, usa-today")
|
||||
|
||||
|
||||
def get_all_news_by_keyword(keyword, from_date="2000-01-01"):
|
||||
"""get all news to keyword
|
||||
@@ -41,8 +41,8 @@ def get_all_news_by_keyword(keyword, from_date="2000-01-01"):
|
||||
Returns:
|
||||
JSON/dict: dict containing articles
|
||||
"""
|
||||
top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) # keywords can be combined with OR (e.g. keyword = "bitcoin OR ethereum")
|
||||
if(top_headlines["status"] == "ok"):
|
||||
top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) # keywords can be combined with OR (e.g. keyword = "bitcoin OR ethereum")
|
||||
if (top_headlines["status"] == "ok"):
|
||||
return top_headlines
|
||||
else:
|
||||
return None
|
||||
@@ -56,8 +56,8 @@ def get_top_news_by_keyword(keyword):
|
||||
Returns:
|
||||
JSON/dict: dict containing articles
|
||||
"""
|
||||
top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') # get top headlines, measured by popularity from NewsApi
|
||||
if(top_headlines["status"] == "ok"):
|
||||
top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') # get top headlines, measured by popularity from NewsApi
|
||||
if (top_headlines["status"] == "ok"):
|
||||
return top_headlines
|
||||
else:
|
||||
return None
|
||||
@@ -71,24 +71,24 @@ def format_article(article):
|
||||
|
||||
Returns:
|
||||
String: formatted article
|
||||
"""
|
||||
sourcename = hf.make_markdown_proof(article["source"]["name"]) # make attributes markdownv2 proof
|
||||
"""
|
||||
sourcename = hf.make_markdown_proof(article["source"]["name"]) # make attributes markdownv2 proof
|
||||
headline = hf.make_markdown_proof(article["title"])
|
||||
url = hf.make_markdown_proof(article["url"])
|
||||
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax
|
||||
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax
|
||||
|
||||
return formatted_article
|
||||
|
||||
|
||||
if __name__ == '__main__': # only execute if script is called directly -> for simple testing
|
||||
if __name__ == '__main__': # only execute if script is called directly -> for simple testing
|
||||
|
||||
print("this is a module and should not be run directly")
|
||||
print("fetching top news by keyword bitcoin...")
|
||||
|
||||
|
||||
articles = get_all_news_by_keyword("bitcoin")
|
||||
formatted_article = format_article(articles["articles"][0])
|
||||
print(formatted_article)
|
||||
articles = get_top_news_by_keyword("bitcoin")
|
||||
formatted_article = format_article(articles["articles"][0])
|
||||
print(formatted_article)
|
||||
sys.exit(1)
|
||||
sys.exit(1)
|
||||
|
Reference in New Issue
Block a user