-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
118 lines (85 loc) · 3.81 KB
/
test.py
File metadata and controls
118 lines (85 loc) · 3.81 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
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
110
111
112
113
114
115
116
117
from pymongo import MongoClient
import datetime
import requests
import json
class weather():
freegeoip = "http://freegeoip.net/json"
weather_url = 'http://api.openweathermap.org/data/2.5/weather?'
api_id = '6b35061c42fdbe1965095189659a796b'
q = ""
request_url = weather_url + 'appid=' + api_id + "&q="
json_object = ""
current_conditions = ""
def __init__(self):
print "Weather APP\n"
client = MongoClient('localhost', 27017)
db = client.weather
self.collection = db.weather_collection
def get_user_automatic(self):
r = requests.get(self.freegeoip)
j = json.loads(r.text)
current_country = j['time_zone']
current_city = current_country.split("/")[1]
print current_city,"\n"
"""
while True:
r = requests.get(self.freegeoip)
jsonResult = json.loads(r.text)
if jsonResult["country_code"] == "US":
if jsonResult["zip_code"] != "":
self.sort_stuff(jsonResult["zip_code"], True)
break
if jsonResult["city"] != "":
self.sort_stuff(jsonResult["city"], True)
break
if jsonResult["time_zone"] != "":
#call function
break
raise Exception()
"""
def sort_stuff(self, val, boolean):
if boolean == True:
pass
else:
loc = val.split('/')[1]
with open('C:\\Users\\HP-PC\\Downloads\\city.list.json') as data_file:
data = json.load(data_file)
data[loc]["id"]
print ""
def get_user_location(self):
print "Enter the name of the city which you want to display the current weather forecast"
self.q = raw_input("City Name: ")
self.request_url = self.request_url + self.q
def call_api(self):
self.json_object = requests.get(self.request_url).json()
def process_json(self):
temp_kelvin = float(self.json_object['main']['temp'])
self.current_conditions = self.json_object['weather'][0]['description']
temp_celsius = temp_kelvin - 273.15
print "The current temperature in", self.q, "is", str(temp_celsius), "celsius and the current conditions are", self.current_conditions,"\n"
def five_day_forecast(self):
five_day_url = 'http://api.openweathermap.org/data/2.5/forecast?q='+self.q+'&appid=6b35061c42fdbe1965095189659a796b'
json_object_five = requests.get(five_day_url).json()
forecast = json_object_five['list']
print "FIVE DAY FORECAST\n"
for items in forecast:
description = items['weather'][0]['description']
date = items['dt_txt']
day_temp = items['main']['temp']
day_temp_celsius = day_temp - 273.15
print "Date: ", date,"\n", "Description: ", description,"\n", "Temperature: ",day_temp_celsius,"\n"
def update_database(self):
now = datetime.datetime.now()
time_of_call = now.strftime("%H:%M")
self.collection.insert_one({"City": self.q,
"Weather Condition": self.current_conditions,
"time": time_of_call
})
if __name__ == "__main__":
weather = weather()
weather.get_user_automatic()
weather.get_user_location()
weather.call_api()
weather.process_json()
weather.five_day_forecast()
weather.update_database()