Skip to content

Instantly share code, notes, and snippets.

@rdmarsh
Last active February 8, 2018 04:41
Show Gist options
  • Save rdmarsh/fdba87bd6873891a1c4f1fd7d73a28dd to your computer and use it in GitHub Desktop.
Save rdmarsh/fdba87bd6873891a1c4f1fd7d73a28dd to your computer and use it in GitHub Desktop.
makes a list of twitter accounts that would be useful (un)following from a third account
#!/usr/bin/env bash
#
# Makes list of twitter accounts that should be (un)followed. To use; Create a
# twitter list, add primary users to it and set primarylist variable to list
# name You will get three lists
#
# * groupies: these accounts follow you, but you don't follow them
#
# * to follow: these accounts have a relation with your primary accounts, but
# you don't follow them
#
# * to unfollow: these accounts don't have a relation with your primary
# accounts, but you do follow them
#
set -eu
set -o pipefail
_file_exists() {
#check a file exists
local _file="${1:?'file name not supplied'}"
if [[ -f "${_file}" ]] ; then
return 0
else
return 1
fi
}
_file_empty() {
#check a file is empty
local _file="${1:?'file name not supplied'}"
if _file_exists "${_file}" && [[ ! -s "${_file}" ]] ; then
return 0
else
return 1
fi
}
_file_not_empty() {
#check a file has contents
local _file="${1:?'file name not supplied'}"
if _file_exists "${_file}" && [[ -s "${_file}" ]] ; then
return 0
else
return 1
fi
}
_can_read_file() {
#check if we can read the contents of a file
local _file="${1:?'file name not supplied'}"
if _file_exists "${_file}" && [[ -r "${_file}" ]] ; then
return 0
else
return 1
fi
}
_file_younger_than() {
local _time="${1:?'age in mins not supplied'}"
local _file="${2:?'file name not supplied'}"
if _file_exists "${_file}" ; then
#find file age in minutes
local _fileage="$((($(/bin/date +%s) - $(/usr/bin/stat -c '%Y' "${_file}"))/60))"
if (( ${_fileage} < ${_time} )) ; then
return 0
else
return 1
fi
else
return 1
fi
}
_file_older_than() {
local _time="${1:?'age in mins not supplied'}"
local _file="${2:?'file name not supplied'}"
if _file_exists "${_file}" ; then
#find file age in minutes
local _fileage="$((($(/bin/date +%s) - $(/usr/bin/stat -c '%Y' "${_file}"))/60))"
if (( ${_fileage} > ${_time} )) ; then
return 0
else
return 1
fi
else
return 1
fi
}
_get_rel() {
local _id="${1}"
local _rel="${2}"
local _relage="$(( ( RANDOM % 10 ) + ${3}))"
local _list="${datdir}/${_id}.${_rel}.list"
echo "checking ${_rel} for ${_id}"
if ! _file_exists "${_list}" || _file_empty "${_list}" || _file_older_than ${_relage} "${_list}" ; then
echo "getting ${_rel} for ${_id}"
if t ${_rel} "${_id}" 2> "${tmperr}" | grep -v "${_id}" | sort -u > "${_list}" ; then
return 0
else
local _sleep=$(( $(awk '$11 == "seconds." {print $10}' "${tmperr}") + 1 ))
echo "sleeping ${_sleep}..."
sleep "${_sleep}"
return 1
fi
else
return 0
fi
}
main() {
local _id
local _rel
local _list="${datdir}/${primarylist}.list"
local _file
mkdir -p "${datdir}"
#collect our primary accounts
if ! _file_exists "${_list}" || _file_empty "${_list}" || _file_older_than ${relage} "${_list}" ; then
t list members "${primarylist}" | sort -u > "${_list}"
fi
for _id in $( < ${_list} ) ${user} ; do
echo
for _rel in friends groupies followers leaders followings ; do
while ! _get_rel "${_id}" "${_rel}" ${relage} ; do
echo "retrying..."
done
done
done
#collect and sort all ids related to primary accounts
#wipe and rebuild $allids
cp "${_list}" "${allids}"
for _id in $( < ${_list} ) ; do
sort -u ${datdir}/${_id}.*.list >> "${allids}"
done
sort -u "${allids}" -o "${allids}"
#generate to follow list
comm -23 "${allids}" "${datdir}/${user}.followings.list" | sort -u > "${tofollow}"
#generate to unfollow list
comm -13 "${allids}" "${datdir}/${user}.leaders.list" | sort -u > "${tounfollow}"
_file="${datdir}/${user}.groupies.list"
echo
echo 'groupies:'
wc -l "${_file}"
echo "for id in \$(shuf -n15 ${_file}) ; do t open \${id} ; done"
_file="${tofollow}"
echo
echo 'to follow:'
wc -l "${_file}"
echo "for id in \$(shuf -n15 ${_file}) ; do t open \${id} ; done"
_file="${tounfollow}"
echo
echo 'to unfollow:'
wc -l "${_file}"
echo "for id in \$(shuf -n15 ${_file}) ; do t open \${id} ; done"
echo
}
readonly user="$(t whoami | awk '$1 == "Screen" {print $3}' | tr -d '@')"
readonly datdir="${HOME}/Dropbox/tmp/twitter-basic"
readonly primarylist='hosts'
readonly relage=30
readonly allids="${datdir}/all_ids.dat"
readonly tofollow="${datdir}/tofollow.dat"
readonly tounfollow="${datdir}/tounfollow.dat"
readonly tmperr="$(/bin/mktemp)"
trap "rm -f ${tmperr}" EXIT
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment