txtv

Swiss text tv in the terminal
git clone https://git.in0rdr.ch/txtv.git
Log | Files | Refs | Pull requests |Archive | README | LICENSE

commit c071110ce829193b326bfefc16c8e25f0ad4a1af
parent 09f2df624d91371d2ca3ab539c352e191b74569e
Author: Isak Lindhé <isak.e.lindhe@gmail.com>
Date:   Thu, 20 Dec 2018 22:37:46 +0100

error checking refactor

Diffstat:
Mtxtv.py | 27++++++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/txtv.py b/txtv.py @@ -2,16 +2,33 @@ import bs4 import requests as rq import colorama from colorama import Fore, Back, Style -from sys import argv +import sys -if __name__ == '__main__': - colorama.init() - page = int(argv[1]) - res = rq.get(f'https://www.svt.se/svttext/web/pages/{page}.html') +def err(txt): + print(Fore.RED + txt + Fore.RESET, file=sys.stderr) + sys.exit(1) + + +def get_page(num) -> list: + res = rq.get(f'https://www.svt.se/svttext/web/pages/{num}.html') + if res.status_code == 404: + err(f'There is no page number {num}. Pretty sure they start at 100.') + elif res.status_code != 200: + err(f'When i tried to get the page i just got HTTP status code {res.status_code}.') soup = bs4.BeautifulSoup(res.content, 'html.parser') root = soup.find('pre', class_='root') rows = root.find_all('span') + return rows + + +if __name__ == '__main__': + colorama.init() + try: + page = int(sys.argv[1]) + except ValueError: + err('txtv <PAGE>\nexample: txtv 130') + rows = get_page(page) for row in rows: if row.get_text() == ' ' or 'bgB' in row.attrs['class']: continue