Created
January 28, 2016 15:12
-
-
Save SonoSooS/85bae93bc3534cca239b to your computer and use it in GitHub Desktop.
GBATemp notifier written in bash
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
#!/bin/bash | |
# =================================== | |
# GBATemp notifier bash script | |
# | |
# created by MarcusD | |
# Copyright $(date +"%Y") | |
# | |
# :P | |
# =================================== | |
function writecfg { | |
echo $'MSGTIMEOUT='$MSGTIMEOUT$'\nSESSID='$SESSID$'\nTOKN='$TOKN >~/.gbanotif | |
} | |
function readcfg { | |
. ~/.gbanotif | |
} | |
function defaultcmd { | |
echo $'RET=$(notifcount) && [ $RET != "0" ] && notiflist | reformat | limitno $RET | sendnotif\nRET=$(msgcount) && [ $RET != "0" ] && msglist | reformat | reformat_msg | limitno $RET | sendnotif\n\n# DO NOT REMOVE!\n! test' >~/.gbacmd | |
} | |
function reformat { | |
grep -oP '(?<="templateHtml":").*(?=","css")' | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/<div.*>//g' | sed 's/<ol.*>//g' | sed 's/\\\//\//g' | sed 's/\\"/\"/g' | sed 's/<\/div>//g' | sed 's/<\/ol>//g' | sed 's/<li.*>/<li>/g' | sed 's/<abbr.*>\(.*\)<\/abbr>/<i>\1<\/i>/g' | sed 's/href="/href="https\:\/\/gbatemp\.net\//g' | sed 's/<a.*><img.*\/><\/a>//g' | sed 's/<span[^>]*>//g' | sed 's/<\/span>//g' | sed 's/<h3>//g' | sed 's/<\/h3>//g' | sed 's/\t//g' | sed 's/ //g' | sed 's/<li>//g' | sed 's/<\/li>/|/g' | sed 's/<a[^>]*href="\([^"]*\)"[^>]*>/<a href="\1">/g' | tr '\n' $'\xb4' | sed 's/\xb4\+//g' | sed 's/<i>/\n\n<i>/g' | tr $'\xb4' '|' | sed 's/<\/i>/<\/i>|/g' | sed 's/||/|/g' | |
} | |
function reformat_msg { | |
sed 's/<h3[^>]*>//g' | sed 's/\(Last.*\),$/\n\1/g' | sed 's/<\/a>With:/<\/a>\n\nWith:/g' | |
} | |
function tokenfresh_raw { | |
curl -XPOST 'http://gbatemp.net/login/csrf-token-refresh' -H "Cookie: xf_session=$SESSID" --data "&_xfRequestUri=/&_xfNoRedirect=1&_xfToken=$TOKN&_xfResponseType=json" | |
} | |
function livecount_raw { | |
curl -XPOST -H "Cookie: xf_session=$SESSID" -d "_xfRequestUri=%2F&_xfNoRedirect=1&_xfToken=$TOKN&_xfResponseType=json" "https://gbatemp.net/index.php?liveupdate" | |
} | |
function notiflist_raw { | |
curl "https://gbatemp.net/account/alerts-popup?_xfRequestUri=%2F&_xfNoRedirect=1&_xfToken=$TOKN&_xfResponseType=json" -H "Cookie: xf_session=$SESSID" | |
} | |
function msglist_raw { | |
curl "http://gbatemp.net/conversations/popup?_xfRequestUri=%2F&_xfNoRedirect=1&_xfToken=$TOKN&_xfResponseType=json" -H "Cookie: xf_session=$SESSID" | |
} | |
function notifcount { | |
RET=$(livecount_raw) | |
[[ $? -ne 0 ]] && return 1 | |
RET=$(echo $RET | sed 's/.*_visitor_alertsUnread":"\([0-9]*\).*"/\1/g' | rev | cut -b 2- | rev) | |
#[[ $? -ne 0 ]] && return 1 | |
[[ $RET =~ ^[0-9]+$ ]] || return 1 | |
echo $RET | |
return 0 | |
} | |
function msgcount { | |
RET=$(livecount_raw) | |
[[ $? -ne 0 ]] && return 1 | |
RET=$(echo $RET | sed 's/.*_visitor_conversationsUnread":"\([0-9]*\).*"/\1/g' | rev | cut -b 2- | rev) | |
#[[ $? -ne 0 ]] && return 1 | |
[[ $RET =~ ^[0-9]+$ ]] || return 1 | |
echo $RET | |
return 0 | |
} | |
function notiflist { | |
RET=$(notiflist_raw) | |
[[ $? -ne 0 ]] && return 1 | |
echo $RET | testerr && return 1 | |
echo $RET | |
return 0 | |
} | |
function msglist { | |
RET=$(msglist_raw) | |
[[ $? -ne 0 ]] && return 1 | |
echo $RET | testerr && return 1 | |
echo $RET | |
return 0 | |
} | |
function limitno { | |
tr '\n' $'\x1b' | tr '|' '\n' | head -n $1 | tr '\n' '|' | tr $'\x1b' '\n' | |
} | |
function sendnotif { | |
tr '|' '\0' | xargs -0 -n1 notify-send -t $MSGTIMEOUT -i firefox "GBATemp"; | |
} | |
function xfsessgen { | |
curl 'http://gbatemp.net/dynamic/' -D - -o /dev/null | grep -oP "xf_session=\K[^;]+" | |
} | |
function xftokengen { | |
curl 'http://gbatemp.net/dynamic/' -H "Cookie: xf_session=$SESSID" | grep -oP '_csrfToken: "\K[^"]+' | |
} | |
function xflogincookie { | |
curl -XPOST -D - -o /dev/null 'http://gbatemp.net/login/login' -H "Cookie: xf_session=$SESSID" --data "login=$1®ister=0&password=$2&cookie_check=1&_xfToken=&redirect=%2F" | grep -oP "xf_session=\K[^;]+" | |
} | |
function xflogoff { | |
curl "http://gbatemp.net/logout/?_xfToken=$TOKN" -H "Cookie: xf_session=$SESSID" | |
} | |
function testerr { | |
grep '"errorOverlayType":' 2>&1 > /dev/null | |
} | |
# ========================== # | |
# ===== [ BEGIN HERE ] ===== # | |
# ========================== # | |
COMD='' | |
if readcfg; then | |
case "$1" in | |
loop) | |
COMD="$2" | |
;; | |
logoff) | |
xflogoff | |
rm ~/.gbanotif | |
exit 0 | |
;; | |
api) | |
return 0 | |
echo This function is for debugging only! Source it if you want to use this! | |
exit 1 | |
;; | |
*) | |
echo Ignored invalid parameter $1 | |
;; | |
esac | |
else | |
MSGTIMEOUT=300000 | |
SESSID=$(xfsessgen) || ( | |
zenity --error --title='xf_session' --text=$'Can\'t get a new session ID from GBATemp!' | |
exit | |
) | |
zenity --info --text=$'This is the first time you launch this,\nso we\'ll need to login into your GBATemp account.\n\nYou\'ll be presented with an error message \(because I\'m lazy :P\),\njust click on yes if you want to continue :P' --ellipsize --title='GBATemp notifier' || exit | |
fi | |
while ! RET=$(notifcount); do | |
zenity --question --icon-name=error --text=$'Error occurred while trying to login.\nDo you wan\'t to try to login again?' --title='GBATemp notifier' || exit | |
IFS='|' read usr pass < <( zenity --username --password) || continue | |
[ -z "${usr// }" -o -z "${pass// }" ] && if zenity --error --text='Invalid login info!'; then continue; else exit; fi | |
usr=$(echo -n "$usr" | perl -MURI::Escape -ne 'print uri_escape($_)') | |
pass=$(echo -n "$pass" | perl -MURI::Escape -ne 'print uri_escape($_)') | |
SESSID=$(xflogincookie $usr $pass) || ( zenity --error --text="$SESSID" --title='SESSID'; continue; ) | |
TOKN=$(xftokengen) || ( zenity --error --text="$SESSID" --title='xfToken'; continue; ) | |
usr='' | |
pass='' | |
done | |
writecfg | |
. ~/.gbacmd || ( echo $'You can edit the payload at "~/.gbacmd"'; defaultcmd; ) && . ~/.gbacmd | |
[ "x$COMD" != "x" ] && ( | |
while sleep $COMD; do | |
. ~/.gbacmd || ( | |
echo Error occurred while trying to run the payload: $? | |
break | |
) | |
done | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment