]> git.gir.st - dst-live.git/blob - format
provide a wrapper for simplifying invocation
[dst-live.git] / format
1 #!/usr/bin/awk -f
2
3 # DerStandard live ticker command line viewer
4 # Pipe the output of 'ticker' into it.
5 # Set -vimg=1 to enable inline images.
6 # Command line arguments:
7 # * -vimg=1 enable inline images
8 # * -vnoembed=1 only show title and body text
9 # * -vwdiff=1 highlight changes in updates
10 #
11 # requries: fold, [curl, viu (https://github.com/atanunq/viu)], [wdiff]
12 #
13 # Copyright (C) 2019 Tobias Girstmair; GNU GPL v3
14
15 $1 ~ /RedContentItem/ {
16 # $1=type(new/update), $2=posting-id, $3=createdate $4=headline,
17 # $5=body, $6=comment-title, $7=comment-body, $8=ExternalProviderUri,
18 # $9=MediaUrl, $10=EmbedCode
19 active_post = $2;
20 gsub(/\\r/, ""); # some posts have dos-lineendings
21 gsub(/\\n*$/, ""); # trim newlines at the very end
22 gsub(/\\n/, "\n"); # unescape lineendings (no other escapes)
23
24 time = substr($3, 12, 5);
25
26 print "\n---\n"; # seperator
27 printf (faint time " " reset); # time
28 if (wdiff && $1 ~ "update") {
29 if ($4) tprint(bold $4); # title; TODO: diff it
30 diff(diff_archive[$2], $5); # body diff
31 } else {
32 if ($4) tprint(bold $4); # title
33 if ($5) tprint($5); # body text
34 }
35 diff_archive[$2] = $5;
36
37 if (noembed) next;
38 # Attachments:
39 if ($6 || $7) { # user comment
40 tprint(italic "\n" join2($6,": ",$7));
41 } else if (!$8 && $9) { # image
42 if (img) # display inline using unicode half-blocks
43 system ("curl -s '" img_dst_at $9 "' | viu -w" w);
44 else # just print a link
45 print img_dst_at $9;
46 } else if ($8 ~ /va.derstandard.at/) { # video
47 if (match($10, /src="[^"]*"/, url)) {
48 url[0] = substr(url[0], 6, length(url[0])-6);
49 sub(/^\/\//, "https://", url[0]);
50 tprint(url[0]);
51 }
52 } else if ($8 ~ /twitter.com/) {
53 gsub(/<[^<]*>/, "", $10); #strip tags (naive html2text)
54 gsub(/&amp;/, "&", $10); #unescape html entities
55 gsub(/&quot;/, "\"", $10); gsub(/&#39;/, "'", $10); # -"-
56 gsub(/&lt;/, "<", $10); gsub(/&gt;/, ">", $10); # -"-
57 gsub(/&mdash;.*$/, "", $10); #strip author (very greedy)
58 gsub(/[⃿-⯿𐀀-􏿽]/, "", $10); #kill emoji (2100-2BFF,10000-10FFFD)
59 tprint("\n" italic $10);
60 } else if ($8) { # other embeds (mostly facebook?)
61 split($8, url, "?"); # remove tracking-query-string
62 tprint(url[0]);
63 }
64 }
65 $1 == "updateVotes" && $2 == active_post {
66 # $1=type(new/update), $2=posting-id, $3=upvotes, $4=downvotes
67 if (!$3 && !$4) next; # don't print -0/+0
68 printf ($4?red:faint) "-" $4 reset " / ";
69 printf ($3?green:faint) "+" $3 reset "\r";
70 }
71 BEGIN {
72 FS="\t" # input is tab-delimetered
73 ENVIRON["PATH"] = ENVIRON["PATH"] ":." # XXX: fallback to local viu
74 w = "60"; # print width
75
76 # some useful constants:
77 bold = "\033[1m";
78 faint = "\033[2m";
79 italic = "\033[3m";
80 reverse = "\033[7m";
81 reset = "\033[0m";
82 red = "\033[91m";
83 green = "\033[92m";
84 img_dst_at = "https://images.derstandard.at/t/LB109";
85 # diff highlighing:
86 dels = "\033[0;9;91m"
87 dele = "\033[0;2m"
88 inss = "\033[0;3;92m"
89 inse = "\033[0;2m"
90 }
91 function tprint(text) {
92 foldcom = "fold -sw " w;
93 if ($1=="updateRedContentItem") printf faint;
94 print text reset | foldcom;
95 close (foldcom);
96 }
97 function diff(old, new) {
98 print old > "/tmp/diff1"; close("/tmp/diff1"); # XXX: not threadsafe
99 print new > "/tmp/diff2"; close("/tmp/diff2");
100 printf faint;
101 system ("wdiff -w '"dels"' -x '"dele"' -y '"inss"' -z '"inse"' /tmp/diff1 /tmp/diff2 | fold -sw " w);
102 printf reset;
103 }
104 function join2(a, sep, b) {
105 return a (a&&b? sep : "") b;
106 }
Imprint / Impressum