Add tweeirc - localbin - leot's localbin (~/bin)
(HTM) hg clone https://bitbucket.org/iamleot/localbin
(DIR) Log
(DIR) Files
(DIR) Refs
---
(DIR) changeset aee2dc565dc4cb7a17a58b18948aec5060635437
(DIR) parent e30617ca66c3de2a208222ab28d1b13eb7f3f187
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sat, 27 Jul 2019 22:56:24
Add tweeirc
Twitter -> IRC gateway
Diffstat:
tweeirc | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 118 insertions(+), 0 deletions(-)
---
diff -r e30617ca66c3 -r aee2dc565dc4 tweeirc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tweeirc Sat Jul 27 22:56:24 2019 +0200
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+#
+# Given an IRC server, port and channel via TWEEIRC_* environment
+# variables and a list of users as arguments fetch all Twitter user
+# timelines, parse them via tscrape and share each tweet on the IRC
+# channel with the proper nick.
+#
+
+
+tweeirc_dir=${TWEEIRC_DIR:="${HOME}/.tweeirc"}
+tweeirc_channel=${TWEEIRC_CHANNEL:="#twitter"}
+tweeirc_server=${TWEEIRC_SERVER:="localhost"}
+tweeirc_port=${TWEEIRC_PORT:="6667"}
+
+base="https://twitter.com"
+
+
+#
+# Print usage message and exit
+#
+usage()
+{
+
+ echo "usage: tweeirc [-o] username ..."
+ exit 1
+}
+
+
+#
+# Given a username print to standard output all tweets newer than the id stored
+# in fileid.
+#
+tweets()
+{
+ username=$1
+
+ curl -m 10 -H 'Accept-Language: en' -gs "${base}/${username}" | tscrape | sort -n |
+ awk -F '\t' \
+ -v fileid="${tweeirc_dir}/${username}" '
+ BEGIN {
+ getline min_id < fileid
+ min_id = int(min_id)
+ max_id = 0
+ }
+
+ {
+ timestamp = $1
+ username = $2
+ text = $4
+ item_id = $5
+ item_username = $6
+ item_retweetid = $8
+ item_pinned = $9
+ id = item_retweetid ? item_retweetid : item_id
+ id = int(id)
+ max_id = id > max_id ? id : max_id
+ }
+
+ (id > min_id) {
+ if (username != item_username)
+ printf("RT @%s ", item_username)
+ printf("%s\n", text)
+ }
+
+ END {
+ if (max_id > min_id)
+ print max_id > fileid
+ }
+ '
+}
+
+
+#
+# Given a username, pipe to the IRC channel
+#
+irc()
+{
+ username=$1
+
+ awk -v nc="nc ${tweeirc_server} ${tweeirc_port} > /dev/null" \
+ -v channel="${tweeirc_channel}" \
+ -v username="${username}" '
+ BEGIN {
+ print "NICK " username | nc
+ print "USER " username " * * *" | nc
+ fflush(nc)
+ }
+
+ {
+ print "PRIVMSG " channel " :" $0 | nc
+ fflush(nc)
+ }
+
+ END {
+ print "QUIT" | nc
+ close(nc)
+ }
+ '
+}
+
+
+while getopts o f; do
+ case $f in
+ o) irc() { awk -v u="$1" '{ printf("<%s> %s\n", u, $0) }'; } ;;
+ \?) usage ;;
+ esac
+done
+shift $((OPTIND - 1))
+if [ $# -lt 1 ]; then
+ usage
+fi
+
+mkdir -p "${tweeirc_dir}" || exit 1
+
+for u in "$@"; do
+ tweets "$u" | irc "$u"
+done