news api caller refactored

This commit is contained in:
Neythen
2021-09-26 10:32:27 +01:00
parent 0ceb8c0f9c
commit c16f7eef5b
4 changed files with 28 additions and 19 deletions

View File

@@ -272,33 +272,41 @@ def updateForex():
def updateNews():
max_per_cat = 10
try:
all_settings = json.load(open('csv/news_settings.json', 'r'))
try:
#load user settings
headlines = []
settings = json.load(open('csv/news_settings.json', 'r'))
if all_settings['use_sources']:
arg_dict ={'sources':str(all_settings['sources'])}
else:
arg_dict = {'country': all_settings['country'], 'category':all_settings['category']}
for setting in settings:
h = newsapi.get_top_headlines(**setting)
if len(h) > max_per_cat:
h = h[0:max_per_cat]
headlines.append(h)
except:
h = newsapi.get_top_headlines(**arg_dict)
if len(h) > max_per_cat:
h = h[0:max_per_cat]
headlines.append(h)
except Exception as e:
print('news ettings not used', e)
#if no settings just get top headlines
headlines = newsapi.get_top_headlines()
headline_titles = [headline['title'] for headline in headlines['articles']]
headline_sources = [headline['source']['name'] for headline in headlines['articles']]
headline_times = [headline['publishedAt']for headline in headlines['articles']]
CSV = open('csv/news.csv', 'w+')
CSV.write('headline,source,date,time\n')
for i, title in enumerate(headline_titles):
date, time = headline_times[i].split('T')
CSV.write(title.replace(',', '^') + ',' + headline_sources[i] + ',' + date + ',' + time + '\n')
headlines = list(zip(headline_titles, headline_sources, headline_times))
all_settings['headlines'] = headlines
json.dump(all_settings, open('csv/news_settings.json', 'w+'))
CSV.close()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
@@ -578,10 +586,11 @@ if __name__ == '__main__':
max_stocks = 200
max_crypto = 100
updateSports()
sys.exit()
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
updateNews()
sys.exit()
update_frequencies = {'stocks':2, 'crypto':10, 'news':120, 'weather': 120, 'sports': 120} #minutes