-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (50 loc) · 2.19 KB
/
app.py
File metadata and controls
54 lines (50 loc) · 2.19 KB
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
51
52
53
54
from flask import Flask,render_template,request
from newsapi import NewsApiClient
app = Flask(__name__)
newsapi = NewsApiClient(api_key='87eddeebf3f34e009bf55a7b327f8c19')
def get_sources_and_domains():
all_sources = newsapi.get()['sources']
sources = []
domains = []
for i in all_sources:
id = i['id']
domain = i['url'].replace("http://","")
domain = domain.replace("https://","")
domain = domain.replace("www.","")
slash = domain.find('/')
if slash != -1:
domain = domain[:slash]
sources.append(id)
domains.append(domain)
sources = ", ".join(sources)
domains = ", ".join(domains)
return sources,domains
@app.route('/',methods=['GET','POST'])
def home():
if request.method == 'POST':
sources, domains = get_sources_and_domains()
keyword = request.form["keyword"]
related_news = newsapi.get_everything(q=keyword, sources = sources, domains= domains, language='en',sort_by='relevancy')
no_of_articles = related_news['totalResults']
if no_of_articles > 100:
no_of_articles = 100
all_articles = newsapi.get_everything(q=keyword,
sources=sources,
domains=domains,
language='en',
sort_by='relevancy',
page_size=no_of_articles)['articles']
return render_template("home.html", all_articles=all_articles,
keyword=keyword)
else:
top_headlines = newsapi.get_top_headlines(country="in", language="en")
total_results = top_headlines['totalResults']
if total_results > 100:
total_results = 100
all_headlines = newsapi.get_top_headlines(country="in",
language="en",
page_size=total_results)['articles']
return render_template("home.html", all_headlines=all_headlines)
return render_template("home.html")
if __name__ == '__main__':
app.run(debug=True)