def run(self): while True: text = input('Выберите действие:\n' '1. Добавить прогноз в базу данных\n' '2. Получить прогноз из базы даных\n' '3. Вывести открытку с прогнозом\n' '4. Вывести прогнозы на консоль\n' '5. Очистить базу данных\n' '6. Выход') if text == '1': self.write_to_base() elif text == '2': self.read_from_base() elif str(text) == '3': self.print_card() elif str(text) == '4': self.print_forecasts() elif str(text) == '5': Weather.delete().execute() elif str(text) == '6': break else: print( 'Введен неправильный номер. Требуется ввод числа от 1 до 6.' )
def index(request): if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): url=weather_url.format(request.POST['ciudad']) ciudad=request.POST['ciudad'] toDel = Weather.objects.filter(zipcode=ciudad) toDel.delete() response = requests.get(url) json_response = json.loads(response.text) w= Weather(temperature=k_to_c(json_response["main"]["temp"]),zipcode=ciudad, description=json_response["weather"][0]["description"], sunrise= datetime.utcfromtimestamp(json_response["sys"]["sunrise"]), sunset= datetime.utcfromtimestamp(json_response["sys"]["sunset"]),wind= json_response["wind"]["speed"]) w.save() #html="<html><body>%s y %s</body></html>"% (w.temperature, w.zipcode) context_dict = {} context_dict['temperature'] = k_to_c(json_response["main"]["temp"]) context_dict['zipcode'] = ciudad context_dict['description'] = json_response["weather"][0]["description"] context_dict['sunrise']= datetime.utcfromtimestamp(json_response["sys"]["sunrise"]) context_dict['sunset']= datetime.utcfromtimestamp(json_response["sys"]["sunset"]) context_dict['wind']= json_response["wind"]["speed"] return render(request, 'weather/weather.html', context_dict) else: w= Weather.objects.all() context_dict = {'city': w,'form':SearchForm()} return render(request, 'weather/index.html', context_dict)
def run(self): curr_time = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) logger.info("Beginning simulation, press Ctrl+C to exit at any time") logger.info("loading kafka connect jdbc source connector") configure_connector() logger.info("beginning cta train simulation") weather = Weather(curr_time.month) try: while True: logger.debug("simulation running: %s", curr_time.isoformat()) # Send weather on the top of the hour if curr_time.minute == 0: weather.run(curr_time.month) _ = [ line.run(curr_time, self.time_step) for line in self.train_lines ] curr_time = curr_time + self.time_step time.sleep(self.sleep_seconds) except KeyboardInterrupt as e: logger.info("Shutting down") _ = [line.close() for line in self.train_lines]
def weather_run(): count=0 for location in Location.objects.all(): weather_base = 'http://api.wunderground.com/api/e1fde55153190b2a/geolookup/conditions/q/' url = weather_base + urllib2.quote(location.state_name) + '/' + urllib2.quote(location.city_name) + '.json' count += 1 print count f = urllib2.urlopen(url) json_string = f.read() parsed_json = json.loads(json_string) if 'current_observation' in parsed_json: wind = parsed_json['current_observation']['precip_today_in'] feels_like = parsed_json['current_observation']['feelslike_f'] weather = parsed_json['current_observation']['weather'] try: float(parsed_json['current_observation']['precip_today_in']) precipitation = float(parsed_json['current_observation']['precip_today_in']) except ValueError: print 'Not string value' temperature = parsed_json['current_observation']['temp_f'] try: int(parsed_json['current_observation']['observation_epoch']) datetime = time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(int(parsed_json['current_observation']['observation_epoch']))) except ValueError: print 'Not Integer value' print wind, precipitation, temperature, weather, datetime weather = Weather(wind=wind, precipitation=precipitation, temperature=temperature, weather=weather, feels_like=feels_like, datetime=datetime) weather.location_id = location.id weather.save()
def create_weather(data): weather = Weather( city=data["city"], pm2_5=data["pm2_5"], temperature=data["temperature"], condition=unicode(data["condition"]), code=data["code"] ) weather.save()
def replace_stored_data(data): # Delete existing data query = Weather.delete().where((Weather.lattitude == data['lat']) & (Weather.longitude == data['long'])) query.execute() # Save new data Weather.create(lattitude=data['lat'], longitude=data['long'], temp=data['temp'])
def load_weather(session, year): swg = StadiumWeatherGetter() for stadium in swg.getStadiums(): yearly_weather = swg.getWeatherForYearAtStadium(stadium, year) for daily_weather in yearly_weather: for hourly_weather in daily_weather: new_weather_object = Weather() new_weather_object.loadWeatherContainer(hourly_weather) try: session.add(new_weather_object) session.commit() except IntegrityError: session.rollback()
def _saving_data(self): try: Weather.create( date=self.weather["дата"], average_temperature=self.weather["средняя температура"], low_temperature=self.weather["минимальная температура"], high_temperature=self.weather["максимальная температура"], wind=self.weather["ветер"], humidity=self.weather["влажность"], weather_description=self.weather["погода"]) except peewee.IntegrityError: print(f"{self.weather['дата']} уже есть в БД") log.debug(f"{self.weather['дата']} уже есть в БД") except Exception as ex: log.info(f"Ошибка: {ex}")
def raw_forecast_to_domain_forecast(raw_forecast) -> Forecast: TIME_CURRENT = 'currently' raw_weather = raw_forecast[TIME_CURRENT]['icon'] weather = Weather.from_value(raw_weather) return Forecast(weather)
def run_server(): """Runs the Tornado Server and begins Kafka consumption""" if topic_check.topic_exists(TURNSTILE_SUMMARY) is False: logger.fatal( "Ensure that the KSQL Command has run successfully before running the web server!" ) exit(1) if topic_check.topic_exists(STATIONS_TABLE) is False: logger.fatal( "Ensure that Faust Streaming is running successfully before running the web server!" ) exit(1) weather_model = Weather() lines = Lines() application = tornado.web.Application( [(r"/", MainHandler, {"weather": weather_model, "lines": lines})] ) application.listen(8888) # Build kafka consumers consumers = [ KafkaConsumer( WEATHER, weather_model.process_message, offset_earliest=True, ), KafkaConsumer( STATIONS_TABLE, lines.process_message, offset_earliest=True, is_avro=False, ), KafkaConsumer( "^org.chicago.cta.station.arrivals.", lines.process_message, offset_earliest=True, ), KafkaConsumer( TURNSTILE_SUMMARY, lines.process_message, offset_earliest=True, is_avro=False, ), ] try: logger.info( "Open a web browser to http://localhost:8888 to see the Transit Status Page" ) for consumer in consumers: tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt as e: logger.info("shutting down server") tornado.ioloop.IOLoop.current().stop() for consumer in consumers: consumer.close()
def wtite_month_in_bd(self, number_month, weather_on_month): """Записывает в базу данных погоду месяца number_month""" if len(number_month) == 1: number_month = "0" + number_month days = weather_on_month.keys() for day in days: date = day + "-" + number_month temperature_day = weather_on_month[day]['temperature_day'] temperature_night = weather_on_month[day]['temperature_night'] day_description = weather_on_month[day]['day_description'] day_info = Weather.get(date=date) if day_info: day_info.delete() Weather(date=date, temperature_day=temperature_day, temperature_night=temperature_night, day_description=day_description)
def write_month(start_day, end_day, number_month): """Пишет на консоль погоду с баззы данных месяца number_month от start_day до end_day""" for day in range(start_day, end_day + 1, 1): if len(str(number_month)) == 1: number_month = "0" + str(number_month) date = str(day) + "-" + str(number_month) weater_one_day = Weather.get(date=date) write_to_console_1_day(weater_one_day, date)
def post(self): data = request.get_json() # city, datetime, cluster, is_rain new_weather = Weather(city=data['city'], datetime=data['datetime'], cluster=data['cluster'], is_rain=int(data['is_rain'])) db.session.add(new_weather) db.session.commit() return serializer([new_weather])
def weather_run(): count = 0 for location in Location.objects.all(): weather_base = 'http://api.wunderground.com/api/e1fde55153190b2a/geolookup/conditions/q/' url = weather_base + urllib2.quote( location.state_name) + '/' + urllib2.quote( location.city_name) + '.json' count += 1 print count f = urllib2.urlopen(url) json_string = f.read() parsed_json = json.loads(json_string) if 'current_observation' in parsed_json: wind = parsed_json['current_observation']['precip_today_in'] feels_like = parsed_json['current_observation']['feelslike_f'] weather = parsed_json['current_observation']['weather'] try: float( parsed_json['current_observation']['precip_today_in']) precipitation = float( parsed_json['current_observation']['precip_today_in']) except ValueError: print 'Not string value' temperature = parsed_json['current_observation']['temp_f'] try: int(parsed_json['current_observation'] ['observation_epoch']) datetime = time.strftime( '%Y-%m-%d %H:%M:%S', time.gmtime( int(parsed_json['current_observation'] ['observation_epoch']))) except ValueError: print 'Not Integer value' print wind, precipitation, temperature, weather, datetime weather = Weather(wind=wind, precipitation=precipitation, temperature=temperature, weather=weather, feels_like=feels_like, datetime=datetime) weather.location_id = location.id weather.save()
def get_weather(place): place = place.replace(" ", "-") url = "https://www.weather-forecast.com/locations/" + place + "/forecasts/latest" r = requests.get(url) soup = bs(r.content, "lxml") weather = soup.findAll("span", {"class": "phrase"})[0].text w = Weather(city=place, response=weather) db_session.add(w) db_session.commit() return weather
def base_reading(self, first_day, last_day): base_text = '' for weather in Weather.select().where((Weather.date >= first_day) & (Weather.date <= last_day)): base_text = ( f' Дата: {weather.date.strftime("%d.%m.%Y")}.\n День: Температура: {weather.temp_d}.' f' Облачность: {weather.cloud_d}.Ветер: {weather.wind_d}. Давление: {weather.pres_d} мм рт.ст.\n ' f' Ночь: Температура: {weather.temp_n}. Облачность: {weather.cloud_n}. Ветер: {weather.wind_n}.' f' Давление: {weather.pres_n} мм рт.ст.\n') print(base_text) return base_text
def announce_weather(audio_path=audio_commands, cb=audio_transcribe, day=None): talk = cb weather_object = Weather() weather = weather_object.weather def convert_temperature(temp_f=None, temp_c=None): if temp_c is None: temp_c = (5.0 / 9.0) * (temp_f - 32) return temp_c if temp_f is None: temp_f = (9.0 / 5.0) * temp_c + 32 return temp_f """Remember to update the cron job to refresh the weather every 3 days so that these function will remain syntactically correct""" def actual_weather(i): whole_day = weather["forecasts"][i] day_forecast = weather["forecasts"][i]["day"] night_forecast = weather["forecasts"][i]["night"] max_temp_c = convert_temperature(temp_f=whole_day["max_temp"]) min_temp_c = convert_temperature(temp_f=whole_day["min_temp"]) day_shortcast = day_forecast["shortcast"] night_shortcast = night_forecast["shortcast"] day_temp = convert_temperature(temp_f=day_forecast["temp"]) night_temp = convert_temperature(temp_f=night_forecast["temp"]) day_relative_humidity = day_forecast["rh"] night_relative_humidity = day_forecast["rh"] # day_rain = night_forecast["precip_type"] # night_rain = night_forecast["precip_type"] # narrative = whole_day["narrative"] start_forecast = "This is the weather report for Lagos. Today's temperature range is from {0:0.1f} degree centigrade" \ ", at the lowest, and {1:0.1f} degree centigrade, at maximum. Average daily temperature is " \ "{6:0.1f} degree centigrade and the average nightly temperature is {7:0.1f} degree centigrade." \ " During the day the relative humidity is {2:0.1f} percent and at night " \ "the relative humidity is {3:0.1f} percent. It is going to be {4} during the day " \ "and {5}, at night." inter_forecast = start_forecast.format(min_temp_c, max_temp_c, day_relative_humidity, night_relative_humidity, day_shortcast, night_shortcast, day_temp, night_temp) return inter_forecast if day == "today" or day is None: talk(actual_weather(0)) if day is not None and day.lower() == "tomorrow": talk(actual_weather(1))
def getLatestWeather(): # SELECT * FROM weather ORDER BY timestamp DESC; conn = psycopg2.connect(connectionString) cur = conn.cursor() query = """ SELECT * FROM weather ORDER BY timestamp DESC; """ cur.execute(query) data = cur.fetchone() w = Weather(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8]) return w
def __init__(self, date, data_files_list, path, info_type): weather_years = [] # In this case, only year 1996 will be in date variable if info_type == '-e': files = [ file_name for file_name in data_files_list if date in file_name ] # In this case, year and month 1996/12 will be in date variable elif info_type == '-a' or info_type == '-c' or info_type == '-d': files = [ file_name for file_name in data_files_list if calendar.month_name[int(date.split('/')[1])][0:3] in file_name and date.split('/')[0] in file_name ] for file_name in files: with open(path + "/" + file_name, 'r') as fp: line = fp.readline() cnt = 1 while line: if cnt >= 3 and '<!--' not in line: weatherDetail = line.split(',') weather = Weather() weather.max_temprature = 0 if weatherDetail[ 1] == '' else int(weatherDetail[1]) weather.lowest_temprature = 0 if weatherDetail[ 3] == '' else int(weatherDetail[3]) weather.most_humid = 0 if weatherDetail[ 7] == '' else int(weatherDetail[7]) weather.date = weatherDetail[0] weather_years.append(weather) line = fp.readline() cnt += 1 self.data = weather_years
def parse_files(date, data_files_list, root_path, info_type): weather_years = [] if info_type == '-e': files = [ file_name for file_name in data_files_list if date in file_name ] elif info_type == '-a': files = [ file_name for file_name in data_files_list if calendar.month_name[int(date.split('/')[1])][0:3] in file_name ] for file_path in files: with open(root_path + "/" + file_path, 'r') as fp: line = fp.readline() cnt = 1 while line: if cnt >= 3 and '<!--' not in line: weatherDetail = line.split(',') weather = Weather() weather.max_temprature = 0 if weatherDetail[ 1] == '' else int(weatherDetail[1]) weather.lowest_temprature = 0 if weatherDetail[ 3] == '' else int(weatherDetail[3]) weather.most_humid = 0 if weatherDetail[7] == '' else int( weatherDetail[7]) weather.date = weatherDetail[0] weather_years.append(weather) line = fp.readline() cnt += 1 return weather_years
def zipcode_post(request): #return HttpResponse("Hello Weather") if (request.method == "POST"): form = SearchForm(request.POST) if form.is_valid(): url=weather_url.format(request.POST['ciudad']) response = requests.get(url) json_response = json.loads(response.text) w= Weather(temperature=k_to_c(json_response["main"]["temp"]),zipcode=zipcode, description=json_response["weather"][0]["description"], sunrise= json_response["sys"]["sunrise"], sunset= json_response["sys"]["sunset"],wind= json_response["wind"]["speed"]) w.save() #html="<html><body>%s y %s</body></html>"% (w.temperature, w.zipcode) context_dict = {} context_dict['temperature'] = k_to_c(json_response["main"]["temp"]) context_dict['zipcode'] = zipcode context_dict['description'] = json_response["weather"][0]["description"] context_dict['sunrise']= datetime.utcfromtimestamp(json_response["sys"]["sunrise"]) context_dict['sunset']= datetime.utcfromtimestamp(json_response["sys"]["sunset"]) context_dict['wind']= json_response["wind"]["speed"] return render(request, 'weather/weather.html', context_dict)
def get(self): weather = Weather(130010) telop = weather.get_telop() if telop.find(u'雨') != -1: msg_tmpl = u'おはようございます。今日は{0}ですね。傘はお持ちですか? {1}' msg = msg_tmpl.format(telop, weather.get_link()) elif telop.find(u'雪') != -1: msg_tmpl = u'おはようございます。今日は{0}ですよ! 暖かくしていってくださいね。 {1}' msg = msg_tmpl.format(telop, weather.get_link()) else: r = random.randint(0, 2) if r == 0: msg = u'おはようございます。今日も1日がんばりましょう。' elif r == 1: msg = u'おはようございます。今日は何しようかな?' elif r == 2: msg = u'おはようございまーす♪' account = Account() account.tweet(msg)
def observation_update(site_key): site = Site.get_by_key_name(site_key) if site is None: return Response(status=404) url = "http://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Observation/%s?format=application/json" % site_key obs = {} def get_db_observation(date): key_name = make_key_name(site, date.date()) if key_name in obs: return obs[key_name] o = ObservationDay.get_by_key_name(key_name) if o is None: o = ObservationDay(key_name=key_name) o.site = site o.observation_date = date.date() o.observations = Observations() obs[key_name] = o return o result = urlfetch.fetch(url) if result.status_code == 200: observations = parse_observation(result.content) issue_date = parse_date(observations['@issueDate']) site.last_obs_issue_datetime = issue_date site.last_obs_update_datetime = datetime.now() for date, data in timesteps(observations): o = get_db_observation(date) o.lastdata_datetime = issue_date w = Weather({}) for k, v in data.items(): prop_name = snake_case(k) if hasattr(w, prop_name): if v == "missing": v = None elif prop_name == 'temperature': v = float(v) setattr(w, prop_name, v) o.observations.add(date, w) for o in obs.values(): o.save() site.save() return Response(status=204)
def writing(self, base_data): weather, created = Weather.get_or_create(date=base_data[0].date(), defaults={ 'temp_d': base_data[1], 'cloud_d': base_data[2], 'wind_d': base_data[3], 'pres_d': base_data[4], 'temp_n': base_data[5], 'cloud_n': base_data[6], 'wind_n': base_data[7], 'pres_n': base_data[8] }) if not created: weather = Weather.update( temp_d=base_data[1], cloud_d=base_data[2], wind_d=base_data[3], pres_d=base_data[4], temp_n=base_data[5], cloud_n=base_data[6], wind_n=base_data[7], pres_n=base_data[8]).where(Weather.id == weather.id) weather.execute()
def print_weather_forecast(self): printed = False for day in Weather.select(): year, month, c_day = day.date.split("-") date = datetime.date(int(year), int(month), int(c_day)) if self.dates_range[0] <= date <= self.dates_range[1]: printed = True print( f"_____________________\nDate: {date.strftime('%d.%m.%Y')}\nMin temp: {day.min_temperature}\nMax " f"temp: {day.max_temperature}\nWeather type: {day.weather_type}\n_____________________" ) if not printed: print( "Dates are out of range or you haven't updated the database!")
def create_post_card(self): printed = False for day in Weather.select(): year, month, c_day = day.date.split("-") date = datetime.date(int(year), int(month), int(c_day)) date_to_print = f"{c_day}-{month}-{year}" if self.dates_range[0] <= date <= self.dates_range[1]: printed = True post_card.make_postcard(date_to_print, day.min_temperature, day.max_temperature, day.weather_type.lower()) if not printed: print( "Dates are out of range or you haven't updated the database!\n" )
def _data_collection(self): try: data = Weather.get(Weather.date == self.request) return { "дата": self.request, "средняя температура": data.average_temperature, "максимальная температура": data.high_temperature, "минимальная температура": data.low_temperature, "ветер": data.wind, "влажность": data.humidity, "погода": data.weather_description } except Exception as ex: log.info(f"Ошибка: {ex}") print(f"{self.request} нет в БД") return 0
def run_server(): """Runs the Tornado Server and begins Kafka consumption""" if not topic_check.topic_exists(TURNSTILE_ENTRIES_TABLE): logger.error( "Ensure that the KSQL Command has run successfully before running the web server!" ) raise Exception("Ensure that the KSQL Command has run") if not topic_check.topic_exists(STATION_MASTER_DATA): logger.error( "Ensure that Faust Streaming is running successfully before running the web server!" ) raise Exception("Ensure that Faust Streaming is running successfully") weather_model = Weather() lines = Lines() application = tornado.web.Application( [(r"/", MainHandler, {"weather": weather_model, "lines": lines})] ) application.listen(8888) consumers = [ KafkaConsumer(WEATHER_STATUS, weather_model.process_message, offset_earliest=True), KafkaConsumer(STATION_MASTER_DATA, lines.process_message, offset_earliest=True, is_avro=False), KafkaConsumer(TRAIN_ARRIVAL, lines.process_message, offset_earliest=True), KafkaConsumer( TURNSTILE_ENTRIES_TABLE, lines.process_message, offset_earliest=True, is_avro=False, ), ] logger.info("Open a web browser to http://localhost:8888 to see the Transit Status Page") try: for consumer in consumers: tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt: logger.info("shutting down server") tornado.ioloop.IOLoop.current().stop() for consumer in consumers: consumer.close()
def test_get_pref(self): weather = Weather(130010) self.assertMultiLineEqual( weather.get_pref(), u'東京都')
def replace_stored_data(data): # Delete existing data query = Weather.delete().where((Weather.lattitude == data['lat']) & (Weather.longitude==data['long'])) query.execute() # Save new data Weather.create(lattitude=data['lat'], longitude=data['long'], temp=data['temp'])
def test_get_telop(self): weather = Weather(130010) print weather.get_telop()
def run_server(): """Runs the Tornado Server and begins Kafka consumption""" if topic_check.topic_exists("TURNSTILE_SUMMARY") is False: logger.fatal( "Ensure that the KSQL Command has run successfully before running the web server!" ) exit(1) if topic_check.topic_pattern_match("faust.stations.transformed") is False: logger.fatal( "Ensure that Faust Streaming is running successfully before running the web server!" ) exit(1) weather_model = Weather() lines = Lines() application = tornado.web.Application([(r"/", MainHandler, { "weather": weather_model, "lines": lines })]) application.listen(WEB_SERVER_PORT) print("Building consumers....") # Kafka consumers for the application consumers = [ KafkaConsumer( "weather", weather_model.process_message, offset_earliest=True, ), KafkaConsumer( "faust.stations.transformed", lines.process_message, offset_earliest=True, is_avro=False, ), KafkaConsumer( "^station.arrivals.*", lines.process_message, offset_earliest=True, ), KafkaConsumer( "TURNSTILE_SUMMARY", lines.process_message, offset_earliest=True, is_avro=False, ), ] try: logger.info( f"Open a web browser to http://localhost:{WEB_SERVER_PORT} to see the Transit Status Page" ) for consumer in consumers: tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt as e: logger.info("shutting down server") tornado.ioloop.IOLoop.current().stop() for consumer in consumers: consumer.close()
def init_maps(): data = [x.json() for x in Weather.select()] # Python list comprehension, .json() method is defined in models if data: socketio.emit('data-multiple',data)
# # WEATHER # os.chdir(os.path.join(data_base, 'weather')) weather_ls = [] direc = os.getcwd() fnames = [os.path.join(direc, fn) for fn in listdir('.') if fn[0] != '.'] for fname in fnames: with open(fname, 'r') as f: dct = json.load(f) obs_day = [] for obs in dct['history']['observations']: a = Weather() dt = obs['date'] dt_str = ' '.join( [dt['year'], dt['mon'], dt['mday'], dt['hour'], dt['min']]) a.datetime = datetime.strptime(dt_str, "%Y %m %d %H %M") a.snow = bool(int(obs['snow'])) try: a.humidity = int(obs['hum']) except: a.humidity = 50 a.temperature = float(obs['tempm']) a.precipitation = max(float(obs['precipm']), 0.0) obs_day.append(a) for i in range(len(obs_day)):
location_dict = dict(location_list) # 컬럼 리스트 생성 columns_name = temperature_sett.keys() # 인설트 "nested for loop" for count, i in enumerate(columns_name): if count != 0: for ccount, record in enumerate(temperature_sett[i]): # 날짜 벨류 생성 Y, M, D = i.split("-") date_value = date(int(Y), int(M), int(D)) #온도, 강수량 분할 P, L, H = record.split("/") low_temperature = int(L.strip()) high_temperature = int(H.strip()) pre_probability = P insert_value = Weather( selected_date_weather=date_value, precipitation_probability=pre_probability, lowest_temperature=low_temperature, highest_temperature=high_temperature, location_code=location_dict[temperature_sett['지역'][ccount]]) db.session.add(insert_value) # 마지막 commit db.session.commit()
# # WEATHER # os.chdir(os.path.join(data_base, 'weather')) weather_ls = [] direc = os.getcwd() fnames = [os.path.join(direc, fn) for fn in listdir('.') if fn[0] != '.'] for fname in fnames: with open(fname, 'r') as f: dct = json.load(f) obs_day = [] for obs in dct['history']['observations']: a = Weather() dt = obs['date'] dt_str = ' '.join([dt['year'], dt['mon'], dt['mday'], dt['hour'], dt['min']]) a.datetime = datetime.strptime(dt_str, "%Y %m %d %H %M") a.snow = bool(int(obs['snow'])) try: a.humidity = int(obs['hum']) except: a.humidity = 50 a.temperature = float(obs['tempm']) a.precipitation = max(float(obs['precipm']), 0.0) obs_day.append(a) for i in range(len(obs_day)):
def get_weather(city): appid = "" private_key = "" areas = Area.objects.all() for area in areas: areaid = area.areaid weather_type = 'forecast_f' date = datetime.datetime.now().strftime("%Y%m%d%H%M") pub_url = "http://open.weather.com.cn/data/?areaid="+areaid+"&type="+weather_type+"&date="+date public_key = pub_url+"&appid="+appid pkey = base64.b64encode(hmac.new(private_key, public_key, digestmod=hashlib.sha1).digest()) key = urllib.quote(pkey) url = pub_url+"areaid=101010100&type=index_v&date=201507192100&appid="+appid[:6]+"&key="+key data = json.loads(urllib2.urlopen(url).read()) city = Weather() city.area_id = areaid city.pub_time = data['f']['f0'] city.day_weather_1 = get_weather_weather(data['f']['f1'][0]['fa']) city.night_weather_1 = get_weather_weather(data['f']['f1'][0]['fb']) city.day_temp_1 = data['f']['f1'][0]['fc'] city.night_temp_1 = data['f']['f1'][0]['fd'] city.day_wd_1 = get_weather_wd(data['f']['f1'][0]['fe']) city.night_wd_1 = get_weather_wd(data['f']['f1'][0]['ff']) city.day_ws_1 = get_weather_wd(data['f']['f1'][0]['fg']) city.night_ws_1 = get_weather_wd(data['f']['f1'][0]['fh']) city.day_weather_2 = get_weather_weather(data['f']['f1'][1]['fa']) city.night_weather_2 = get_weather_weather(data['f']['f1'][1]['fb']) city.day_temp_2 = get_weather_temp(data['f']['f1'][1]['fc']) city.night_temp_2 = get_weather_temp(data['f']['f1'][1]['fd']) city.day_wd_2 = get_weather_wd(data['f']['f1'][1]['fe']) city.night_wd_2 = get_weather_wd(data['f']['f1'][1]['ff']) city.day_ws_2 = get_weather_wd(data['f']['f1'][1]['fg']) city.night_ws_2 = get_weather_wd(data['f']['f1'][1]['fh']) city.day_weather_3 = get_weather_weather(data['f']['f1'][2]['fa']) city.night_weather_3 = get_weather_weather(data['f']['f1'][2]['fb']) city.day_temp_3 = get_weather_temp(data['f']['f1'][2]['fc']) city.night_temp_3 = get_weather_temp(data['f']['f1'][2]['fd']) city.day_wd_3 = get_weather_wd(data['f']['f1'][2]['fe']) city.night_wd_3 = get_weather_wd(data['f']['f1'][2]['ff']) city.day_ws_3 = get_weather_wd(data['f']['f1'][2]['fg']) city.night_ws_3 = get_weather_wd(data['f']['f1'][2]['fh']) city.sun_rise = get_weather_sunrise(data['f']['f1'][0]['fi']) city.sun_set = get_weather_sunset(data['f']['f1'][0]['fi'])
def run_server(): """Runs the Tornado Server and begins Kafka consumption""" if topic_check.topic_exists(TURNSTILE_SUMMARY_TOPIC) is False: logger.fatal( "ensure that the KSQL Command has run successfully before running the web server!" ) exit(1) if topic_check.topic_exists(STATIONS_TOPIC_V1) is False: logger.fatal( "ensure that Faust Streaming is running successfully before running the web server!" ) exit(1) weather_model = Weather() lines = Lines() application = tornado.web.Application([(r"/", MainHandler, { "weather": weather_model, "lines": lines })]) application.listen(8888) # Build kafka consumers consumers = [ KafkaConsumer( id="weather.consumer", topic_name_pattern=WEATHER_TOPIC_V1, message_handler=weather_model.process_message, offset_earliest=True, ), KafkaConsumer( id="stations.table.consumer", topic_name_pattern=STATIONS_TOPIC_V1, message_handler=lines.process_message, offset_earliest=True, is_avro=False, ), KafkaConsumer(id="arrival.consumer", topic_name_pattern=f"^{ARRIVALS_TOPIC_PREFIX}", message_handler=lines.process_message, offset_earliest=True), KafkaConsumer( id="turnstile.summary.consumer", topic_name_pattern=TURNSTILE_SUMMARY_TOPIC, message_handler=lines.process_message, offset_earliest=True, is_avro=False, ) ] try: logger.info( "open a web browser to http://localhost:8888 to see the Transit Status Page" ) for consumer in consumers: tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt as e: logger.info("shutting down server") tornado.ioloop.IOLoop.current().stop() for consumer in consumers: consumer.close()
def test_get_url(self): weather = Weather(130010) self.assertMultiLineEqual( weather.get_url(), 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010')
def test_get_link(self): weather = Weather(130010) self.assertMultiLineEqual( weather.get_link(), 'http://weather.livedoor.com/area/forecast/130010')