-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
109 lines (92 loc) · 2.87 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import time
from flask import *
import requests
from keys import *
app = Flask(__name__)
global refresh_cap
refresh_cap = 0
# if this hits 4, we ping newsapi to get us new json
global total
total = 0
#total views/refreshes done
global news
news = ""
# This is the cache to stop newsapi to brick my apikey ;)
# add new urls
global url
global base_url
url = "https://newsapi.org/v2/top-headlines?apiKey=" + NEWS
base_url = "http://api.openweathermap.org/data/2.5/weather?appid=" + WEATHER
def get_news(urls):
response = requests.get(urls)
newsx = json.loads(response.text)
return newsx
@app.route('/')
def frontpage():
final_url = base_url
x = request.args
if x.get('country') is not None:
country = x.get('country')
final_url = url + "&q=" + country
refresh_cap = 0
if x.get('category') is not None:
category = x.get('category')
final_url = url + "&category=" + category
refresh_cap = 0
if x.get('sources') is not None:
sources = x.get('sources')
final_url = url + "&sources=" + sources
refresh_cap = 0
if x.get('from') is not None:
from_date = x.get('from')
final_url = url + "&from=" + from_date
refresh_cap = 0
if x.get('to') is not None:
to_date = x.get('to')
final_url = url + "&to=" + to_date
refresh_cap = 0
if x.get('domain') is not None:
domain = x.get('domain')
final_url = url + "&domain=" + domain
refresh_cap = 0
else:
final_url = url + "&q=india"
refresh_cap = refresh_cap + 1
global total
total = total + 1
if total == 1:
global news
news = get_news(final_url)
elif refresh_cap == 4:
news = get_news(final_url)
print(refresh_cap)
refresh_cap = 0
else:
pass
fontsize = request.args.get("fs")
if fontsize == None:
fontsize = 0
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip = request.environ['REMOTE_ADDR']
else:
ip = request.environ['HTTP_X_FORWARDED_FOR']
hmmm = "https://geolocation-db.com/json/"+ip+"position=true"
new = requests.get(hmmm).json()
city = new['city']
final_url = base_url + "&q=" + city
response = requests.get(final_url)
x = response.json()
if x["cod"] != "404":
y = x["main"]
current_temperature = y["temp"]
current_temperature = round(current_temperature - 273.15, 1)
far = (current_temperature * 9/5) + 32
current_pressure = y["pressure"]
current_humidity = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
else:
print(x)
return render_template('frontpage.html',far=far,city=city,news=news,f=fontsize,ct=current_temperature,cp=current_pressure,ch=current_humidity,cd=weather_description)
# Suvid Datta 2021
app.run(debug=True)