def get_json_weather_specific_day(self, day_count): req = requests.get( "http://api.openweathermap.org/data/2.5/forecast/daily?lat=" + self.latitude + "&lon=" + self.longitude + "&cnt=4&mode=json&units=metric&appid=15d62eede0fc0659129e2956202bfc48" ) data = json.loads(req.text) forecast_data = {} count = 0 for forecast in data["list"]: if count == day_count: date_epoch = forecast["dt"] day = (datetime.fromtimestamp(date_epoch).strftime("%A") + "") date = (datetime.fromtimestamp(date_epoch).strftime( '%Y-%m-%d %H:%M:%S.%f') + "")[8:10] month = (datetime.fromtimestamp(date_epoch).strftime( '%Y-%m-%d %H:%M:%S.%f') + "")[5:7] temp = forecast["temp"] weather = forecast["weather"][0] wind_speed = forecast["speed"] deg_wind = forecast["deg"] trans_desc = HindiEnglish() desc_hindi = trans_desc.eng_to_hindi(weather["description"]) forecast_data = { "ty": "we1", "da": day, "dt": date, "m": month, "h": temp["max"], "l": temp["min"], "t": desc_hindi, "ic": weather["icon"], "ws": wind_speed, "wd": deg_wind } print forecast_data print ":::" + str(count) + ":::" break else: count += 1 return json.dumps(forecast_data)
def parse_json_trade_sol(self, solution, crop_name): answer_json = {} final_solution_value_ref = "" trans = HindiEnglish() for cur_sol in solution["resolution"]["solutions"]: if cur_sol["status"] == "FRONT": final_solution_value_ref = cur_sol["solution_ref"] for cur_value in solution["problem"]["options"]: if cur_value["key"] == final_solution_value_ref: answer_json.update({"crn":trans.eng_to_hindi(crop_name), "ty":"pcpr1" ,"cn":trans.eng_to_hindi(cur_value["name"]), "pr":cur_value["values"]["price"], "di":cur_value["values"]["dist"]}) return answer_json
def get_crop_info(self, crop_name): cur = conn.cursor() cur.execute("SELECT * FROM Cultivationdetails WHERE C1 LIKE '" + crop_name + "'") row = cur.fetchone() crop_info_array = {"ty": "crin", "data": {}} count = 1 translate = HindiEnglish() while row is not None: cr_crp_name = translate.eng_to_hindi(row[0]) cr_temp = translate.eng_to_hindi(row[1]) cr_soil = translate.eng_to_hindi(row[2]) cr_manure = translate.eng_to_hindi(row[3]) cr_pests = translate.eng_to_hindi(row[4]) cr_diseases = translate.eng_to_hindi(row[5]) cr_water = row[6] cr_drainage = row[7] cr_file_name = row[8] crop_info_array["data"] = { "na_c": cr_crp_name, "temp": cr_temp, "soil": cr_soil, "man": cr_manure, "pes": cr_pests, "dis": cr_diseases, "wat": cr_water, "dra": cr_drainage, "ic": cr_file_name } count += 1 row = cur.fetchone() return json.dumps(crop_info_array)
def start(self): # Translate from Hindi translate = HindiEnglish() eng_query = translate.hindi_to_eng(self.hindi_query) # Get Intent and entity conversation conversation = FarmerConversation() str_intent_entity_json = conversation.converse(eng_query) intent_entity_json = json.loads(str_intent_entity_json) intent = intent_entity_json["intent"] entity_type = intent_entity_json["entity_type"] entity_value = intent_entity_json["entity_value"] # Do something with these requests if intent == "Weather": weather = WeatherYahoo(self.latitude, self.longitude) if entity_type == "Weather_Days": if entity_value == "tomorrow": self.reply_str = weather.get_json_weather_specific_day(1) self.send_sms(self.reply_str) if entity_value == "today": if self.is_persist == "true": self.reply_str = (weather.get_json_weather_one_day() + "").replace("we1", "pwe1") else: self.reply_str = weather.get_json_weather_one_day() self.send_sms(self.reply_str) if entity_value == "day after": self.reply_str = weather.get_json_weather_specific_day(2) self.send_sms(self.reply_str) if entity_value == "next three days": self.reply_str = weather.get_json_weather_three_days() self.send_sms(self.reply_str) if intent == "SellPlacePrice": crop_price = Crop_Price() if entity_type == "crop": self.reply_str = crop_price.get_prices_in_cities( entity_value, self.latitude, self.longitude) self.send_sms(self.reply_str) if intent == "TipOfTheDay": tod = Tip_Of_Day() self.reply_str = tod.get_tip_of_day() self.send_sms(self.reply_str) if intent == "NewWord": wod = Word_Of_The_Day() self.reply_str = wod.get_word_of_day() self.send_sms(self.reply_str) if intent == "CropInfo": if entity_type == "crop": crop_info = Crop_Info() self.reply_str = crop_info.get_crop_info(entity_value) self.send_sms(self.reply_str) return self.reply_str
def eng_to_hindi(): translation = HindiEnglish() return translation.eng_to_hindi(request.form["eng_text"])
def hindi_to_eng(): translation = HindiEnglish() return translation.hindi_to_eng(request.form["hindi_text"])