Skip to content

Instantly share code, notes, and snippets.

@jtangelder
Last active June 8, 2021 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtangelder/32b7af5031a4e22198c6 to your computer and use it in GitHub Desktop.
Save jtangelder/32b7af5031a4e22198c6 to your computer and use it in GitHub Desktop.
CLI teletekst.
import urllib.request
import urllib.error
import json
import re
import cmd
import html
def getTeletekstPage(page):
url = 'http://teletekst-data.nos.nl/json/%s' % (page)
res = urllib.request.urlopen(url)
return json.loads(res.read().decode('utf-8'))
def parseLine(line):
line = re.sub(r"<[^<>]+>", "", line)
line = re.sub(r"&\#x[a-zA-Z0-9]+;", "&#x2588;", line)
return html.unescape(line);
def printContent(content):
lines = content.splitlines();
for line in lines:
print(parseLine(line))
class TeletekstShell(cmd.Cmd):
prompt = '(teletekst) '
def onecmd(self, pageId):
try:
page = getTeletekstPage(pageId)
printContent(page.get('content'))
except urllib.error.HTTPError as err:
print(err.reason)
if __name__ == '__main__':
TeletekstShell().cmdloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment