File size: 4,387 Bytes
a447435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import requests
from bs4 import BeautifulSoup as bs


STATE_CODES = {
	'Andaman-Nicobar': '01',
	'Andhra-Pradesh': '02',
	'Arunachal-Pradesh': '03',
	'Assam': '04',
	'Bihar': '05',
	'Chandigarh': '06',
	'Chhattisgarh': '07',
	'Dadra-and-Nagar-Haveli': '08',
	'Daman-and-Diu': '09',
	'Delhi': '10',
	'Goa': '11',
	'Gujarat': '12',
	'Haryana': '13',
	# 14
	'Himachal-Pradesh': '15',
	'Jammu-Kashmir': '16',
	'Jharkhand': '17',
	'Karnataka': '18',
	'Kerala': '19',
	'Lakshadweep': '20',
	'Madhya-Pradesh': '21',
	'Maharashtra': '22',
	'Manipur': '23',
	'Meghalaya': '24',
	'Mizoram': '25',
	'Nagaland': '26',
	'Odisha': '27',
	'Pondicherry': '28',
	'Punjab': '29',
	'Rajasthan': '30',
	'Sikkim': '31',
	'Tamilnadu': '32',
	'Telangana': '33',
	'Tripura': '34',
	'Uttar-Pradesh': '35',
	'Uttarakhand': '36',
	'West-Bengal': '37',
}

# List of states that are given as the input selection to https://nwp.imd.gov.in/blf/blf_temp/ to get the weather forecast
STATES = {
	'Andaman-Nicobar': {},
	
	'Andhra-Pradesh': {},
	
	'Arunachal-Pradesh': {},
	
	'Assam': {},
	
	'Bihar': {},
	
	'Chandigarh': {},
	
	'Chhattisgarh': {},
	
	'Dadra-and-Nagar-Haveli': {},
	
	'Daman-and-Diu': {},
	
	'Delhi': {
		'CENTRAL-DELHI': ['CENTRAL-DELHI'],
		'EAST-DELHI': ['EAST-DELHI'],
		'NEW-DELHI': ['NEW-DELHI'],
		'NORTH-DELHI': ['NORTH-DELHI'],
		'NORTH-EAST-DELHI': ['NORTH-EAST-DELHI'],
		'NORTH-WEST-DELHI': ['NORTH-WEST-DELHI'],
		'SHAHDARA': ['SHAHDARA'],
		'SOUTH-DELHI': ['SOUTH-DELHI'],
		'SOUTH-EAST-DELHI': ['SOUTH-EAST-DELHI'],
		'SOUTH-WEST-DELHI': ['SOUTH-WEST-DELHI'],
		'WEST-DELHI': ['WEST-DELHI'],
	},
	
	'Goa': {},
	
	'Gujarat': {
		'AHMADABAD': ['AHMEDABAD-CITY', 'BAVLA', 'DASKROI', 'DETROJ-RAMPURA', 'DHANDHUKA', 'DHOLERA', 'DHOLKA', 'MANDAL', 'SANAND', 'VIRAMGAM'],
		'AMRELI': ['AMRELI', 'BABRA', 'BAGASARA', 'DHARI', 'JAFRABAD', 'KHAMBHA', 'KUNKAVAV-VADIA', 'LATHI', 'LILIA', 'RAJULA', 'SAVERKUNDLA'],
		'ANAND': [],
		'ARVALLI': [],
		'BANASKANTHA': [],
		'BHARUCH': [],
		'BHAVNAGAR': [],
		'BOTAD': [],
		'CHHOTAUDEPUR': [],
		'DANG': [],
		'DEVBHUMI-DWARKA': [],
		'DOHAD': [],
		'GANDHINAGAR': [],
		'GIR-SOMNATH': [],
		'JAMNAGAR': [],
		'JUNAGADH': [],
		'KACHCHH': [],
		'KHEDA': [],
		'MAHESANA': [],
		'MAHISAGAR': [],
		'MORBI': [],
		'NARMADA': [],
		'NAVSARI': [],
		'PANCH-MAHALS': [],
		'PATAN': [],
		'PORBANDAR': [],
		'RAJKOT': [],
		'SABAR-KANTHA': [],
		'SURAT': ['BARDOLI', 'CHORASI', 'KAMREJ', 'MAHUVA', 'MANDVI', 'MANGROL', 'OLPAD', 'PALSANA', 'SURAT-CITY', 'UMARPADA'],
		'SURENDRANAGAR': [],
		'TAPI': [],
		'VADODARA': [],
		'VALSAD': [],
	},
	
	'Haryana': {},
	
	'Himachal-Pradesh': {},
	
	'Jammu-Kashmir': {},
	
	'Jharkhand': {},
	
	'Karnataka': {},
	
	'Kerala': {},
	
	'Lakshadweep': {},
	
	'Madhya-Pradesh': {},
	
	'Maharashtra': {},
	
	'Manipur': {},
	
	'Meghalaya': {},
	
	'Mizoram': {},
	
	'Nagaland': {},
	
	'Odisha': {},
	
	'Pondicherry': {},
	
	'Punjab': {},
	
	'Rajasthan': {},
	
	'Sikkim': {},
	
	'Tamilnadu': {},
	
	'Telangana': {},
	
	'Tripura': {},
	
	'Uttar-Pradesh': {},
	
	'Uttarakhand': {},
	
	'West-Bengal': {},
}



class WEATHER:
	def __init__(self):
		self.base_url = 'https://nwp.imd.gov.in/blf/blf_temp'
		

	# Weather forecast from Govt. website
	def get_weather_forecast(self, state, district, is_block_level=False):
		self.district_url = f"{self.base_url}/block.php?dis={STATE_CODES.get(state, '') + district}"
		self.block_url = f'{self.base_url}/table2.php'

		response = requests.get(self.district_url if not is_block_level else self.block_url)
		soup = bs(response.text, 'html.parser')
		scripts = soup.findAll('font')[0]
		return scripts.text


	# Weather using Google weather API
	def get_weather(self, city):
		city = city + " weather"
		city = city.replace(" ", "+")

		headers = {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
		}
		response = requests.get(
			f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8', headers=headers)

		soup = bs(response.text, 'html.parser')
		location = soup.select('#wob_loc')[0].getText().strip()
		time = soup.select('#wob_dts')[0].getText().strip()
		info = soup.select('#wob_dc')[0].getText().strip()
		temperature = soup.select('#wob_tm')[0].getText().strip()
		temperature = temperature + "°C"

		return time, info, temperature