Source

sxsync / sxsync.sh

Full commit
#!/bin/bash

set -e

user=mrucci
sxname=(stackoverflow.com serverfault.com superuser.com)
userID=(133106 25062 16752)
nsx=${#sxname[*]}
sxiter="$(seq 0 $((nsx-1)))"

ffProfileDir=$(cat ${HOME}/.mozilla/firefox/profiles.ini | grep "Path=" | cut -d'=' -f2)
ffProfileDir="${HOME}/.mozilla/firefox/"$(basename $ffProfileDir)

cookiesDbOrig="$ffProfileDir/cookies.sqlite"
[ -f "$cookiesDbOrig" ] || { echo "Cookies database not found."; exit; }

# The copy is necessary to avoid database lock issues.
cookiesDb="cookies-copy.sqlite"
cp "$cookiesDbOrig" "$cookiesDb"

# Cookies table schema: moz_cookies (id, name, value, host, path,expiry, lastAccessed, isSecure, isHttpOnly)

query='SELECT host, name,value FROM moz_cookies WHERE name = "usr" OR name = "somusr" OR name = "user"'
cookies=$(sqlite3 -separator '=' "$cookiesDb" "$query" );
rm $cookiesDb;

for i in $sxiter; do
	cookie[$i]=$(echo "$cookies" | grep --max-count=1 ${sxname[$i]} | cut -d= -f2-)
	echo "Site: ${sxname[$i]} - Cookie: ${cookie[$i]}"
done

echo ""
echo "Rss feeds of your favorite tags:"

# Brutally parse the html of the preferences page in order to extract the favorite tags.
for i in $sxiter; do
	if [ ! -z ${cookie[$i]} ]; then
		prefhtml=$(wget --no-cookies --header "Cookie: ${cookie[$i]};" --output-document=- --quiet http://${sxname[$i]}/users/${userID[$i]}/$user?tab=prefs)
		interestingTags=$(echo "${prefhtml}" | grep interestingTags | awk 'BEGIN {RS="<";FS=">";} {print $2};' | grep -v "^\s*$")
		rss[$i]="http://${sxname[$i]}/feeds/tag/"
		for tag in $interestingTags; do
			rss[$i]="${rss[$i]}+or+$tag"
		done
		echo "${rss[$i]}"
	fi
done

# Set to 1 if you use newsbeuter and want to automatically update 'urls' file
updateNewsbeuterFeeds=1
if [ $updateNewsbeuterFeeds -eq 1 ]; then
	sxpretag='~Favorite tags '
	#newsbeuterFeeds=urls
	newsbeuterFeeds=${HOME}/.newsbeuter/urls
	for i in $sxiter; do
		tag=${sxpretag}${sxname[$i]}
		if [ ! -z ${cookie[$i]} ]; then
			if grep -q "$tag" "$newsbeuterFeeds"; then
				sed -i".bak" "/$tag/ c ${rss[$i]} \"$tag\"" $newsbeuterFeeds
			else
				echo "${rss[$i]} \"$tag\"" >> $newsbeuterFeeds
			fi
		fi
	done
fi