mastodon.dcgi - gophercgis - Collection of gopher CGI/DCGI for geomyidae
 (HTM) hg clone https://bitbucket.org/iamleot/gophercgis
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mastodon.dcgi
       ---
            1 #!/bin/sh
            2 
            3 . ../common/config.sh
            4 
            5 CGI="${CGI_BASEDIR}/mastodon/mastodon.dcgi"
            6 
            7 
            8 url=$2
            9 
           10 
           11 #
           12 # Parse all mastodon toots and format them to gph
           13 #
           14 mastodon_gph()
           15 {
           16 
           17         awk -v cgi="${CGI}" \
           18 '
           19         function print_tootcontent(c)
           20         {
           21                 # Nonexistent proxy to block HTTP requests
           22                 cmd = "http_proxy=\"http://localhost:31283128\""
           23 
           24                 cmd = cmd " LANG=en_US.UTF-8"
           25                 cmd = cmd " w3m -T text/html"
           26                 cmd = cmd " -dump -no-cookie -cols 72"
           27                 cmd = cmd " -o display_link=false"
           28                 cmd = cmd " -o display_link_number=false"
           29                 cmd = cmd " -o display_image=false"
           30                 cmd = cmd " -o pseudo_inlines=false"
           31                 cmd = cmd " | sed -e \"s/^/t/\""
           32                 cmd = cmd " | sed -E -e \"s;^(t)(.*)(https?://[^ ]+)(.*)$;[h|\\2\\3\\4|URL:\\3|server|port];\""
           33 
           34                 print c | cmd
           35                 close(cmd)
           36         }
           37 
           38         function print_toot(t)
           39         {
           40                 desc = sprintf("%s   @%s   %s",
           41                     t["fullname"], t["username"], t["time"])
           42                 gsub("\\|", "\\|", desc)
           43 
           44                 printf("[1|%s|%s?%s|server|port]\n", desc, cgi, t["url"])
           45                 print_tootcontent(t["content"])
           46         }
           47 
           48         /^<link href=/ {
           49                 if (match($0, /<link href=.+ rel=.next.>$/)) {
           50                         next_link = substr($0, RSTART + 12, RLENGTH - 25)
           51                 }
           52 
           53                 if (match($0, /<link href=.+ rel=.prev.>$/)) {
           54                         prev_link = substr($0, RSTART + 12, RLENGTH - 25)
           55                 }
           56 
           57         }
           58 
           59         {
           60                 if (match($0, /class="status__relative-time/) ||
           61                     match($0, /class="detailed-status__datetime/)) {
           62                         match($0, />[^<]+<\/time>$/)
           63                         toot["time"] = substr($0, RSTART + 1, RLENGTH - 8)
           64                         match($0, /href="[^"]+"/)
           65                         toot["url"] = substr($0, RSTART + 6, RLENGTH - 7)
           66                 }
           67 
           68                 if (match($0, /class="status__display-name/) ||
           69                     match($0, /class="detailed-status__display-name/)) {
           70                         sub(/\/users\//, "/@")
           71                         match($0, /@[^"]+/)
           72                         toot["username"] = substr($0, RSTART + 1, RLENGTH - 1)
           73                 }
           74 
           75                 if (match($0, /class=.display-name__html/)) {
           76                         match($0, /.>[^<]+</)
           77                         toot["fullname"] = substr($0, RSTART + 2, RLENGTH - 3)
           78                 }
           79 
           80                 if (match($0, /class=.status__content/)) {
           81                         gsub(/&apos;/, "\\&#39;");
           82                         gsub(/&nbsp;<\/span><a class=.+ href=.#.>Show more<\/a>/, "");
           83                         toot["content"] = $0
           84                 }
           85 
           86                 if (toot["url"] && toot["content"]) {
           87                         print_toot(toot)
           88                         print "t"
           89 
           90                         toot["time"] = ""
           91                         toot["url"] = ""
           92                         toot["username"] = ""
           93                         toot["fullname"] = ""
           94                         toot["content"] = ""
           95                 }
           96         }
           97 
           98         END {
           99                 print "t"
          100 
          101                 if (next_link) {
          102                         printf("[1|>> Next|%s?%s|server|port]\n",
          103                                 cgi, next_link)
          104                 }
          105 
          106                 if (prev_link) {
          107                         printf("[1|<< Prev|%s?%s|server|port]\n",
          108                                 cgi, prev_link)
          109                 }
          110         }
          111 '
          112 }
          113 
          114 echo ""
          115 echo "Mastodon"
          116 echo ""
          117 curl -Lgs -- "${url}" | mastodon_gph