URI: 
       Add ifdbgames example. Thanks pazz0! - brcon2024-hackathons - Bitreichcon 2024 Hackathons
  HTML git clone git://bitreich.org/brcon2024-hackathons git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/brcon2024-hackathons
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR Submodules
       ---
   DIR commit 657b359aba0a48bc973a0cffe152a941297f6543
   DIR parent ad0bd2f3d8d0c6d5e4aa3209cbeb81d4d9751a1a
  HTML Author: Christoph Lohmann <20h@r-36.net>
       Date:   Sun,  4 Aug 2024 20:47:53 +0200
       
       Add ifdbgames example. Thanks pazz0!
       
       Diffstat:
         M gopher/README.md                    |       3 +++
         A gopher/ifdbgames.sh                 |     349 +++++++++++++++++++++++++++++++
       
       2 files changed, 352 insertions(+), 0 deletions(-)
       ---
   DIR diff --git a/gopher/README.md b/gopher/README.md
       @@ -42,6 +42,9 @@ fiction for gopher.
        
        Is this enough?
        
       +        https://ifarchive.org/indexes/if-archive/info/ifdb/
       +        gopher://gopher.0xfa.de/0/pazz0/p/ifdbgames.sh
       +
        ## Pharos Internet Archive Interface
        
        Help pi31415 and extend his Internet Archive Interface for Gopher:
   DIR diff --git a/gopher/ifdbgames.sh b/gopher/ifdbgames.sh
       @@ -0,0 +1,349 @@
       +#!/bin/sh
       +getrawdata() {
       +        printf "getrawdata: start\n" 1>&2
       +        sqlite3 "$1" <<-EOF
       +                .separator "\x1f" "\x1e"
       +
       +                with ratings as (
       +                        select gameid, avg(rating) rawavg, round(avg(rating) * 2) / 2 stars, count(*) cnt from reviews where special isnull group by gameid
       +                )
       +                select
       +                        games.id,
       +                        games.title,
       +                        games.sort_title,
       +                        games.author,
       +                        games.sort_author,
       +                        games.tags,
       +                        iif(time(games.published) = '00:00:00',strftime('%Y', games.published), date(games.published)),
       +                        games.license,
       +                        games.desc,
       +                        games.genre,
       +                        games.forgiveness,
       +                        games.language,
       +                        games.website,
       +                        games.downloadnotes,
       +
       +                        gamelinks.title,
       +                        gamelinks.desc,
       +                        gamelinks.url,
       +
       +                        filetypes.externid,
       +                        filetypes.fmtname,
       +                        filetypes.desc,
       +
       +                        extreviews.url,
       +                        extreviews.sourcename,
       +                        extreviews.sourceurl,
       +                        reviews.summary,
       +                        reviews.review,
       +                        reviews.rating,
       +
       +                        ratings.stars,
       +                        ratings.cnt
       +                from games
       +                left outer join gamelinks on gamelinks.gameid = games.id
       +                left outer join filetypes on filetypes.id = gamelinks.fmtid
       +                left outer join extreviews on extreviews.gameid = games.id
       +                left outer join reviews on reviews.id = extreviews.reviewid
       +                left outer join ratings on ratings.gameid = games.id
       +                order by games.id, gamelinks.displayorder, extreviews.displayorder
       +        EOF
       +        printf "getrawdata: end\n" 1>&2
       +}
       +
       +# Sadly OpenBSD's awk has some problems with some multibyte characters in the
       +# dataset Maybe the encoding in the database is wonky. I don't know. Let's
       +# ignore it! And just use gawk. I want to see something in the end. I doesn't
       +# have to be correct. :)
       +getrawdata "$1" | \
       +gawk -v"FS=\x1f" -v"RS=\x1e" '
       +function removehtmltags(s) {
       +        gsub("<[^>]+>", "", s)
       +        return s
       +}
       +# TODO Do it, future_pazz0!
       +function resolvehtmlentities(s) {
       +        return s
       +}
       +function trim(s) {
       +        gsub("^[         ]+", "", s)
       +        gsub("[         ]+$", "", s)
       +        return s
       +}
       +function foldinto(input, w, indentstr, file,
       +        p, t, l, linel, line, n) {
       +        l = length(input)
       +        p = 1
       +        line = indentstr 
       +        n = 0
       +        while (p <= l) {
       +                for (t = p; t <= l && !index(" \t\n", substr(input, t, 1)); t++);
       +
       +                # Doesnt fit in the current line
       +                if (length(line) + t - p + 1 > w) {
       +                        print line >> file
       +                        line = indentstr 
       +                        n++
       +                }
       +
       +                # Doesnt fit anyways
       +                if (t - p > w) {
       +                        print line >> file
       +                } else {
       +                        if (line && line != indentstr)
       +                                line = line " "
       +                        line = line substr(input, p, t - p)
       +                }
       +
       +                for (; t <= l && index(" \t\n", substr(input, t, 1)); t++) {
       +                        if (substr(input, t, 1) == "\n" && substr(input, t - 1, 1) == "\n") {
       +                                print line >> file
       +                                line = indentstr 
       +                                print indentstr >> file
       +                                n++
       +                        }
       +                }
       +                p = t
       +        }
       +        if (line)
       +                print line >> file
       +}
       +function flushdescription2(file,
       +        i) {
       +        cmd = "groff -Tutf8 -ms > " file
       +
       +        print ".nr LL 80n" | cmd
       +        print ".nr LT 80n" | cmd
       +
       +        print ".SH\n" data[2] | cmd
       +        print ".IP Author(s)\n" data[4] | cmd
       +        if (data[10])
       +                print ".IP Genre\n" data[10] | cmd
       +        if (data[13])
       +                print ".IP Website\n" data[13] | cmd
       +        if (data[12])
       +                print ".IP Language\n" data[12] | cmd
       +        if (data[7])
       +                print ".IP \"First Publication Date\"\n" data[7] | cmd
       +        if (data[8])
       +                print ".IP License\n" data[8] | cmd
       +        if (data[11])
       +                print ".IP \"Forgiveness Rating\"\n" data[11] | cmd
       +        if (ratings["stars"])
       +                printf(".IP Rating\n%s (based on %s rating%s)\n", ratings["stars"], ratings["count"], ratings["count"] > 1 ? "s" : "") | cmd
       +
       +        if (data[9]) {
       +                print ".SH\nAbout the Story" | cmd
       +                gsub("\n\n", "\n.LP\n", data[9])
       +                print ".LP\n" data[9] | cmd
       +        }
       +
       +        if (reviews["length"]) {
       +                print ".SH\nEditorial Reviews" | cmd
       +                for (i = 1; i <= reviews["length"]; i++) {
       +                        print ".SH\n" reviews[i, "sourcename"] | cmd
       +                        print ".IP URL\n" reviews[i, "url"] | cmd
       +                        if (reviews[i, "summary"])
       +                                print ".SH\n" reviews[i, "summary"] | cmd
       +                        if (reviews[i, "review"])
       +                                print ".IP\n" reviews[i, "review"] | cmd
       +                }
       +        }
       +        if (links["length"]) {
       +                print ".SH\nExternal Links" | cmd
       +                if (data[14])
       +                        print ".LP\n" data[14] | cmd
       +                for (i = 1; i <= links["length"]; i++) {
       +                        print ".SH\n" links[i, "title"] | cmd
       +                        print ".IP URL\n" links[i, "url"] | cmd
       +                        if (links[i, "desc"])
       +                                print ".IP\n" links[i, "desc"] | cmd
       +                        if (links[i, "fmtdesc"]) {
       +                                print ".IP\n" links[i, "fmtdesc"] | cmd
       +                                print ".IP\n" links[i, "fmtdesclink"] | cmd
       +                        }
       +                }
       +        }
       +
       +        close(cmd)
       +}
       +function keyvalue(key, value, file, w) {
       +        if (length(key ": " value) <= w) {
       +                printf("%s: %s\n", key, value) >> file
       +                return
       +        }
       +        printf("%s:\n", key) >> file
       +        foldinto(sprintf("%s\n", value), w, "  ", file)
       +}
       +function flushdescription_(file,
       +        i, refs, temp) {
       +#        file = data[1] ".txt"
       +#        file = "/dev/stdout"
       +
       +        printf("") > file
       +        foldinto(sprintf("%s\n", data[2]), 80, "", file)
       +        printf("--------------------------------------------------------------------------------\n") >> file
       +
       +        keyvalue("Author(s)", data[4], file, 80)
       +#        foldinto(sprintf("Author(s): %s\n", data[4]), 80, " ", file)
       +        if (data[10])
       +                printf("Genre: %s\n", data[10]) >> file
       +        if (data[13])
       +                printf("Website: %s\n", data[13]) >> file
       +        if (data[12])
       +                printf("Language: %s\n", data[12]) >> file
       +        if (data[7])
       +                printf("First Publication Date: %s\n", data[7]) >> file
       +        if (data[8])
       +                printf("License: %s\n", data[8]) >> file
       +        if (data[11])
       +                printf("Forgiveness Rating: %s\n", data[11]) >> file
       +        if (ratings["stars"])
       +                printf("Rating: %s (based on %s rating%s)\n", ratings["stars"], ratings["count"], ratings["count"] > 1 ? "s" : "") >> file
       +
       +        if (data[9]) {
       +                printf("\n\nABOUT THE STORY\n\n") >> file
       +                foldinto(data[9], 80, "  ", file)
       +        }
       +
       +        if (links["length"]) {
       +                printf("\n\nEXTERNAL LINKS\n") >> file
       +                for (i = 1; i <= links["length"]; i++) {
       +                        printf("\n  %s <%s>\n", links[i, "title"], links[i, "url"]) >> file
       +                        if (links[i, "desc"])
       +                                foldinto(links[i, "desc"], 80, "    ", file)
       +                        if (links[i, "fmtdesc"]) {
       +                                temp = links[i, "fmtdesc"]
       +                                if (links[i, "fmtdesclink"]) {
       +                                        if (!refs["set", links[i, "fmtdesclink"]]) {
       +                                                refs["length"]++
       +                                                refs[refs["length"]] = links[i, "fmtdesclink"]
       +                                                refs["set", links[i, "fmtdesclink"]] = refs["length"]
       +                                        }
       +                                        temp = temp sprintf("[%s]", refs["set", links[i, "fmtdesclink"]])
       +                                }
       +                                foldinto(temp, 80, "    ", file)
       +                        }
       +                }
       +        }
       +
       +        if (reviews["length"]) {
       +                printf("\n\nEDITORIAL REVIEWS\n") >> file
       +                for (i = 1; i <= reviews["length"]; i++) {
       +                        printf("\n  %s\n", reviews[i, "sourcename"]) >> file
       +                        printf("  ") >> file
       +                        if (reviews[i, "summary"])
       +                                printf("%s ", reviews[i, "summary"]) >> file
       +                        printf("<%s>\n",  reviews[i, "url"]) >> file
       +                        if (reviews[i, "review"])
       +                                foldinto(reviews[i, "review"], 80, "  > ", file)
       +                }
       +        }
       +
       +        if (refs["length"]) {
       +                printf("\n\nREFERENCES\n\n") >> file
       +                for (i = 1; i <= refs["length"]; i++)
       +                        printf("  [%s] <%s>\n", i, refs[i]) >> file
       +        }
       +
       +        close(file)
       +}
       +function gphescape(s, f) {
       +        gsub("        ", " ", s)
       +        gsub("\n", " ", s)
       +        if (f)
       +                gsub("^\\[", "[|&", s) 
       +        else
       +                gsub("\\|", "\\|", s)
       +        return s
       +}
       +function gphitem(type, text, selector, server, port, file) {
       +        printf("[%c|%s|%s|%s|%s]\n", type, gphescape(text), gphescape(selector), server, port) >> file
       +}
       +function flushmenu(file) {
       +        printf("") > file
       +        gphitem("0", sprintf("About \"%s\"", data[2]), "./" data[1] ".txt", "server", "port", file)
       +        foldinto(sprintf("%s\n", data[2]), 80, "", file)
       +
       +        close(file)
       +}
       +function getlink(s,
       +        i) {
       +        if (!match(s, /href="[^"]*"/))
       +                return ""
       +        return substr(s, RSTART + length("href=\""), RLENGTH - length("href=\"") - 1) 
       +}
       +function flush(tuid) {
       +        print tuid > "/dev/stderr"
       +        flushdescription_(tuid ".txt")
       +#        flushmenu(tuid ".gph")
       +        printf("%s\t%s\n", tuid ".txt", data[2]) >> "index.txt"
       +}
       +BEGIN {
       +        printf("") > "index.txt"
       +}
       +tuid && $1 != tuid {
       +        flush(tuid)
       +
       +        delete links
       +        delete reviews
       +        delete ratings
       +        delete data
       +}
       +$1 != tuid {
       +        for (i = 1; i <= 14; i++) {
       +                $i = removehtmltags($i)
       +                $i = resolvehtmlentities($i)
       +                data[i] = $i
       +        }
       +        ratings["stars"] = $27
       +        ratings["count"] = $28
       +        tuid = $1
       +}
       +$17 && !links["url", $17] {
       +        links["length"]++
       +        links[links["length"], "title"] = removehtmltags($15)
       +        links[links["length"], "desc"] = removehtmltags($16)
       +        links[links["length"], "url"] = removehtmltags($17)
       +
       +        links[links["length"], "externid"] = removehtmltags($18)
       +        links[links["length"], "fmtname"] = removehtmltags($19)
       +        links[links["length"], "fmtdesc"] = removehtmltags($20)
       +        links[links["length"], "fmtdesclink"] = getlink($20)
       +
       +        links["url", $17]++
       +}
       +$21 && !reviews["url", $21] {
       +        reviews["length"]++
       +        reviews[reviews["length"], "url"] = removehtmltags($21)
       +        reviews[reviews["length"], "sourcename"] = removehtmltags($22)
       +        reviews[reviews["length"], "sourceurl"] = removehtmltags($23)
       +        reviews[reviews["length"], "summary"] = removehtmltags($24)
       +        reviews[reviews["length"], "review"] = removehtmltags($25)
       +        reviews[reviews["length"], "rating"] = removehtmltags($26)
       +
       +        reviews["url", $21]++
       +}
       +END {
       +        flush(tuid)
       +}
       +'
       +
       +sort -t '        ' -k2 index.txt | \
       +gawk -v"FS=\t" ' 
       +function gphescape(s, f) {
       +        gsub("        ", " ", s)
       +        gsub("\n", " ", s)
       +        if (f)
       +                gsub("^\\[", "[|&", s) 
       +        else
       +                gsub("\\|", "\\|", s)
       +        return s
       +}
       +function gphitem(type, text, selector, server, port, file) {
       +        printf("[%c|%s|%s|%s|%s]\n", type, gphescape(text), gphescape(selector), server, port) >> file
       +}
       +{
       +        gphitem("0", $2, "./" $1, "servere", "port", "index.gph")
       +}
       +'