|
from flask import Flask, render_template, request, jsonify, Response |
|
import requests |
|
from bs4 import BeautifulSoup |
|
from flask import stream_with_context |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
CROP_TO_PESTS = { |
|
"Sorgum": ["FallArmyWorm"], |
|
"Maize": ["FallArmyWorm"], |
|
"Rice": ["Blast", "GallMidge", "YSB", "PlantHopper", "BlueBeetle", "BacterialLeafBlight"], |
|
"Cotton": ["Thrips", "Whitefly", "PinkBollworm", "Jassid", "BollRot", "AmericanBollworm"], |
|
"Soybean": ["Girdlebeetle", "H.armigera", "Semilooper", "Spodoptera", "StemFLy"], |
|
"Tur": ["Wilt", "Webbed_Leaves", "Pod_damage"], |
|
"Sugarcane": ["FallArmyGrub", "WhiteGrub"], |
|
"Gram": ["H.armigera", "Wilt"] |
|
} |
|
|
|
|
|
YEARS = ["2024-25", "2023-24", "2022-23", "2021-22"] |
|
|
|
|
|
CROP_MAPPING = { |
|
"Cotton": "1", |
|
"Gram": "4", |
|
"Maize": "7", |
|
"Rice": "3", |
|
"Sorghum": "6", |
|
"Soybean": "2", |
|
"Sugarcane": "8", |
|
"Tur": "5", |
|
"Sorgum": "6" |
|
} |
|
|
|
|
|
PEST_MAPPING = { |
|
"Cotton": { |
|
"FallArmyWorm": "71" |
|
}, |
|
"Gram": { |
|
"H.armigera": "72", |
|
"Wilt": "73" |
|
}, |
|
"Maize": { |
|
"FallArmyWorm": "74" |
|
}, |
|
"Rice": { |
|
"Blast": "75", |
|
"GallMidge": "76", |
|
"YSB": "77", |
|
"PlantHopper": "78", |
|
"BlueBeetle": "79", |
|
"BacterialLeafBlight": "80" |
|
}, |
|
"Soybean": { |
|
"Girdlebeetle": "81", |
|
"H.armigera": "82", |
|
"Semilooper": "83", |
|
"Spodoptera": "84", |
|
"StemFLy": "85" |
|
}, |
|
"Tur": { |
|
"Wilt": "86", |
|
"Webbed_Leaves": "87", |
|
"Pod_damage": "88" |
|
}, |
|
"Sugarcane": { |
|
"FallArmyGrub": "89", |
|
"WhiteGrub": "90" |
|
}, |
|
"Sorgum": { |
|
"FallArmyWorm": "91" |
|
} |
|
} |
|
|
|
|
|
PARAMS = { |
|
"Mint": "Min Temperature", |
|
"Maxt": "Max Temperature", |
|
"RH": "Relative Humidity", |
|
"RF": "Rainfall", |
|
"PR": "Pest Report" |
|
} |
|
|
|
@app.route('/') |
|
def index(): |
|
|
|
crop = request.args.get('crop', '') |
|
pest = request.args.get('pest', '') |
|
year = request.args.get('year', '') |
|
week = request.args.get('week', '') |
|
param = request.args.get('param', '') |
|
|
|
image_url = "" |
|
if crop and pest and year and week and param: |
|
|
|
base_url = f"http://www.icar-crida.res.in:8080/naip/gisimages/{crop}/{year}/{pest}_" |
|
external_image_url = f"{base_url}{param}{week}.jpg" |
|
|
|
image_url = f"/proxy-image?url={external_image_url}" |
|
|
|
return render_template('index.html', |
|
crops=list(CROP_TO_PESTS.keys()), |
|
crop_to_pests=CROP_TO_PESTS, |
|
years=YEARS, |
|
params=PARAMS, |
|
selected_crop=crop, |
|
selected_pest=pest, |
|
selected_year=year, |
|
selected_week=week, |
|
selected_param=param, |
|
image_url=image_url) |
|
|
|
@app.route('/fetch_weeks') |
|
def fetch_weeks(): |
|
crop = request.args.get('crop', '') |
|
pest = request.args.get('pest', '') |
|
year = request.args.get('year', '') |
|
|
|
ext_crop = CROP_MAPPING.get(crop, '') |
|
ext_pest = "" |
|
if crop in PEST_MAPPING and pest in PEST_MAPPING[crop]: |
|
ext_pest = PEST_MAPPING[crop][pest] |
|
|
|
payload = { |
|
"country": ext_crop, |
|
"city": ext_pest, |
|
"sowing": year |
|
} |
|
|
|
weeks = [] |
|
try: |
|
response = requests.get("http://www.icar-crida.res.in:8080/naip/gismaps.jsp", params=payload, timeout=10) |
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
week_options = soup.select('select[name="week"] option') |
|
weeks = [opt.get('value') for opt in week_options if opt.get('value') and "Select" not in opt.get('value')] |
|
if not weeks: |
|
weeks = [str(i) for i in range(1, 53)] |
|
except Exception as e: |
|
weeks = [str(i) for i in range(1, 53)] |
|
return jsonify({"weeks": weeks}) |
|
|
|
@app.route('/proxy-image') |
|
def proxy_image(): |
|
external_url = request.args.get('url') |
|
if not external_url: |
|
return "Missing URL", 400 |
|
|
|
try: |
|
|
|
resp = requests.get(external_url, timeout=10, stream=True) |
|
return Response( |
|
stream_with_context(resp.iter_content(chunk_size=1024)), |
|
mimetype=resp.headers.get('Content-Type', 'image/jpeg') |
|
) |
|
except Exception as e: |
|
return str(e), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |