From ebb16d1dabedfdc3fc392b8c36f46a486ee3549f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 9 Nov 2018 23:26:42 +0100 Subject: Initial commit.t --- 100-2.html | 25 ++++++++++++++++++ 100.html | 46 ++++++++++++++++++++++++++++++++ Main.hs | 8 ++++++ parse.scm | 3 +++ script.scm | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ texttv.py | 24 +++++++++++++++++ wrapper.sh | 2 ++ 7 files changed, 197 insertions(+) create mode 100644 100-2.html create mode 100644 100.html create mode 100644 Main.hs create mode 100644 parse.scm create mode 100755 script.scm create mode 100755 texttv.py create mode 100755 wrapper.sh diff --git a/100-2.html b/100-2.html new file mode 100644 index 0000000..8871c2d --- /dev/null +++ b/100-2.html @@ -0,0 +1,25 @@ +
100 SVT Text Fredag 09 nov 2018 + + + + + +

Ätstörningar bland ungdomar fördubblas

+ Allt fler i åldern 10-14 år söker vård + 106 + + Man död efter skjutning + i Uppsala i går kväll + 108 + +

Kalifornien-stad förstörd i skogsbrand

+ Räddningstjänsten: Staden helt ödelagd + 130 + + Österrikisk överste spionmisstänkt 136 + + + + +

Inrikes 101 Utrikes 104 Innehåll 700

+
diff --git a/100.html b/100.html new file mode 100644 index 0000000..defc465 --- /dev/null +++ b/100.html @@ -0,0 +1,46 @@ + + +
100 SVT Text Fredag 09 nov 2018 + + + + + +

Ätstörningar bland ungdomar fördubblas

+ Allt fler i åldern 10-14 år söker vård + 106 + + Man död efter skjutning + i Uppsala i går kväll + 108 + +

Kalifornien-stad förstörd i skogsbrand

+ Räddningstjänsten: Staden helt ödelagd + 130 + + Österrikisk överste spionmisstänkt 136 + + + + +

Inrikes 101 Utrikes 104 Innehåll 700

+
diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..c0e4ed0 --- /dev/null +++ b/Main.hs @@ -0,0 +1,8 @@ +import Text.ParserCombinators.Parsec + +docParser :: GenParser Char () Doc + +parseLine :: + +main :: IO () +main = putStrLn "HaHa" diff --git a/parse.scm b/parse.scm new file mode 100644 index 0000000..fa4a5b9 --- /dev/null +++ b/parse.scm @@ -0,0 +1,3 @@ +(use-modules (ice-9 rdelim)) + +(read-delimited '(#\< #\> #\" #\space)) diff --git a/script.scm b/script.scm new file mode 100755 index 0000000..c2027b1 --- /dev/null +++ b/script.scm @@ -0,0 +1,89 @@ +#!/usr/bin/guile \ +-e main -s +!# + +(use-modules (sxml simple) + (sxml match) + (ice-9 match) + (srfi srfi-1)) + +(define (setmode . args) + "Sets display mode" + (with-output-to-string + (lambda () + (for-each display + `(#\escape #\[ ,@args #\m))))) + + +(define* (deffunc name idx + #:optional (func-pre '#{}#) (mode-pre "")) + (let ((str (gensym))) + `(define (,(symbol-append func-pre name) . ,str) + (string-append + (setmode ,mode-pre ,idx) + #; (string #\escape #\[ #\K) + (string-concatenate ,str) + (setmode 0))))) + +(define-macro (create-modes modes) + `(begin + ,@(map deffunc + modes + (iota (length modes))))) + +(define-macro (create-colors modes) + `(begin + ,@(apply append + (map (lambda (itm idx) + (list (deffunc itm idx 'fg- "0;3") + (deffunc itm idx 'bg- "0;4"))) + modes + (iota (length modes)))))) + +(create-modes (off bold dim slant underline)) +(create-colors (black red green yellow blue purple cyan white)) + +(define classmap + (match-lambda ("B" fg-blue) + ("bgB" bg-blue) + ("Y" fg-yellow) + ("C" fg-cyan) + ("DH" bold) + (_ identity))) + +(define (class-handlers class-str) + (fold compose identity + (map classmap (string-split class-str #\space)))) + +(define (fmt-sub nodes) + (string-concatenate (map fmt-tag nodes))) + +(define (fmt-tag tag) + (sxml-match tag + [(a #; (@ (class ,class)) ,text) + (underline text)] + + [(h1 (@ (class ,class)) ,nodes ...) + ((class-handlers class) + (bold (fmt-sub nodes)))] + + [(span (@ (class ,class)) ,nodes ...) + ((class-handlers class) + (fmt-sub nodes))] + + [,str (guard (string? str)) str] + + [,default (format #f "[|~a|]" default)] + )) + +(define (parse-doc sxml) + (sxml-match sxml + [(*TOP* (div #; (span (@ (class "toprow")) ,top-row ...) + ,spans ...)) + spans + ])) + +(define (main args) + (define d (call-with-input-file "100-2.html" xml->sxml)) + (display (string-concatenate (map fmt-tag (parse-doc d)))) + (newline)) diff --git a/texttv.py b/texttv.py new file mode 100755 index 0000000..a6cf2b1 --- /dev/null +++ b/texttv.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python2 + +from httplib2 import Http +from urllib import urlencode +import json +import re +import sys + +def cleanhtml(raw_html): + cleanr = re.compile('<.*?>') + cleantext = re.sub(cleanr, '', raw_html) + return cleantext + +if len(sys.argv) > 1: + page = sys.argv[1] +else: + page = '100' + +http = Http() +url = 'http://api.texttv.nu/api/get/' + page + '?app=python' +headers = {'Accept': 'application/json', 'Accept-Language': 'en-US,en'} +response, content = http.request(url, 'GET', headers=headers) +data = json.loads(content) +print cleanhtml(data[0]['content'][0]) diff --git a/wrapper.sh b/wrapper.sh new file mode 100755 index 0000000..11d9236 --- /dev/null +++ b/wrapper.sh @@ -0,0 +1,2 @@ +curl http://api.texttv.nu/api/get/100?app=hugonikanor \ + | jq -j '.[0].content[0]' \ -- cgit v1.2.3