Skip to content

Instantly share code, notes, and snippets.

@albert-tomanek
Created November 9, 2024 20:09
Show Gist options
  • Save albert-tomanek/b73ccadba63de803de700698df60e96c to your computer and use it in GitHub Desktop.
Save albert-tomanek/b73ccadba63de803de700698df60e96c to your computer and use it in GitHub Desktop.
Česká Televize (ČT) teletext terminal viewer
#!/bin/env python3
import time, json
import termbox as TB
from urllib.request import urlopen
try:
with urlopen(f"https://api-teletext.ceskatelevize.cz/teletext-api/v2/text/?t={int(time.time())}") as f:
tt_json = f.read().decode('utf-8')
pages = json.loads(tt_json)['data']
except Exception as err:
print('Error fetching teletext.')
print()
print(err)
quit()
class Viewer:
def __init__(self, page=100):
self.page = str(page)
self.subpage_i = 0
def get_subpage_codes(self):
try:
return pages[self.page]['subpages']
except KeyError:
return []
def get_text(self):
try:
if self.get_subpage_codes(): # If there are subpages
text = pages[self.page]['text'][self.page + self.get_subpage_codes()[self.subpage_i]]
else:
text = pages[self.page]['text'][self.page]
return text.replace('<pre>', '').replace('</pre>', '')
except KeyError:
return "Page not found."
def next_subpage(self):
if (not self.get_subpage_codes()) or (self.subpage_i == len(self.get_subpage_codes()) - 1):
self.page = str(int(self.page) + 1)
self.subpage_i = 0
else:
self.subpage_i += 1
def prev_subpage(self):
if (not self.get_subpage_codes()) or (self.subpage_i == 0):
self.page = str(int(self.page) - 1)
self.subpage_i = len(self.get_subpage_codes()) - 1
else:
self.subpage_i -= 1
def run(self):
tb = TB.Termbox()
buffer = ''
history = []
while True:
tb.clear()
# Draw UI
for x in range(0, tb.width()):
tb.change_cell(x, tb.height() - 1, ord(' '), TB.BLACK, TB.WHITE)
tb_print(tb, tb.width() - 10, tb.height() - 1, f'Page {self.page}' + (f'.{self.subpage_i}' if self.get_subpage_codes() else ''), TB.BLACK, TB.WHITE)
tb_print(tb, 0, tb.height() - 1, '[ ]', TB.BLACK, TB.WHITE)
tb_print(tb, 1, tb.height() - 1, (' ' * (3 - len(buffer))) + buffer[-3:], TB.BLACK, TB.WHITE)
# Draw text
text = self.get_text()
w, h = len(text.splitlines()[0]), len(text.splitlines())
tb_print(tb, (tb.width() - w) // 2, (tb.height() - h) // 2, text, TB.WHITE, TB.DEFAULT)
tb.present()
# Wait for events
match tb.poll_event():
case (TB.EVENT_KEY, 'q', *args):
break;
case (TB.EVENT_KEY, c, *args) if str(c).isdigit():
buffer += c;
case (TB.EVENT_KEY, _, TB.KEY_BACKSPACE, *args) | (TB.EVENT_KEY, _, TB.KEY_BACKSPACE2, *args):
buffer = buffer[:-1]
case (TB.EVENT_KEY, _, TB.KEY_ENTER, *args):
self.page = buffer
history.append(buffer) # Add to history
self.subpage_i = 0
buffer = ''
case (TB.EVENT_KEY, _, TB.KEY_ARROW_LEFT, TB.MOD_ALT, *args):
if history:
self.page = history.pop()
self.subpage_i = 0
case (TB.EVENT_KEY, '[', *args):
self.page = str(int(self.page) - 1)
self.subpage_i = 0
case (TB.EVENT_KEY, ']', *args):
self.page = str(int(self.page) + 1)
self.subpage_i = 0
case (TB.EVENT_KEY, _, TB.KEY_ARROW_RIGHT, *args):
self.next_subpage()
case (TB.EVENT_KEY, _, TB.KEY_ARROW_LEFT, *args):
self.prev_subpage()
tb.close()
def tb_print(tb, x, y, text, fg, bg):
cx, cy = x, y
for c in text:
if c == '\n':
cx = x
cy += 1
continue
tb.change_cell(cx, cy, ord(c), fg, bg)
cx += 1
if __name__ == '__main__':
v = Viewer()
v.run()
@albert-tomanek
Copy link
Author

Screenshot_20241109_211251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment