-
Notifications
You must be signed in to change notification settings - Fork 1
/
Process.py
65 lines (44 loc) · 1.94 KB
/
Process.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
import requests
import pymongo
def collect():
# These code snippets use an open-source library. http://unirest.io/python
response = requests.get("https://newsrain-petrol-diesel-prices-india-v1.p.mashape.com/capitals/history",
headers={
"X-Mashape-Key": "6doJchD6BsmshvPoZEwpowzZ8Rtep1xQLFRjsnopSWmfSXAed8",
"Accept": "application/json"
}
)
return response.json()
response_data_dict = collect()
response_date = response_data_dict["date"]
client = pymongo.MongoClient()
db = client.ml_fuel;
data = db['monthly_data']
cursor = data.find({'date':response_date})
if cursor.count()==0:
#If data of response not already present
data.insert_one(response_data_dict)
print("Done...recorded raw data")
proc_data_petrol = db['processed_data_petrol']
proc_data_diesel = db['processed_data_diesel']
labels_list = response_data_dict['history']['labels']
dataset_petrol_list = response_data_dict['history']['datasets'][0]["data"]
dataset_diesel_list = response_data_dict['history']['datasets'][1]["data"]
dict_petrol = {'obtained_on':response_date}
dict_diesel = {'obtained_on':response_date}
for label,data in zip(labels_list,dataset_petrol_list):
dict_petrol[label]=float(data)
for label,data in zip(labels_list,dataset_diesel_list):
dict_diesel[label]=float(data)
proc_data_petrol.insert_one(dict_petrol)
proc_data_diesel.insert_one(dict_diesel)
#City wise extraction
db_city_wise = db['city_wise_price']
cities_list_of_objects = response_data_dict['cities']
for object in cities_list_of_objects:
db_city_wise.insert_one(object)
print("Processed.....")
db_checkLastUpdated = db['checkLastUpdated']
db_checkLastUpdated.update_one({"_id":1001},{"updated_on":response_date})
else:
print("Data Up to Date........")