news api caller refactored
This commit is contained in:
parent
0ceb8c0f9c
commit
c16f7eef5b
@ -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
|
||||
|
||||
|
@ -1 +1 @@
|
||||
{"feature": "News", "speed": "medium", "animation": "continuous", "country": "USA", "category": "general", "title": true, "use_sources":true, "sources": {"Bloomberg": -1, "ABC News": -1, "Yahoo": -1}}
|
||||
{"feature": "News", "speed": "medium", "animation": "continuous", "country": "USA", "category": "general", "title": true, "use_sources": true, "sources": ["Yahoo", "ABC News", "Bloomberg"], "headlines": [["Missing women in Hunua Ranges found safe and uninjured - New Zealand Herald", "New Zealand Herald", "2021-09-26T08:36:41Z"], ["Labour Conference 2021 \u2013 live: Rayner defends calling Tory ministers \u2018scum\u2019, as infighting mars first day - The Independent", "Independent", "2021-09-26T08:30:06Z"], ["Eleazar wants review of PNPA policies after cadet's death - GMA News Online", "GMA News", "2021-09-26T08:13:43Z"], ["Highlights, Australia Women vs India Women 3rd ODI, .. Score: India end Australias streak with thrilling win - Firstpost", "Firstpost", "2021-09-26T08:10:38Z"], ["Supply crisis: Grant Shapps urges motorists to fill up only 'when they need to' - Sky News", "Sky.com", "2021-09-26T08:08:57Z"], ["One person dead after single-vehicle collision in Southland - Stuff.co.nz", "Stuff.co.nz", "2021-09-26T08:05:00Z"], ["Pair charged over violent picnic attack - Herald Sun", "Newscorpaustralia.com", "2021-09-26T08:04:56Z"], ["NASA selects Moon's Nobile Crater for ice-hunting rover - eNCA", "eNCA", "2021-09-26T07:45:00Z"], ["118 more cops sick with COVID-19 \u2014PNP - GMA News Online", "GMA News", "2021-09-26T07:42:07Z"], ["Bottas hit with Russian GP grid penalty after F1 engine change - Motorsport.com", "Motorsport.com", "2021-09-26T07:36:38Z"], ["Direct Provision: limbo continues for thousands with no idea of when they'll get out - TheJournal.ie", "TheJournal.ie", "2021-09-26T07:33:47Z"], ["Evolving strategic reality in Indo-Pacific may make India go the Australia way - Hindustan Times", "Hindustan Times", "2021-09-26T07:31:41Z"], ["MACC seizes over RM1.6mil in assets linked to its senior officer, says report - The Star Online", "The Star Online", "2021-09-26T07:25:00Z"], ["Anthony Joshua 'will bounce back' and take rematch after Oleksandr Usyk defeat - BBC Sport", "BBC News", "2021-09-26T07:20:24Z"], ["Sabina Nessa murder: Police arrest man in Sussex in 'significant development' - The Telegraph", "Telegraph.co.uk", "2021-09-26T07:20:00Z"], ["\u2018Vulnerable\u2019: $100b subs could be useless - NEWS.com.au", "News.com.au", "2021-09-26T07:19:54Z"], ["Sir John Key is being provocative but his ideas make sense, writes Jack Tame - TVNZ", "1News", "2021-09-26T07:18:45Z"], ["Kashmir Police shoot dead man who had killed BJP leader & family last year - The Indian Express", "The Indian Express", "2021-09-26T07:15:40Z"], ["Old faces of New Labour in Keir Starmer\u2019s inner circle - The Guardian", "The Guardian", "2021-09-26T07:15:00Z"], ["India needs 4 or 5 more banks like State Bank of India (SBI): Finance Minister Nirmala Sitharaman - Mint", "Livemint", "2021-09-26T07:12:57Z"]]}
|
@ -440,7 +440,7 @@ def save_news_settings(input_settings):
|
||||
current_settings['country'] = input_settings['country']
|
||||
|
||||
|
||||
current_settings = combine_dict(current_settings, input_settings['sources'], 'sources')
|
||||
current_settings['sources'] = list(set(current_settings['sources'] + input_settings['sources']))
|
||||
|
||||
json.dump(current_settings, open('csv/' + filename, 'w+'))
|
||||
|
||||
|
@ -1379,7 +1379,7 @@
|
||||
id="news-features"
|
||||
class="display-features-list text-dark sources-list"
|
||||
>
|
||||
{% for f in news_settings.sources.keys() %}
|
||||
{% for f in news_settings.sources %}
|
||||
<li>{{f}}</li>
|
||||
{% endfor%}
|
||||
</ul>
|
||||
|
Loading…
Reference in New Issue
Block a user