tweets.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
       ---
       weets.dcgi
       ---
            1 #!/bin/sh
            2 
            3 . ../common/config.sh
            4 
            5 CGI="${CGI_BASEDIR}/twitter/tweets.dcgi"
            6 
            7 base="https://twitter.com"
            8 mbase="https://mobile.twitter.com"
            9 args="$2"
           10 
           11 case "${args}" in
           12 ${base}/*)
           13         args=${args#${base}/}
           14         ;;
           15 ${mbase}/*)
           16         args=${args#${mbase}/}
           17         ;;
           18 esac
           19 
           20 case "${args}" in
           21 */status/*)
           22         IFS=/ set -- "${args}"
           23         set -- $*
           24         username="$1"
           25         id="$3"
           26         ;;
           27 */*)
           28         IFS=/ set -- "${args}"
           29         set -- $*
           30         username="$1"
           31         id="$2"
           32         ;;
           33 *)
           34         username="${args}"
           35         id=""
           36         ;;
           37 esac
           38 
           39 
           40 #
           41 # Add HTML header to make a response tscrape-able
           42 #
           43 header()
           44 {
           45         echo "<div class=\"user-actions\""
           46         echo "     data-screen-name=\"${username}\""
           47         echo "     data-name=\"${name}\">"
           48         echo "<li class=\"js-stream-item\">"
           49 }
           50 
           51 
           52 #
           53 # Fetch a Twitter URL
           54 #
           55 fetch()
           56 {
           57         url=$1
           58         curl -Lgs -- "${url}"
           59 }
           60 
           61 
           62 #
           63 # Given a username and optional max id tscrape the next 20 tweets
           64 #
           65 timeline()
           66 {
           67         username=$1
           68         name=""
           69         maxid=$2
           70 
           71         url="${base}/i/profiles/show/${username}/timeline/tweets?lang=en"
           72         if [ "${maxid}" ]; then
           73                 url="${url}&max_position=${maxid}"
           74         fi
           75         {
           76                 header;
           77                 fetch "${url}" | jq -r '.items_html';
           78         } | tscrape
           79 }
           80 
           81 
           82 #
           83 # Given a username and an id of a status, tscrape the replies
           84 #
           85 status()
           86 {
           87         username=$1
           88         name=""
           89         id=$2
           90 
           91         url="${base}/${username}/status/${id}"
           92         {
           93                 header;
           94                 fetch "${url}";
           95         } | tscrape
           96 }
           97 
           98 
           99 #
          100 # Format the tscrape output in gph
          101 #
          102 tscrape_gph()
          103 {
          104         awk -v base="${mbase}" -v cgi="${CGI}" -F "\t" \
          105 '
          106         function print_tweettext(text)
          107         {
          108                 cmd = "par -d 0 -B=. -w 72"
          109                 cmd = cmd " | sed -e \"s/^/t/\""
          110                 cmd = cmd " | sed -E -e \"s;^(t)(.*)(https?://[^ ]+)(.*)$;[h|\\2\\3\\4|URL:\\3|server|port];\""
          111 
          112                 print text | cmd
          113                 close(cmd)
          114         }
          115 
          116         function print_tweet(t)
          117         {
          118                 if (t["item_retweet_id"])
          119                         id = t["item_retweet_id"]
          120                 else
          121                         id = t["item_id"]
          122 
          123                 desc = sprintf("%s@%s   %s",
          124                     t["item_fullname"] ? t["item_fullname"] "   " : "",
          125                     t["item_username"] ? t["item_username"] : t["username"],
          126                     strftime("%Y-%m-%d %H:%M", t["timestamp"]))
          127                 gsub("\\|", "\\|", desc)
          128 
          129                 printf("[1|%s|%s?%s/status/%s|server|port]\n",
          130                     desc,
          131                     cgi,
          132                     t["item_username"],
          133                     id)
          134                 print_tweettext(t["text"])
          135         }
          136 
          137         {
          138                 tweet["timestamp"] = $1
          139                 tweet["username"] = $2
          140                 tweet["fullname"] = $3
          141                 tweet["text"] = $4
          142                 tweet["item_id"] = $5
          143                 tweet["item_username"] = $6
          144                 tweet["item_fullname"] = $7
          145                 tweet["item_retweetid"] = $8
          146 
          147                 print_tweet(tweet)
          148                 printf("\n")
          149         }
          150 
          151         END {
          152                 printf("[1|<< Older|%s?%s/%s|server|port]\n",
          153                     cgi, tweet["username"], tweet["item_id"] - 1)
          154         }
          155 '
          156 }
          157 
          158 
          159 echo ""
          160 echo "Twitter   (@${username})"
          161 echo ""
          162 if [ "$2" = "status" ]; then
          163         status "${username}" "${id}"
          164 else
          165         timeline "${username}" "${id}"
          166 fi | tscrape_gph