def news_headlines(entries, conf, region=None): pagenum = conf['headlines'] if region: page = ttxpage.TeletextPage(f"Regional Headlines {pagenum:03x}", pagenum) page.header(pagenum) category = newsheaders.newsheader(page, region) nextpage = conf['first'] maxlines = 9 else: page = ttxpage.TeletextPage(f"News Headlines {pagenum:03x}", pagenum) page.header(pagenum) category = newsheaders.newsheader(page, 'headlines') nextpage = conf['first'] maxlines = 8 line = 4 count = 0 for contents in entries: if line == 4: textattr = ttxcolour.colour(ttxcolour.DOUBLE_HEIGHT) else: textattr = ttxcolour.colour(ttxcolour.ALPHAWHITE) headline = page.truncate(contents['short_title'], 70, ' ') headlines = textwrap.wrap(headline, 35) page.addline( line, '{}{:<35}{}{:03x}'.format(textattr, headlines[0], ttxcolour.yellow(), nextpage)) nextpage = ttxutils.nextpage(nextpage) if line <= 4: line += 2 else: line += 1 if line >= 22: break if len(headlines) > 1: page.addline(line, '{} {}'.format(ttxcolour.white(), headlines[1])) if line < 7 and region is None: line += 2 else: line += 1 count += 1 if count >= maxlines: break newsheaders.newsheadlinesfooter(page, category) page.save()
def weatherindex(regionhead, ukhead): cfg = _config["weather"] page = ttxpage.TeletextPage("Weather Index", cfg["index"]) regionheads = textwrap.wrap(page.fixup(regionhead), 35) regionhead = f"{regionheads[0]:<35.35}" regionheads = regionheads[1:] headline = "WEATHER NEWS:¬" + page.fixup(ukhead) headlines = textwrap.wrap(headline, 39) for i in range(len(headlines)): l = headlines[i] if i == 0: l = ttxcolour.yellow() + l.replace("¬", ttxcolour.green()) elif i == len(headlines) - 1: l = ( ttxcolour.green() + f"{l:<35.35}" + ttxcolour.yellow() + f"{cfg['maps']:03x}" ) else: l = ttxcolour.green() + l headlines[i] = l header = [ "␗j#3kj#3kj#3k␔␝␑␓h44|h<h<|(|$|h4|$|l ", "␗j $kj $kj 'k␔␝␑␓*uu?jwj7␡ ␡ ␡k5␡1␡k4 ", '␗"###"###"###␔////,,.-,-.,/,/,-.,.,-.///', ] middle = [ "␃UK␄````````````````````````````````````", "␆Forecast maps ␃401␆UK Cities 5 Day␃406", "␆Regions ␃402␆ ␃ ", "␆National ␃403␆ ␃ ", "␆Current Weather␃404 ", " ", ] attrib = "From the Met Office" attrib = f"{attrib:^39}"[2:] footer = headlines + [ "", ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + f"{ttxcolour.yellow()}{attrib}", ttxutils.decode("€AMaps €BRegional €COutlook €FMain Menu"), ] page.header(cfg["index"]) line = 1 for l in header: page.addline(line, ttxutils.decode(l)) line += 1 page.addline( line, ttxcolour.colour(ttxcolour.DOUBLE_HEIGHT) + regionhead + ttxcolour.yellow() + f"{cfg['regional']:03x}", ) line += 2 for l in regionheads: page.addline(line, " " + l) line += 1 line += 1 for l in middle: page.addline(line, ttxutils.decode(l)) line += 1 line = 25 - len(footer) for l in footer: page.addline(line, l) line += 1 page.addfasttext( cfg["maps"], cfg["regional"], cfg["national"], 0x100, 0x8FF, 0x199 ) page.save()
def weatherfiveday(W): cfg = _config["weather"] maximum = -9999 minimum = 9999 today = None entries = [] for city, num in fiveday_ids.items(): wx = W.loc_forecast(num, metoffer.DAILY).data if today is None: today = wx[0]["timestamp"][0] entry = [city] for i in range(5): wx_type = short_weathers.get(wx[i * 2]["Weather Type"][0], "N/A") max_temp = int(wx[i * 2]["Day Maximum Temperature"][0]) min_temp = int(wx[i * 2 + 1]["Night Minimum Temperature"][0]) date = wx[i * 2]["timestamp"][0] entry.append( f"{date.strftime('%a')}" f"{max_temp:>4}{min_temp:>4} {wx_type}" ) if max_temp > maximum: maximum = max_temp if min_temp < minimum: minimum = min_temp entries.append(entry) header = [ "␗j#3kj#3kj#3k␔␝␑␓h44|h<h<|(|$|h4|$|l ", "␗j $kj $kj 'k␔␝␑␓*uu?jwj7␡ ␡ ␡k5␡1␡k4 ", '␗"###"###"###␔////,,.-,-.,/,/,-.,.,-.///', ] temps_c = [] temps_f = [] steps = (maximum - minimum) / 10 if steps < 1: steps = 1 minimum = (minimum + maximum) // 2 - 5 for i in range(11): temp_c = round(minimum + i * steps) temp_f = round((temp_c * 1.8) + 32) c = str(temp_c) f = str(temp_f) l = max(len(c), len(f)) temps_c.append(f"{c:>{l}}") temps_f.append(f"{f:>{l}}") footer = [] back = ( ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + ttxcolour.yellow() ) footer.append(f"{back}C= " + " ".join(temps_c)) footer.append(f"{back}F= " + " ".join(temps_f)) footer.append( ttxutils.decode("€AUK map €B Sport €CWeather €FMain Menu") ) page = ttxpage.TeletextPage("Weather Five Day", cfg["fiveday"]) numpages = (len(entries) + 3) // 4 for sub in range(numpages): body = [] top = f"{sub+1}/{numpages}" body.append(f"{top:>39.39}") body.append( ttxcolour.yellow() + "UK FIVE DAY FORECAST FROM " + today.strftime("%e %b").strip() ) body.append(ttxcolour.green() + "max for 0600-1800 min for 1800-0600") body.append( ttxcolour.yellow() + " max min" + ttxcolour.white() + "C " + ttxcolour.yellow() + " max min" + ttxcolour.white() + "C" ) for i in range(2): count = 0 c1 = [] c2 = [] offset = (sub * 4) + i if offset < len(entries): c1 = entries[offset] offset = (sub * 4) + i + 2 if offset < len(entries): c2 = entries[offset] while len(c1) and len(c2): row = "" if count % 2: row += ttxcolour.cyan() else: row += ttxcolour.white() if len(c1): row += f"{c1[0]:<19.19} " c1 = c1[1:] if len(c2): row += c2[0] c2 = c2[1:] body.append(row) count += 1 body.append("") page.header(cfg["fiveday"], sub + 1, status=0xC000) line = 1 for l in header: page.addline(line, ttxutils.decode(l)) line += 1 for l in body: page.addline(line, l) line += 1 line = 25 - len(footer) for l in footer: page.addline(line, l) line += 1 page.addfasttext(cfg["maps"], 0x300, cfg["index"], 0x100, 0x8FF, 0x199) page.save()
def weatherobservations(W): cfg = _config["weather"] minimum = 9999 maximum = -9999 rows = [] timestamp = None for city, num in observation_ids.items(): obs = W.loc_observations(num, cache_hours=1).data time = None temp = '-' wind_speed = '-' wind_dir = '-' pressure = '-' trend = '-' weather_type = None for o in obs: if "timestamp" in o: time = o["timestamp"][0] if "Temperature" in o: temp = round(float(o["Temperature"][0])) if "Wind Speed" in o: wind_speed = o["Wind Speed"][0] if "Wind Direction" in o: wind_dir = o["Wind Direction"][0] if "Pressure" in o: pressure = o["Pressure"][0] if "Pressure Tendency" in o: trend = o["Pressure Tendency"][0] if "Weather Type" in o: weather_type = o["Weather Type"][0] if len(rows) % 2 == 1: row_colour = ttxcolour.white() else: row_colour = ttxcolour.cyan() if trend == "F": trend_colour = ttxcolour.green() elif trend == "S": trend_colour = ttxcolour.white() else: trend_colour = ttxcolour.cyan() row = f"{row_colour}{city:<12.12}{temp:>4}{wind_dir:>4}{wind_speed:>3}" row += f"{pressure:>6}{trend_colour}{trend}{row_colour}" if weather_type is not None: row += short_weathers[weather_type] else: row += "n/a" rows.append(row) if time is not None and (timestamp is None or time > timestamp): timestamp = time if temp and temp != '-': if temp < minimum: minimum = temp if temp > maximum: maximum = temp header = [ "␗j#3kj#3kj#3k␔␝␑␓h44|h<h<|(|$|h4|$|l ", "␗j $kj $kj 'k␔␝␑␓*uu?jwj7␡ ␡ ␡k5␡1␡k4 ", '␗"###"###"###␔////,,.-,-.,/,/,-.,.,-.///', ] temps_c = [] temps_f = [] steps = (maximum - minimum) / 10 if steps < 1: steps = 1 minimum = (minimum + maximum) // 2 - 5 for i in range(11): temp_c = round(minimum + i * steps) temp_f = round((temp_c * 1.8) + 32) c = str(temp_c) f = str(temp_f) l = max(len(c), len(f)) temps_c.append(f"{c:>{l}}") temps_f.append(f"{f:>{l}}") footer = [] footer.append(ttxutils.decode(" ␃pressure␆R␃rising␇S␃steady␂F␃falling")) footer.append("") back = ( ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + ttxcolour.yellow() ) footer.append(f"{back}C= " + " ".join(temps_c)) footer.append(f"{back}F= " + " ".join(temps_f)) footer.append( ttxutils.decode("€AUK 5 day€B Sport €CWeather €FMain Menu") ) page = ttxpage.TeletextPage("Weather Reports", cfg["observations"]) for subpage in (1, 2, 3): body = [] top = f"{subpage}/3" body.append(f"{top:>39.39}") time = timestamp.strftime("%H%M") body.append(f"{ttxcolour.yellow()}CURRENT UK WEATHER: Report at {time}") body.append("") body.append(ttxutils.decode(" ␃temp wind pres")) body.append(ttxutils.decode(" ␇C mph mB")) base = (subpage - 1) * 10 for l in rows[base : base + 10]: body.append(l) page.header(cfg["observations"], subpage, status=0xC000) line = 1 for l in header: page.addline(line, ttxutils.decode(l)) line += 1 for l in body: page.addline(line, l) line += 1 line = 25 - len(footer) for l in footer: page.addline(line, l) line += 1 page.addfasttext( cfg["fiveday"], 0x300, cfg["index"], 0x100, 0x8FF, 0x199 ) subpage += 1 page.save()
def weatheroutlook(W): cfg = _config["weather"] wx_text = W.text_forecast(metoffer.REGIONAL_FORECAST, regions["uk"]) headline = wx_text.data[0][1] page = ttxpage.TeletextPage("Weather Page", cfg["national"]) pages = [] for i in range(3): head = wx_text.data[i + 1][0].replace(":", "") head = textwrap.wrap(page.fixup(head), 39) body = wx_text.data[i + 1][1] body = re.sub(r" M(ax|in)imum Temperature \d+C.*", r"", body) body = textwrap.wrap(page.fixup(body), 39) block = [] for h in head: block.append(f" {h:<.39}") for b in body: block.append(f"{ttxcolour.cyan()}{b:<.39}") if len(pages) and len(pages[-1]) + len(block) + 1 <= 15: pages[-1].append('') pages[-1].extend(block) else: pages.append(block) header = [ "␗j#3kj#3kj#3k␔␝␑␓h44|h<h<|(|$|h4|$|l ", "␗j $kj $kj 'k␔␝␑␓*uu?jwj7␡ ␡ ␡k5␡1␡k4 ", '␗"###"###"###␔////,,.-,-.,/,/,-.,.,-.///', ] title = "From the Met Office" title = f"{title:^39}"[2:] footer = [ ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + f"{ttxcolour.yellow()}{title}", ttxutils.decode("€AUK cities €BSport €CWeather €FMain Menu"), ] for subpage in range(len(pages)): rows = [] top = f"{subpage + 1}/{len(pages)}" rows.append(f"{top:>39.39}") rows.append(" UK WEATHER OUTLOOK") rows.append("") rows.extend(pages[subpage]) page.header(cfg["national"], subpage + 1, status=0xC000) line = 1 for l in header: page.addline(line, ttxutils.decode(l)) line += 1 for l in rows: page.addline(line, l) line += 1 line = 25 - len(footer) for l in footer: page.addline(line, l) line += 1 page.addfasttext( cfg["observations"], 0x300, cfg["index"], 0x100, 0x8FF, 0x199 ) page.save(add_to_newsreel=True) return headline
def weathermaps(W): cfg = _config["weather"] if datetime.datetime.now().hour > 16: advance = 1 else: advance = 0 pagenum = cfg["maps"] subpage = 1 page = ttxpage.TeletextPage("Weather Maps", pagenum, time=10) for index in range(3): caption = None advanced = False page.header(pagenum, subpage, status=0xC000) region_wx = dict() region_wx_reduced = dict() for reg_name, reg_id in region_ids.items(): w = W.loc_forecast(reg_id, metoffer.DAILY) now = w.data[index + advance] if (not advanced and now["timestamp"][0].date() < datetime.datetime.now().date()): # handle overnight transition advanced = True advance += 1 now = w.data[index + advance] wx_type = now["Weather Type"][0] wx_type_full = weathers.get(wx_type, "N/A") if wx_type_full in region_wx: region_wx[wx_type_full].append(reg_name) else: region_wx[wx_type_full] = [reg_name] if wx_type in reduction_mapping: wx_type = reduction_mapping[wx_type] wx_type_reduced = weathers.get(wx_type, "N/A") if wx_type_reduced in region_wx_reduced: region_wx_reduced[wx_type_reduced].append(reg_name) else: region_wx_reduced[wx_type_reduced] = [reg_name] if len(region_wx.keys()) > len(colours_a): # Reduce colours in weather map region_wx = region_wx_reduced map = weathermap.WeatherMap() reg_mapping = dict() i = 0 for wx, locs in region_wx.items(): if i >= len(colours_a): print(f"Out of colours on weather map {subpage}", flush=True) i = 0 map.plot_text(firstmap(locs), colours_a[i], wx) for loc in locs: reg_mapping[loc] = colours_m[i] i += 1 for city_name, city_id in city_ids.items(): w = W.loc_forecast(city_id, metoffer.DAILY) now = w.data[index + advance] if not caption: caption = now["timestamp"][0].strftime("%A") if now["timestamp"][1] == "Night": caption += " night" if now["timestamp"][1] == "Day": wx_temp = now["Day Maximum Temperature"][0] else: wx_temp = now["Night Minimum Temperature"][0] map.plot_temp(city_name, wx_temp) # This needs to go after temperatures, as it changes # the colours following the numbers map.plot_borders(reg_mapping) title = f"Weather for {caption}" title = f"{title:^39}"[2:] page.addline( 1, ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + f"{ttxcolour.yellow()}{title}", ) l = 2 subpagenum = chr(ttxcolour.ALPHAWHITE) + f"{subpage}/3" map.put_text(2, 35, subpagenum) for ll in map.map_lines(): page.addline(l, ll) l += 1 title = "From the Met Office" title = f"{title:^39}"[2:] page.addline( 23, ttxcolour.blue() + ttxcolour.colour(ttxcolour.NEW_BACK) + f"{ttxcolour.yellow()}{title}", ) page.addline( 24, ttxutils.decode("€ARegional €BSport €CWeather €FMain Menu") ) page.addfasttext( cfg["regional"], 0x300, cfg["index"], 0x100, 0x8FF, 0x199 ) subpage += 1 page.save(add_to_newsreel=True)
def index_page(category : str, pages : dict, header : List[str], footer : List[str], entries : list, fasttext : Optional[List[int]] = None, increment : int =1, rule: Optional[str] = None, number_colour: str = ttxcolour.white()): index = pages['index'] page = ttxpage.TeletextPage( f"{category} Index {index:03x}", index) page.header(index) line = 1 for l in header: page.addline(line, decode(l)) line += 1 colour = ttxcolour.colour(ttxcolour.DOUBLE_HEIGHT) number = pages['first'] index = 0 for contents in entries: if increment == 1: if index in [ 1, 6, 10 ]: line += 1 elif index == 1: line += 1 if line == rule: page.addline(line, f"{ttxcolour.magenta()}" "```````````````````````````````````````") line += 1 title = contents['short_title'] l, _, r = title.partition(': ') if r: title = r title = page.truncate(title, 35, ' ') page.addline(line, f"{colour}{title:<35.35}{number_colour}{number:03x}") colour = ttxcolour.cyan() line += increment index += 1 number = nextpage(number) if number > pages['last']: break line = 25 - len(footer) for l in footer: page.addline(line, decode(l)) line += 1 if fasttext and len(fasttext) in [3, 4, 6]: if len(fasttext) == 3: f = [pages['first'], *fasttext, 0x8ff, 0x199] elif len(fasttext) == 4: f = [*fasttext, 0x8ff, 0x199] else: f = fasttext page.addfasttext(*f) else: page.addfasttext(pages['first'], 0x100, 0x101, 0x100, 0x8ff, 0x199) page.save()