-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
61 lines (48 loc) · 1.76 KB
/
app.py
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
55
56
57
58
59
60
61
from flask import Flask, render_template, request
from newsapi import NewsApiClient
import requests
import json
import os
app = Flask(__name__)
key1 = os.environ.get("key1")
key2 = os.environ.get("key2")
# Home route
@app.route('/',methods=["GET"])
def my_app():
return render_template('index.html')
# Search via city name route
@app.route('/search',methods=["POST"])
def myfun():
cityName = request.form.get("cityName")
url = f"https://api.openweathermap.org/data/2.5/weather?q={cityName}&appid={key1}&units=metric"
data = requests.get(url)
# Converting into python object
myData = json.loads(data.content)
img = myData["weather"][0]["icon"]
return render_template("weather.html",city=cityName,data=myData,img=img)
# Search via pincode route
@app.route('/search-pincode',methods=["POST"])
def myfunction():
pincode = request.form.get("pincode")
url = f"https://api.openweathermap.org/data/2.5/weather?zip={pincode}&appid={key1}&units=metric"
data = requests.get(url)
# Converting into python object
myData = json.loads(data.content)
image = myData["weather"][0]["icon"]
# print(img)
return render_template("weather_pincode.html",pin=pincode,data=myData,image=image)
# News route
@app.route("/news",methods=["GET"])
def newsfun():
url = (f'https://newsapi.org/v2/top-headlines?q=politics&from=2022-09-10&apiKey={key2}')
response = requests.get(url)
newsData = json.loads(response.content)
return render_template("news.html",data=newsData["articles"])
@app.errorhandler(500)
def page_not_found(e):
return render_template("500.html"), 500
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
if __name__ == '__main__':
app.run(debug=True, port=5000)