Created
February 3, 2016 03:44
-
-
Save abargnesi/2fa23ae5802f3330c349 to your computer and use it in GitHub Desktop.
Receive gitter room chat log in chronological order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Retrieves chat messages in chronological order for a Gitter room. | |
| # Each chat message is outputted per line and is formatted like: | |
| # | |
| # (sent) username: text | |
| # | |
| if [ $# -ne 2 ]; then | |
| echo "Retrieves gitter room's chat log in chronological order." 1>&2 | |
| echo "Supply your authorization token and room name" 1>&2 | |
| echo "" 1>&2 | |
| echo "usage: recv_gitter.sh AUTH_TOKEN ROOM_NAME" 1>&2 | |
| echo "example: recv_gitter.sh afe48192... your/chat" 1>&2 | |
| exit 1 | |
| fi | |
| hash egrep > /dev/null 2>&1 | |
| if [ $? -ne 0 ]; then | |
| echo "error: you will need the egrep executable installed." 1>&2 | |
| fi | |
| hash curl > /dev/null 2>&1 | |
| if [ $? -ne 0 ]; then | |
| echo "error: you will need the curl executable installed." 1>&2 | |
| echo " see: https://curl.haxx.se/" | |
| fi | |
| hash jq > /dev/null 2>&1 | |
| if [ $? -ne 0 ]; then | |
| echo "error: you will need the jq executable installed." 1>&2 | |
| echo " see: https://stedolan.github.io/jq/" | |
| fi | |
| AUTH_TOKEN="$1" | |
| ROOM_NAME="$2" | |
| echo "Retrieving log for $ROOM_NAME" 1>&2 | |
| ROOM_ID=$(curl -s \ | |
| -H "Accept: application/json" \ | |
| -H "Authorization: Bearer $AUTH_TOKEN" \ | |
| "https://api.gitter.im/v1/rooms" | \ | |
| jq -r '.[] | [.id, .name] | @csv' | \ | |
| grep "$ROOM_NAME" | \ | |
| egrep -o '"[0-9a-f]+"' | tr -d '"') | |
| echo "The room id is $ROOM_ID" 1>&2 | |
| before_messages() { | |
| ROOM_ID="$1" | |
| if [ -n "$2" ]; then | |
| BEFORE_ID_PARAM="&beforeId=$2" | |
| else | |
| BEFORE_ID_PARAM="" | |
| fi | |
| MESSAGES=$(curl -s \ | |
| -H "Accept: application/json" \ | |
| -H "Authorization: Bearer $AUTH_TOKEN" \ | |
| "https://api.gitter.im/v1/rooms/$ROOM_ID/chatMessages?limit=200$BEFORE_ID_PARAM") | |
| echo "$MESSAGES" | |
| return 0 | |
| } | |
| at_message() { | |
| ROOM_ID="$1" | |
| MESSAGE_ID="$2" | |
| MESSAGE=$(curl -s \ | |
| -H "Accept: application/json" \ | |
| -H "Authorization: Bearer $AUTH_TOKEN" \ | |
| "https://api.gitter.im/v1/rooms/$ROOM_ID/chatMessages/$MESSAGE_ID") | |
| echo "$MESSAGE" | |
| return 0 | |
| } | |
| after_messages() { | |
| ROOM_ID="$1" | |
| if [ -n "$2" ]; then | |
| AFTER_ID_PARAM="&afterId=$2" | |
| else | |
| AFTER_ID_PARAM="" | |
| fi | |
| MESSAGES=$(curl -s \ | |
| -H "Accept: application/json" \ | |
| -H "Authorization: Bearer $AUTH_TOKEN" \ | |
| "https://api.gitter.im/v1/rooms/$ROOM_ID/chatMessages?limit=200$AFTER_ID_PARAM") | |
| echo "$MESSAGES" | |
| return 0 | |
| } | |
| find_first_msg_id() { | |
| ROOM_ID="$1" | |
| MESSAGES=$(before_messages $ROOM_ID) | |
| MESSAGE_COUNT=$(echo $MESSAGES | jq 'length') | |
| while [ $MESSAGE_COUNT -gt 0 ]; do | |
| FIRST_ID=$(echo $MESSAGES | jq '.[0].id' | tr -d '"') | |
| MESSAGES=$(before_messages $ROOM_ID $FIRST_ID) | |
| MESSAGE_COUNT=$(echo $MESSAGES | jq 'length') | |
| done | |
| echo "$FIRST_ID" | |
| return 0 | |
| } | |
| FIRST_MSG_ID=$(find_first_msg_id $ROOM_ID) | |
| echo "Found first message id: $FIRST_MSG_ID" 1>&2 | |
| # Log first message | |
| echo $(at_message $ROOM_ID $FIRST_MSG_ID | jq '"(\(.sent)) \(.fromUser.username): \(.text)"') | sed 's#^"\(.*\)"$#\1#g' | |
| # Log each block after, until empty | |
| MESSAGES=$(after_messages $ROOM_ID $FIRST_MSG_ID) | |
| MESSAGE_COUNT=$(echo $MESSAGES | jq 'length') | |
| echo "$MESSAGES" | jq '.[] | "(\(.sent)) \(.fromUser.username): \(.text)"' | sed 's#^"\(.*\)"$#\1#g' | |
| while [ $MESSAGE_COUNT -gt 0 ]; do | |
| LAST_ID=$(echo $MESSAGES | jq '. | .[length - 1].id' | tr -d '"') | |
| MESSAGES=$(after_messages $ROOM_ID $LAST_ID) | |
| MESSAGE_COUNT=$(echo $MESSAGES | jq 'length') | |
| echo "$MESSAGES" | jq '.[] | "(\(.sent)) \(.fromUser.username): \(.text)"' | sed 's#^"\(.*\)"$#\1#g' | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment