Add a twitter DCGI based on tscrape - 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
---
(DIR) changeset cb9e0cd648b6fe2f6932f5330fb7a3837f1f4143
(DIR) parent fe8958404066226a51160fb9ec0d3c757696c218
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sun, 16 Dec 2018 02:45:12
Add a twitter DCGI based on tscrape
Diffstat:
twitter/tweets.dcgi | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 110 insertions(+), 0 deletions(-)
---
diff -r fe8958404066 -r cb9e0cd648b6 twitter/tweets.dcgi
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/twitter/tweets.dcgi Sun Dec 16 02:45:12 2018 +0100
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+. ../common/config.sh
+
+CGI="${CGI_BASEDIR}/twitter/tweets.dcgi"
+
+
+case "$2" in
+*/*)
+ IFS=/ set -- "$2"
+ set -- $*
+ username="$1"
+ maxid="$2"
+ ;;
+*)
+ username="$2"
+ maxid=""
+ ;;
+esac
+
+
+base="https://twitter.com"
+
+
+#
+# Given a username and optional max id tscrape the next 20 tweets
+#
+timeline()
+{
+ username=$1
+ name=""
+ maxid=$2
+
+ url="${base}/i/profiles/show/${username}/timeline/tweets?lang=en"
+ if [ "${maxid}" ]; then
+ url="${url}&max_position=${maxid}"
+ fi
+
+ {
+ echo "<div class=\"user-actions\"";
+ echo " data-screen-name=\"${username}\"";
+ echo " data-name=\"${name}\">";
+ curl -s "${url}" | jq -r '.items_html';
+ } | tscrape
+}
+
+
+#
+# Format the tscrape output in gph
+#
+tscrape_gph()
+{
+ awk -v base="${base}" -v cgi="${CGI}" -F "\t" \
+'
+ function print_fmt(text)
+ {
+ cmd = "fmt"
+ cmd = cmd " | sed -e \"s/^/t/\""
+
+ print text | cmd
+ close(cmd)
+ }
+
+ function print_tweet(t)
+ {
+ if (t["item_retweet_id"])
+ id = t["item_retweet_id"]
+ else
+ id = t["item_id"]
+
+ desc = sprintf("%s @%s %s",
+ t["item_username"],
+ t["item_fullname"],
+ strftime("%Y-%m-%d %H:%M", t["timestamp"]))
+ gsub("\\|", "\\|", desc)
+
+ printf("[h|%s|URL:%s/%s/status/%s|server|port]\n",
+ desc,
+ base,
+ t["item_username"],
+ id)
+ print_fmt(t["text"])
+ }
+
+ {
+ tweet["timestamp"] = $1
+ tweet["username"] = $2
+ tweet["fullname"] = $3
+ tweet["text"] = $4
+ tweet["item_id"] = $5
+ tweet["item_username"] = $6
+ tweet["item_fullname"] = $7
+ tweet["item_retweetid"] = $8
+
+ print_tweet(tweet)
+ printf("\n")
+ }
+
+ END {
+ printf("[1|<< Older|%s?%s/%s|server|port]\n",
+ cgi, tweet["username"], tweet["item_id"] - 1)
+ }
+'
+}
+
+
+echo ""
+echo "Twitter"
+echo ""
+timeline "${username}" "${maxid}" | tscrape_gph