def process_metie(data): """ Process met.ie forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" desc = common.get_string('metierev', '') for jobj in json.loads(data): myday = day.Day() tmp_day = jobj["date"] + "T" + jobj["time"] myday.timestamp = time.mktime(time.strptime(tmp_day, '%Y-%m-%dT%H:%M')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.max = str(jobj["temperature"]) myday.icon = jobj["weatherNumber"] myday.text = jobj["weatherDescription"] if use_icons != "1": myday.icon = "wi wi-met-ie-" + myday.icon else: myday.icon = "y" + myday.icon + ".png" if metric == "1": myday.max += "°C" else: myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_metservice(data): """ Process metservice forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) loop = jobj['days'] # ftime = loop[0]["issuedAtISO"] desc = jobj["locationECWasp"] + ", New Zealand" for jtmp in loop: myday = day.Day() jtmp['dateISO'] = jtmp['dateISO'][:-6] myday.timestamp = time.mktime(time.strptime(jtmp['dateISO'], "%Y-%m-%dT%H:%M:%S")) myday.day = jtmp["dow"] myday.text = jtmp["forecast"] myday.max = jtmp["max"] myday.min = jtmp["min"] if "partDayData" in jtmp: myday.icon = jtmp["partDayData"]["afternoon"]["forecastWord"] else: myday.icon = jtmp["forecastWord"] myday.icon = myday.icon.lower().replace(" ", "-").strip() if use_icons != "1": if myday.icon != "frost": myday.icon = "wi wi-metservice-" + myday.icon else: myday.icon = "flaticon-thermometer" else: myday.icon = myday.icon.replace("-", "_") myday.icon = "ms_" + myday.icon + ".png" if metric: myday.max += "°C" myday.min += "°C" else: myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_mf(data): """ Process meteofrance.com Forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" desc = data.split("<h1>", 1)[1].split("</h1>", 1)[0].strip() data = data.split("<!-- LISTE JOURS -->", 1)[1].split("<!-- LISTE JOURS/ -->", 1)[0].strip() bits = data.split('title="') del bits[0] print("") for bit in bits: myday = day.Day() myday.day = bit.split("<a>", 1)[1].split("</a>", 1)[0].strip() myday.min = bit.split('class="min-temp">', 1)[1].split('°C Minimale', 1)[0].strip() myday.max = bit.split('class="max-temp">', 1)[1].split('°C Maximale', 1)[0].strip() myday.icon = bit.split('<dd class="pic40 ', 1)[1].split('">', 1)[0].strip() if metric == "1": myday.max = myday.max + "°C" myday.min = myday.min + "°C" else: myday.max = str(round(int(myday.max) * 9 / 5 + 32)) + "°F" myday.min = str(round(int(myday.max) * 9 / 5 + 32)) + "°F" icon = TABLE.get(myday.icon) if icon is None: # TODO: report/log missing CSS name myday.icon = None else: if use_icons != "1": if icon.replace("_", "-") != 'j-w1-8-n"': myday.icon = "wi wi-meteofrance-" + icon.replace('_', '-') else: myday.icon = "flaticon-thermometer" else: myday.icon = "mf_" + icon + ".png" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" print(days) return [True, days, desc]
def process_metoffice(data): """ Process MET Office forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" desc = data.split('<title>', 1)[1].split(' weather - Met Office</title>', 1)[0].strip() forecasts = data.split('<ul id="dayNav"', 2)[1].split('</ul>', 2)[0].split('<li') del forecasts[0] for line in forecasts: myday = day.Day() myday.day = line.split('data-tab-id="', 1)[1].split('"')[0].strip() myday.timestamp = time.mktime(time.strptime(myday.day, '%Y-%m-%d')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.icon = "https://beta.metoffice.gov.uk" + line.split('<img class="icon"')[1] myday.icon = myday.icon.split('src="')[1].split('">')[0].strip() myday.icon = os.path.basename(myday.icon).replace(".svg", ".png").strip() myday.min = line.split('<span class="tab-temp-low"', 1)[1].split('">')[1] myday.min = myday.min.split("</span>", 1)[0].strip() myday.max = line.split('<span class="tab-temp-high"', 1)[1].split('">')[1] myday.max = myday.max.split("</span>")[0].strip() myday.text = line.split('<div class="summary-text', 1)[1].split('">', 3)[2] myday.text = myday.text.split("</div>", 1)[0] myday.text = myday.text.replace('</span>', '').replace('<span>', '').strip() if metric == "1": myday.min += "C" myday.max += "C" else: myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if use_icons == "1": myday.icon = "met" + myday.icon else: myday.icon = "wi wi-metoffice-" + myday.icon[:-4] days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_yahoo(data): """ Process Yahoo forecast """ metric = common.get_string("metric", "1") days = "" bits = data.split('data-reactid="7">', 7) town, rest = bits[7].split('</h1>', 1) country, rest = rest.split('data-reactid="8">', 1)[1].split('</div>', 1) desc = town.strip() + ", " + country.strip() daynums = [196, 221, 241, 261, 281, 301, 321, 341, 361, 381] for startid in daynums: myday, rest = process_day(rest, startid, metric) ret = check_files(myday.icon) if ret[0] is True: myday.icon = ret[1] else: return ret days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_wcom(data): """ Process weather.com forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = jobj["id"] valid_date = jobj["vt1dailyForecast"]["validDate"] icons = jobj["vt1dailyForecast"]["day"]["icon"] phrase = jobj["vt1dailyForecast"]["day"]["phrase"] day_temp = jobj["vt1dailyForecast"]["day"]["temperature"] night_temp = jobj["vt1dailyForecast"]["night"]["temperature"] rng = range(len(valid_date)) for i in rng: myday = day.Day() myday.timestamp = time.mktime(time.strptime(valid_date[i], '%Y-%m-%dT%H:%M:%S%z')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.text = phrase[i] myday.icon = icons[i] myday.max = str(day_temp[i]) myday.min = str(night_temp[i]) if use_icons != "1": myday.icon = "wi wi-yahoo-" + myday.icon else: myday.icon = "yahoo" + str(myday.icon) + ".gif" if metric == "1": myday.max += "°C" myday.min += "°C" else: myday.max += "°F" myday.min += "°F" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_apixu(data): """ Process apixu.com forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = jobj["location"]["name"] + ", " + jobj["location"]["country"] for j in jobj["forecast"]["forecastday"]: myday = day.Day() myday.timestamp = j['date_epoch'] myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") this_day = j["day"] if metric == "1": myday.min = str(this_day['mintemp_c']) + "°C" myday.max = str(this_day['maxtemp_c']) + "°C" else: myday.min = str(this_day['mintemp_f']) + "°F" myday.max = str(this_day['maxtemp_f']) + "°F" for cond in common.CONDITIONS: if cond["code"] == this_day["condition"]["code"]: myday.icon = str(cond["icon"]) myday.text = cond["day"] break if use_icons != "1": myday.icon = "wi wi-apixu-" + myday.icon else: myday.icon = "apixu_" + myday.icon + ".png" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_wmo(data): """ Process WMO forecast """ metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = jobj['city']['cityName'] + ", " + jobj['city']['member']['memName'] # tmp = jobj['city']['forecast']['issueDate'] # "yyyy-MM-dd HH:mm:ss" # timestamp = time.mktime(time.strptime(tmp, '%Y-%m-%d %H:%M:%S')) jarr = jobj['city']['forecast']['forecastDay'] for j in jarr: myday = day.Day() myday.timestamp = time.mktime(time.strptime(j['forecastDate'], '%Y-%m-%d')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.text = j['weather'] myday.max = j['maxTemp'] + "°C" myday.min = j['minTemp'] + "°C" if metric != "1": myday.max = j['maxTempF'] + "°F" myday.min = j['minTempF'] + "°F" code = str(j['weatherIcon'])[:-2] if code == "28": myday.icon = "flaticon-cactus" elif code == "29" or code == "30": myday.icon = "flaticon-thermometer" elif code == "32": myday.icon = "flaticon-cold" elif code == "33": myday.icon = "flaticon-warm" elif code == "34": myday.icon = "flaticon-cool" else: myday.icon = "wi wi-wmo-" + code days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_yrno(data): """ Process yr.no forecast """ use_icons = common.get_string("use_icons", "0") jobj = xmltodict.parse(data) jobj = jobj['weatherdata'] location = jobj['location'] desc = location['name'] + ", " + location['country'] days = "" jarr = jobj['forecast']['tabular']['time'] for line in jarr: myday = day.Day() from_time = line['@from'] to_time = line['@to'] code = line['symbol']['@var'] myday.min = line['precipitation']['@value'] + "mm" myday.max = line['temperature']['@value'] + "°C" myday.text = line['windSpeed']['@name'] + ", " + line['windSpeed']['@mps'] + "m/s from the " myday.text += line['windDirection']['@name'] date1 = time.mktime(time.strptime(from_time, '%Y-%m-%dT%H:%M:%S')) date2 = time.mktime(time.strptime(to_time, '%Y-%m-%dT%H:%M:%S')) myday.timestamp = date1 from_time = datetime.datetime.fromtimestamp(date1).strftime("%H:%M") to_time = datetime.datetime.fromtimestamp(date2).strftime("%H:%M") date = datetime.datetime.fromtimestamp(date1).strftime("%A") myday.day = date + ": " + from_time + "-" + to_time if use_icons != "1": myday.icon = "wi wi-yrno-" + code else: myday.icon = "yrno" + code + ".png" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_owm(data): """ Process OpenWeatherMap.org forecasts """ metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = jobj["city"]["name"] + ", " + jobj["city"]["country"] for j in jobj['list']: myday = day.Day() myday.timestamp = j['dt'] myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.max = str(round(float(j['temp']['max']))) myday.min = str(round(float(j['temp']['min']))) weather = j['weather'][0] myday.text = weather['description'] myday.icon = weather['icon'] if not myday.icon.endswith('n'): myday.icon = "wi wi-owm-day-" + str(weather['id']) else: myday.icon = "wi wi-owm-night-" + str(weather['id']) if metric == "1": myday.max += "°C" myday.min += "°C" else: myday.max += "°F" myday.min += "°F" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_darksky(data): """ Process darksky forecasts """ metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = str(jobj["latitude"]) + ", " + str(jobj["longitude"]) daily = jobj["daily"] for jarr in daily["data"]: myday = day.Day() myday.icon = jarr["icon"] myday.timestamp = jarr['time'] myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.max = str(round(float(jarr['temperatureHigh']))) myday.min = str(round(float(jarr['temperatureLow']))) if metric == "1": myday.max += "°C" myday.min += "°C" else: myday.max += "°F" myday.min += "°F" myday.icon = "wi wi-forecast-io-" + myday.icon myday.text = jarr["summary"] days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" print(days) return [True, days, desc]
def process_wz(data): """ Process Weather Zone forecast """ jobj = xmltodict.parse(data) jobj = jobj['rss']['channel'] pub_date = jobj["pubDate"] desc = jobj["title"] # Sun, 12 May 2019 08:56:04 +1000 # EEE, dd MMM yyyy HH:mm:ss Z last_ts = time.mktime(time.strptime(pub_date, '%a, %d %b %Y %H:%M:%S %z')) mydesc = jobj['item'][0]["description"].split("<b>") days = "" use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") for line in mydesc: line = line.strip() if line == "": continue tmp = line.split("</b>", 1) myday = day.Day() myday.timestamp = convert_day_to_ts(tmp[0].strip(), last_ts) if myday.timestamp != 0: myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") else: myday.day = tmp[0].strip() if len(tmp) <= 1: continue mybits = tmp[1].strip().split("<br />") mybits[1] = mybits[1].strip() tmpstr = '<img src="http://www.weatherzone.com.au/images/icons/fcast_30/' myimg = mybits[1].replace(tmpstr, "") myimg = myimg.replace("\">", "").replace(".gif", "").replace("_", "-").strip() myday.text = mybits[2].strip() myrange = mybits[3].split(" - ", 1) if use_icons != "1": if myimg != "frost-then-sunny": myday.icon = "wi wi-weatherzone-" + myimg else: myday.icon = "flaticon-thermometer" else: myday.icon = "wz" + myimg.replace("-", "_") + ".png" myday.max = myrange[1].strip() myday.min = myrange[0].strip() if metric != "1": myday.max = str(round(float(myrange[1][:-7]) * 9.0 / 5.0 + 32.0, 0)) + "°F" myday.min = str(round(float(myrange[0][:-7]) * 9.0 / 5.0 + 32.0, 0)) + "°F" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_wgov(data): """ Process the data from weather.gov """ metric = common.get_string("metric", "1") days = "" jobj = json.loads(data) desc = jobj['currentobservation']['name'] # tmp = jobj['creationDate'] # date = tmp[:-3] + tmp[-2:] # print(date) # timestamp = time.mktime(time.strptime(date, '%Y-%m-%dT%H:%M:%S%z')) period_name = jobj['time']['startPeriodName'] valid_time = jobj['time']['startValidTime'] weather = jobj['data']['weather'] icon_link = jobj['data']['iconLink'] temperature = jobj['data']['temperature'] rng = range(0, len(period_name)) for i in rng: myday = day.Day() # https://forecast.weather.gov/newimages/medium/bkn.png # DualImage.php?i=bkn&j=scttsra&jp=30 # https://forecast.weather.gov/DualImage.php?i=bkn&j=scttsra&jp=30 icon_link[i] = icon_link[i].replace("http://", "https://").replace( ".png", ".jpg").strip() file_name = "wgov" + os.path.basename(icon_link[i]) if file_name.startswith("wgovDualImage.php"): tmp = icon_link[i].split("?", 1)[1].strip() fimg = simg = "" fper = sper = "" lines = tmp.split("&") for line in lines: line = line.strip() bits = line.split("=", 1) if bits[0].strip() == "i": fimg = "wgov" + bits[1].strip() + ".jpg" if bits[0].strip() == "j": simg = "wgov" + bits[1].strip() + ".jpg" if bits[0].strip() == "ip": fper = bits[1].strip() if bits[0].strip() == "jp": sper = bits[1].strip() bmp1 = Image.open(common.CACHEBASE + "/" + fimg).convert("RGBA") if fimg != simg: bmp2 = Image.open(common.CACHEBASE + "/" + simg).convert("RGBA") icon_link[i] = combine_images(bmp1, bmp2, fimg[4:-4], simg[4:-4], fper + "%", sper + "%") else: icon_link[i] = combine_image(bmp1, fper + "%", sper + "%") else: match = re.search(r"\d{2,3}", file_name) if match is not None: file_name = re.sub(r'\d{2,3}\.jpg$', ".jpg", file_name) bmp1 = Image.open(common.CACHEBASE + "/" + file_name).convert("RGBA") icon_link[i] = combine_image(bmp1, match.group(0) + "%", "%") else: icon_link[i] = file_name tmp = valid_time[i] date = tmp[:-3] + tmp[-2:] myday.timestamp = time.mktime( time.strptime(date, '%Y-%m-%dT%H:%M:%S%z')) myday.day = period_name[i] myday.max = temperature[i] + "°F" if metric == "1": myday.max = str(round( (float(temperature[i]) - 32) * 5 / 9)) + "°C" myday.text = weather[i] myday.icon = icon_link[i] days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_dwd(data): """ Process DWD.de forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") days = "" bits = data.split("<title>", 1)[1] desc = bits.split("</title>", 1)[0] desc = desc.split(' - ', 2)[2].strip() ftime = data.split('<tr class="headRow">', 1)[1].split('</tr>', 1)[0].strip() date = ftime.split('<td width="30%" class="stattime">', 1)[1].split('</td>', 1)[0].strip() ftime = date + " " + ftime.split('<td width="40%" class="stattime">', 2)[1] ftime = ftime.split(' Uhr</td>', 1)[0].strip() # dd.MM.yyyy HH last_ts = time.mktime(time.strptime(ftime, "%d.%m.%Y %H")) # dayname = datetime.datetime.fromtimestamp(timestamp).strftime("%A -- %Y-%m-%d %H") # print(dayname) data = data.split('<td width="40%" class="statwert">Vorhersage</td>', 1)[1] data = data.split('</table>', 1)[0].strip() lines = data.split('<tr') del lines[0] for line in lines: myday = day.Day() if len(line.split('<td ><b>')) > 1: myday.day = line.split('<td ><b>', 1)[1].split('</b></td>', 1)[0].strip() else: myday.day = line.split('<td><b>', 1)[1].split('</b></td>', 1)[0].strip() myday.timestamp = convert_day_to_ts(myday.day, last_ts, "de_DE") if len(line.split('<td ><img name="piktogramm" src="', 2)) > 1: myday.icon = line.split('<td ><img name="piktogramm" src="', 1)[1] myday.icon = myday.icon.split('" width="50" alt="', 1)[0].strip() else: myday.icon = line.split('<td><img name="piktogramm" src="', 1)[1] myday.icon = myday.icon.split('" width="50" alt="', 1)[0].strip() for i in line.split('<td'): if 'Grad' in i: myday.max = i myday.max = myday.max.split('Grad', 1)[0].strip()[1:] break myday.icon = myday.icon.replace('/DE/wetter/_functions/piktos/vhs_', '') myday.icon = myday.icon.replace('?__blob=normal', '').strip() myday.icon = "dwd_" + myday.icon.replace('-', '_') if use_icons != "1": myday.icon = myday.icon[4:-4] if myday.icon != "pic-48" and myday.icon != "pic-66" and myday.icon != "pic67": myday.icon = "wi wi-dwd-" + myday.icon else: myday.icon = "flaticon-thermometer" if metric == "1": myday.max += "°C" else: if myday.max != "": myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if not myday.max.startswith('°'): days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_aemet(data): """ Process AEMET forecasts """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") jobj = xmltodict.parse(data) jobj = jobj["root"] desc = jobj["nombre"] + ", " + jobj["provincia"] # print(desc) # elaborado = jobj["elaborado"] # yyyy-MM-dd'T'HH:mm:ss # timestamp = time.mktime(time.strptime(elaborado, "%Y-%m-%dT%H:%M:%S")) # dayname = datetime.datetime.fromtimestamp(timestamp).strftime("%A -- %H:%M") # print(dayname) days = "" dates = jobj['prediccion']['dia'] for date in dates: myday = day.Day() myday.day = date['@fecha'] myday.timestamp = time.mktime(time.strptime(myday.day, "%Y-%m-%d")) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.max = date['temperatura']['maxima'] myday.min = date['temperatura']['minima'] if isinstance(date['estado_cielo'], collections.OrderedDict): myday.text = date['estado_cielo']['@descripcion'] myday.icon = date['estado_cielo']['#text'] elif isinstance(date['estado_cielo'], list): for i in date['estado_cielo']: try: if isinstance(i, collections.OrderedDict) and i['#text'] != "": myday.text = i['@descripcion'] myday.icon = i['#text'] # print(myday) break except KeyError: pass if use_icons != "1": if myday.icon != "7": myday.icon = "wi wi-aemet-" + myday.icon else: myday.icon = "flaticon-thermometer" else: myday.icon = "aemet_" + myday.icon + "_g.png" if metric == "1": myday.max += "°C" myday.min += "°C" else: if myday.min != "": myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" if myday.max != "": myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if myday.min.startswith("°"): myday.min = "" if myday.max == "" or myday.max.startswith("°"): myday.max = "N/A" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_bom2(data): """ Process BoM forecasts method 2 """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") desc = data.split('<title>', 1)[1].split(' Weather - Bureau of Meteorology</title>', 1)[0] desc = desc + ", Australia" data = data.split('<div class="forecasts">', 1)[1] days = "" bits = data.split('<dl class="forecast-summary">') del bits[0] bit = bits[0] myday = day.Day() myday.day = bit.split('<a href="', 1)[1].split('">', 1)[0].split("/forecast/detailed/#d", 1)[1] myday.timestamp = time.mktime(time.strptime(myday.day, '%Y-%m-%d')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.icon = bit.split("<img src=\"", 1)[1].split("\" alt=\"", 1)[0] myday.icon = myday.icon.strip() if '<dd class="max">' in bit: myday.max = bit.split('<dd class="max">', 1)[1].split('</dd>', 1)[0].strip() if '<dd class="min">' in bit: myday.min = bit.split("<dd class=\"min\">")[1].split("</dd>")[0].strip() myday.text = bit.split('<dd class="summary">', 1)[1].split('</dd>', 1)[0].strip() file_name = os.path.basename(myday.icon)[:-4] if use_icons != "1": if file_name != "frost": myday.icon = "wi wi-bom-" + file_name else: myday.icon = "flaticon-thermometer" else: myday.icon = "bom2" + file_name.replace('-', '_') + ".png" myday.max = myday.max.replace("°C", "").replace("°C", "").strip() myday.min = myday.min.replace("°C", "").replace("°C", "").strip() if metric == "1": myday.max += "°C" myday.min += "°C" else: if myday.min != "": myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" if myday.max != "": myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if myday.min.startswith("°"): myday.min = "" if myday.max == "" or myday.max.startswith("°"): myday.max = "N/A" days += str(myday) + "," del bits[0] for bit in bits: if '</div>' in bit: bit = bit.split('</div>', 1)[0].strip() myday = day.Day() myday.day = bit.split('<a href="', 1)[1].split('">', 1)[0] myday.day = myday.day.split("/forecast/detailed/#d", 1)[1].strip() myday.timestamp = time.mktime(time.strptime(myday.day, '%Y-%m-%d')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") myday.icon = bit.split('<img src="', 1)[1].split('" alt="', 1)[0].strip() myday.max = bit.split('<dd class="max">')[1].split('</dd>')[0].strip() myday.min = bit.split('<dd class="min">')[1].split('</dd>')[0].strip() myday.text = bit.split('<dd class="summary">', 1)[1].split('</dd>', 1)[0].strip() file_name = os.path.basename(myday.icon)[:-4] if use_icons != "1": if file_name != "frost": myday.icon = "wi wi-bom-" + file_name else: myday.icon = "flaticon-thermometer" else: myday.icon = "bom2" + file_name.replace('-', '_') + ".png" myday.max = myday.max.replace("°C", "").replace("°C", "").strip() myday.min = myday.min.replace("°C", "").replace("°C", "").strip() if metric == "1": myday.max += "°C" myday.min += "°C" else: if myday.min != "": myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" if myday.max != "": myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if myday.min.startswith("°"): myday.min = "" if myday.max == "" or myday.max.startswith("°"): myday.max = "N/A" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_bom1(data): """ Process BoM FTP forecast """ use_icons = common.get_string("use_icons", "0") metric = common.get_string("metric", "1") bomtown = common.get_string("bomtown", "") if bomtown == "": return [False, "Town or suburb not set, update settings.txt"] jobj = xmltodict.parse(data)['product'] # content = jobj["amoc"]["issue-time-local"]['#text'] days = "" loc_content = None for area in jobj["forecast"]["area"]: if bomtown == area['@description']: loc_content = area break if loc_content is None: return[False, "Unable to match '" + bomtown + "'", ""] jobj = loc_content desc = jobj["@description"] + ", Australia" for forecast in jobj['forecast-period']: myday = day.Day() start_time = forecast['@start-time-local'][:-6] myday.timestamp = time.mktime(time.strptime(start_time, '%Y-%m-%dT%H:%M:%S')) myday.day = datetime.datetime.fromtimestamp(myday.timestamp).strftime("%A") if forecast['@index'] != "0": for i in range(len(forecast['element'])): if forecast['element'][i]['@type'] == "forecast_icon_code": myday.icon = forecast['element'][i]['#text'] if forecast['element'][i]['@type'] == "air_temperature_minimum": myday.min = forecast['element'][i]['#text'] if forecast['element'][i]['@type'] == "air_temperature_maximum": myday.max = forecast['element'][i]['#text'] else: try: myday.icon = forecast['element']['#text'] except Exception: for i in range(len(forecast['element'])): if forecast['element'][i]['@type'] == "forecast_icon_code": myday.icon = forecast['element'][i]['#text'] for i in range(len(forecast['text'])): if forecast['text'][i]['@type'] == "precis": myday.text = forecast['text'][i]['#text'] if use_icons != "1": if myday.icon != "14": myday.icon = "wi wi-bom-ftp-" + myday.icon else: myday.icon = "flaticon-thermometer" else: myday.icon = "bom" + myday.icon + ".png" if metric == "1": myday.max += "°C" myday.min += "°C" else: myday.min = str(round(float(myday.min) * 9.0 / 5.0 + 32.0)) + "°F" myday.max = str(round(float(myday.max) * 9.0 / 5.0 + 32.0)) + "°F" if myday.max == "°C" or myday.max == "°F" or myday.max == "": myday.max = "N/A" if myday.min == "°C" or myday.min == "°F": myday.min = "" days += str(myday) + "," if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]
def process_wca(data): """ Process forecast for weather.gc.ca """ metric = common.get_string("metric", "1") use_icons = common.get_string("use_icons", "0") days = "" last_ts = 0 obs = data.split("Forecast issued: ", 1)[1].strip() obs = obs.split("</span>", 1)[0].strip() i = 0 j = obs.index(":") hour = obs[i:j] i = j + 1 j = obs.index(" ", i) minute = obs[i:j] i = j + 1 j = obs.index(" ", i) # ampm = obs[i:j] i = j + 1 j = obs.index(" ", i) # TZ = obs[i:j] i = j + 1 j = obs.index(" ", i) # DOW = obs[i:j] i = j + 1 j = obs.index(" ", i) date = obs[i:j] i = j + 1 j = obs.index(" ", i) month = obs[i:j] i = j + 1 j = len(obs) year = obs[i:j] # obs = hour + ":" + minute + " " + ampm + " " + date + " " + month + " " + year obs = year + "-" + month + "-" + date + " " + hour + ":" + minute # + " " + ampm # "h:mm aa d MMMM yyyy" # last_ts = timestamp = time.mktime(time.strptime(obs, '%Y-%B-%d %I:%M %p')) last_ts = timestamp = time.mktime(time.strptime(obs, '%Y-%B-%d %I:%M')) date = datetime.datetime.fromtimestamp(timestamp).strftime( "%Y-%m-%d %H:%M") year = datetime.datetime.fromtimestamp(last_ts).strftime("%Y") desc = data.split('<dt>Observed at:</dt>', 1)[1] desc = desc.split('<dd class="mrgn-bttm-0">', 1)[1].split("</dd>", 1)[0].strip() data = data.split('<div class="div-table">', 1)[1].strip() data = data.split( '<section><details open="open" class="wxo-detailedfore">', 1)[0].strip() data = data[:-7].strip() doc = BeautifulSoup(data, "html.parser") div = doc.find_all("div", class_="div-column") j = 0 while j < len(div): myday1 = day.Day() myday2 = day.Day() head1 = head2 = "" content1 = content2 = "" img1 = img2 = "" maxtemp = mintemp = "" pop1 = "0%" pop2 = "0%" if '<div class="div-row div-row1 div-row-head greybkgrd">' not in str( div[j]): head1 = str(div[j]).split( '<div class="div-row div-row1 div-row-head">', 1)[1] head1 = head1.split("</div>", 1)[0].strip() tmpday = head1.split("<br/>", 1)[1] month = tmpday.split('title="', 1)[1].split('"', 1)[0].strip() tmpday = tmpday.split("\xa0", 1)[0].strip() date = tmpday + " " + month + " " + year timestamp1 = time.mktime(time.strptime(date, '%d %B %Y')) maxtemp = str(div[j]).split('title="max">', 1)[1].split("°", 1)[0].strip() if metric == "1": maxtemp += "°C" else: maxtemp = str(round(int(maxtemp) * 9 / 5 + 32)) + "°F" mystr = str(div[j]).split( '<div class="div-row div-row2 div-row-data">', 1)[1] mystr = mystr.split('</div>', 1)[0].strip() if '<p class="mrgn-bttm-0 pop text-center" title="' in mystr: pop1 = mystr.split('<small>', 1)[1].split('</small>', 1)[0].strip() if '<div class="div-row div-row3 div-row-head greybkgrd">' not in str( div[j]): if '<div class="div-row div-row3 div-row-head" ' in str(div[j]): head2 = str(div[j]) head2 = head2.split( '<div class="div-row div-row3 div-row-head" title="', 1)[1] head2 = head2.split("</div>", 1)[0].split('">', 1)[0].strip() bits = head2.split("\xa0") tmpday = bits[1] month = bits[2] date = tmpday + " " + month + " " + year timestamp2 = time.mktime(time.strptime(date, '%d %B %Y')) else: head2 = str(div[j]).split( '<div class="div-row div-row3 div-row-head">', 1)[1] head2 = head2.split("</div>", 1)[0].strip() timestamp2 = last_ts mintemp = str(div[j]).split('title="min">', 1)[1].split("°", 1)[0].strip() if metric == "1": mintemp += "°C" else: mintemp = str(round(int(mintemp) * 9 / 5 + 32)) + "°F" mystr = str(div[j]).split( '<div class="div-row div-row4 div-row-data">', 1)[1] mystr = mystr.split('</div>', 1)[0].strip() if '<p class="mrgn-bttm-0 pop text-center" title="' in mystr: pop2 = mystr.split('<small>', 1)[1].split('</small>', 1)[0].strip() srcs = [img['src'] for img in div[j].find_all('img')] alts = [alt['alt'] for alt in div[j].find_all('img')] if head1 != "": count = len(srcs) if count > 0: img1 = srcs[0] content1 = alts[0] if head2 != "": if count > 1: img2 = srcs[1] content2 = alts[1] else: count = len(srcs) if count > 0: img2 = srcs[0] content2 = alts[0] if head1 != "": img1 = img1[14:-4] if use_icons == "1": img1 = "wca" + img1 + ".png" else: if img1 == "26": img1 = "flaticon-thermometer" else: img1 = "wi wi-weather-gc-ca-" + img1 myday1.text = content1 myday1.icon = img1 myday1.max = maxtemp last_ts = myday1.timestamp = timestamp1 myday1.day = datetime.datetime.fromtimestamp( myday1.timestamp).strftime("%A") myday1.min = pop1 days += str(myday1) + "," if head2 != "": img2 = img2[14:-4] if use_icons == "1": img2 = "wca" + img2 + ".png" else: if img2 == "26": img2 = "flaticon-thermometer" else: img2 = "wi wi-weather-gc-ca-" + img2 myday2.text = content2 myday2.icon = img2 myday2.max = mintemp last_ts = myday2.timestamp = timestamp2 myday2.day = datetime.datetime.fromtimestamp( myday2.timestamp).strftime("%A") myday2.day += " Night" myday2.min = pop2 days += str(myday2) + "," j += 1 if days[-1:] == ",": days = days[:-1] days = "[" + days + "]" return [True, days, desc]