|
| 1 | + |
1 | 2 | # -*- coding: utf-8 -*-
|
2 | 3 | """
|
3 | 4 | Created on Fri Mar 26 14:43:36 2021
|
4 | 5 |
|
5 | 6 | @author: DRASHTI
|
6 | 7 | """
|
7 | 8 |
|
8 |
| - |
9 | 9 | import hashlib
|
10 |
| - |
11 | 10 | from flask import Flask, render_template
|
12 | 11 | import requests
|
13 | 12 | import json
|
14 | 13 |
|
15 |
| -# enter your api key |
16 |
| -# I have used google-news-api |
17 |
| -api_key="18a186f151b642f7900ece5d626029f4" |
18 |
| -api_key="18a186f151b642f7900ece5d626029f4" |
| 14 | +# **Security:** Store your API key in a separate configuration file |
| 15 | +# or environment variable and avoid committing it to version control. |
| 16 | +# Here, we'll use a placeholder for demonstration. |
| 17 | +api_key = "YOUR_API_KEY" # Replace with your actual API key |
19 | 18 |
|
20 |
| -url="https://newsapi.org/v2/top-headlines?sources=google-news-in&apiKey="+ api_key |
21 |
| -response=response = requests.get(url) |
| 19 | +def fetch_news(category=None, query=None): |
| 20 | + url = "https://newsapi.org/v2/top-headlines" |
| 21 | + params = {"apiKey": api_key} |
| 22 | + if category: |
| 23 | + params["category"] = category |
| 24 | + if query: |
| 25 | + params["q"] = query # Use "q" parameter for search |
22 | 26 |
|
23 |
| -data = response.text |
24 |
| -parsed = json.loads(data) |
25 |
| -print(json.dumps(parsed, indent=4)) |
| 27 | + response = requests.get(url, params=params) |
| 28 | + data = response.text |
| 29 | + parsed = json.loads(data) |
| 30 | + return parsed |
26 | 31 |
|
27 |
| -app=Flask(__name__) |
| 32 | +app = Flask(__name__) |
28 | 33 |
|
29 |
| -#<img src="{{url_for()}}{{parsed['articles'][0]['urlToImage']}}"> |
30 | 34 | @app.route('/')
|
31 | 35 | def home():
|
32 |
| - url="https://newsapi.org/v2/top-headlines?sources=google-news-in&apiKey="+ api_key |
33 |
| - response=response = requests.get(url) |
34 |
| - |
35 |
| - data = response.text |
36 |
| - parsed = json.loads(data) |
37 |
| - print(json.dumps(parsed, indent=4)) |
38 |
| - print("##################################################################################") |
39 |
| - |
40 |
| - return render_template('headlines.html',parsed=parsed) |
| 36 | + parsed = fetch_news() # Fetch top headlines |
| 37 | + return render_template('headlines.html', articles=parsed["articles"]) |
41 | 38 |
|
42 | 39 | @app.route('/headlines')
|
43 | 40 | def headlines():
|
44 |
| - url="https://newsapi.org/v2/top-headlines?sources=google-news-in&apiKey="+ api_key |
45 |
| - response=response = requests.get(url) |
| 41 | + return home() # Redirect to home for headlines |
46 | 42 |
|
47 |
| - data = response.text |
48 |
| - parsed = json.loads(data) |
49 |
| - print(json.dumps(parsed, indent=4)) |
50 |
| - |
51 |
| - return render_template('headlines.html',parsed=parsed) |
52 | 43 | @app.route('/sports')
|
53 | 44 | def sports():
|
54 |
| - url="https://newsapi.org/v2/top-headlines?country=in&category=sports&apiKey="+ api_key |
55 |
| - response=response = requests.get(url) |
56 |
| - |
57 |
| - data = response.text |
58 |
| - parsed = json.loads(data) |
59 |
| - print(json.dumps(parsed, indent=4)) |
60 |
| - |
61 |
| - return render_template('sports.html',parsed=parsed) |
| 45 | + parsed = fetch_news(category="sports") |
| 46 | + return render_template('sports.html', articles=parsed["articles"]) |
62 | 47 |
|
63 | 48 | @app.route('/tech')
|
64 | 49 | def tech():
|
65 |
| - url="https://newsapi.org/v2/top-headlines?country=in&category=technology&apiKey="+ api_key |
66 |
| - response=response = requests.get(url) |
67 |
| - |
68 |
| - data = response.text |
69 |
| - parsed = json.loads(data) |
70 |
| - print(json.dumps(parsed, indent=4)) |
71 |
| - |
72 |
| - return render_template('tech.html',parsed=parsed) |
| 50 | + parsed = fetch_news(category="technology") |
| 51 | + return render_template('tech.html', articles=parsed["articles"]) |
73 | 52 |
|
74 | 53 | @app.route('/science')
|
75 | 54 | def science():
|
76 |
| - url="https://newsapi.org/v2/top-headlines?country=in&category=science&apiKey="+ api_key |
77 |
| - response=response = requests.get(url) |
| 55 | + parsed = fetch_news(category="science") |
| 56 | + return render_template('science.html', articles=parsed["articles"]) |
78 | 57 |
|
79 |
| - data = response.text |
80 |
| - parsed = json.loads(data) |
81 |
| - print(json.dumps(parsed, indent=4)) |
82 |
| - |
83 |
| - return render_template('science.html',parsed=parsed) |
84 | 58 | @app.route('/business')
|
85 | 59 | def business():
|
86 |
| - url="https://newsapi.org/v2/top-headlines?country=in&category=business&apiKey="+ api_key |
87 |
| - response=response = requests.get(url) |
88 |
| - |
89 |
| - data = response.text |
90 |
| - parsed = json.loads(data) |
91 |
| - print(json.dumps(parsed, indent=4)) |
92 |
| - |
93 |
| - return render_template('business.html',parsed=parsed) |
| 60 | + parsed = fetch_news(category="business") |
| 61 | + return render_template('business.html', articles=parsed["articles"]) |
94 | 62 |
|
95 | 63 | @app.route('/ent')
|
96 | 64 | def ent():
|
97 |
| - url="https://newsapi.org/v2/top-headlines?country=in&category=entertainment&apiKey="+ api_key |
98 |
| - response=response = requests.get(url) |
99 |
| - |
100 |
| - data = response.text |
101 |
| - parsed = json.loads(data) |
102 |
| - print(json.dumps(parsed, indent=4)) |
103 |
| - |
104 |
| - return render_template('ent.html',parsed=parsed) |
| 65 | + parsed = fetch_news(category="entertainment") |
| 66 | + return render_template('ent.html', articles=parsed["articles"]) |
105 | 67 |
|
106 | 68 | @app.route('/crypto')
|
107 | 69 | def crypto():
|
108 |
| - url="https://newsapi.org/v2/everything?q=bitcoin&apiKey="+ api_key |
109 |
| - response=response = requests.get(url) |
110 |
| - |
111 |
| - data = response.text |
112 |
| - parsed = json.loads(data) |
113 |
| - print(json.dumps(parsed, indent=4)) |
114 |
| - return render_template('crypto.html',parsed=parsed) |
| 70 | + parsed = fetch_news(query="bitcoin") |
| 71 | + return render_template('crypto.html', articles=parsed["articles"]) |
115 | 72 |
|
116 |
| -if __name__=='__main__': |
| 73 | +if __name__ == '__main__': |
117 | 74 | app.run()
|
0 commit comments