forked from Rishabh-Kumar-Bothra/krishiplusplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflood-cyclone.py
104 lines (80 loc) · 2.61 KB
/
flood-cyclone.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
import requests
from bs4 import BeautifulSoup
import simplejson as json
# This is the url to which the query is made
url_hasura = "https://data.combatant32.hasura-app.io/v1/query"
# This is the json payload for the query
requestPayload = {
"type": "update",
"args": {
"table": "krishiplusplus",
"where": {
"state": {
"$eq": "Gujarat"
}
},
"$set": {
"flood": "No Flood Prediction",
"cyclone": "No Cyclone Preediction"
}
}
}
# Setting headers
headers = {
"Content-Type": "application/json",
"Authorization": "Authorization Token"
}
'''FLOOD'''
url = 'http://india-water.gov.in/ffs/current-flood-forecast/'
try:
r = requests.get(url,timeout=4)
except requests.exceptions.RequestException as error:
print('Error Encountered : requests library')
soup = BeautifulSoup(r.text, 'lxml')
sub_heading = soup.find_all('tr')
level_forecast = sub_heading[2].find_all('td')
inflow_forecast = sub_heading[5].find_all('td')
if ( len(level_forecast) > 1 ):
print( 'Flood (level) in state: ' + level_forecast[2].text )
requestPayload["args"]["where"]["state"]["$eq"] = level_forecast[2].text
requestPayload["args"]["$set"]["flood"] = 'Warning: Flood (level) Prediction'
else:
print('No level flood forecast')
if( len(inflow_forecast) > 1 ):
print( 'Flood (inflow) in state: ' + inflow_forecast[2].text )
requestPayload["args"]["where"]["state"]["$eq"] = inflow_forecast[2].text
requestPayload["args"]["$set"]["flood"] = 'Warning: Flood (inflow) Prediction'
else:
print('No inflow flood forecast')
print('\nsending flood update request')
resp = requests.request("POST", url_hasura, data=json.dumps(requestPayload), headers=headers)
print(resp.content)
print('\nENDFLOOD\n')
''' CYCLONE'''
newurl = 'http://www.rsmcnewdelhi.imd.gov.in/index.php?lang=en'
try:
r = requests.get(newurl,timeout=4)
except requests.exceptions.RequestException as error:
print('Error Encountered : requests library')
soup = BeautifulSoup(r.text, 'lxml')
sub_heading = soup.find_all('span')
ans = sub_heading[0].text
if ans.find('no') != -1:
print('No Cyclone Forecast')
else:
CylonerequestPayload = {
"type": "update",
"args": {
"table": "krishiplusplus",
"where": {},
"$set": {
"cyclone": "No cyclone prediction"
}
}
}
print('Warning Cyclone : ' + ans)
requestPayload["args"]["$set"]["cyclone"] = ans
print('\nsending cyclone update request')
resp = requests.request("POST", url_hasura, data=json.dumps(CylonerequestPayload), headers=headers)
print(resp.content)
print('\nENDCYCLONE\n')