KKMS-KSSW-HF / src /weather.py
Chintan Donda
Moving kkms_kssw.py to src/
04e306a
raw
history blame
No virus
4.99 kB
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