Skip to content

Instantly share code, notes, and snippets.

@jtangelder jtangelder/teletekst.py

Last active Nov 7, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.