Skip to content

Instantly share code, notes, and snippets.

@aaroncampbell
Last active November 3, 2017 21:56
Show Gist options
  • Save aaroncampbell/3e62627059c18d559d9cc3ce69630ec8 to your computer and use it in GitHub Desktop.
Save aaroncampbell/3e62627059c18d559d9cc3ce69630ec8 to your computer and use it in GitHub Desktop.
Creates a list with all twitter users linked from a given URL. Requires https://github.com/sferik/t and you must have already authorized your Twitter account using it.
#!/usr/bin/env bash
# Load in our options
while getopts "u:l:d:a" option
do
case ${option} in
u) url=${OPTARG};;
l) list_name=${OPTARG};;
d) list_description=${OPTARG};;
a) append=1;;
esac
done
if [ -z "${url}" ] || [ -z "${list_name}" ]; then
echo "Usage: $0 -u url -l list-name [-d list-description]"
echo "-or to append-"
echo "Usage: $0 -u url -l list-slug -a"
exit 1
fi
if [ -n "${append}" ] && [ ! -z "${list_description}" ]; then
echo "When appending to an existing list (-a) the list description is ignored"
echo "Press any key to continue (or CTRL+C to quit)"
read -n 1 -s
fi
# Grab the URL
# -f gives us empty data for failures
# -k lets all SSL certs work
# -L follows redirects
# -s is silent mode (no progress)
echo "Fetching URL..."
page="$(curl -f -k -L -s $url)"
echo "Done!"
if [ -z "$page" ]; then
echo "Unable to fetch url or no data on page"
exit 1
fi
twitter_users=()
pattern="https?://twitter.com/([^\"'/]+)"
for word in $page
do
[[ ${word} =~ $pattern ]]
if [[ ${BASH_REMATCH[1]} ]]
then
twitter_users[${#twitter_users[*]}]=$(echo ${BASH_REMATCH[1]} | tr '[:upper:]' '[:lower:]');
fi
done
if [ ${#twitter_users[@]} -eq 0 ]; then
echo "No possible Twitter users found"
exit 1
fi
# Only create a list if we're not appending to an existing one
if [ ! -n "${append}" ]; then
#
# Since `t list create` doesn't return the list slug, we have to get the lists before we add it
# Then get them again after and look for the new one
#
# Get current lists
lists=$(t lists -l)
IFS=$'\n' read -rd '' -a starting_lists <<<"${lists}"
id_pattern=" *([0-9]+)"
index=0
while [ "${index}" -lt "${#starting_lists[@]}" ]; do
[[ ${starting_lists[${index}]} =~ ${id_pattern} ]]
starting_lists[${index}]=${BASH_REMATCH[1]}
((index++))
done
# Create new list
echo "Creating Twitter list ${list_name}..."
list_created=$(t list create "${list_name}" "${list_description}")
echo "Done!"
# Get current lists
lists=$(t lists -l)
IFS=$'\n' read -rd '' -a ending_lists <<<"${lists}"
index=0
list=
# Loop through current lists to find the newly added one
while [ "${index}" -lt "${#ending_lists[@]}" ]; do
[[ ${ending_lists[${index}]} =~ ${id_pattern} ]]
start_index=0;
match=
# Loop through lists we grabbed before adding one, and ignore those
while [ "${start_index}" -lt "${#starting_lists[@]}" ]; do
if [ ${BASH_REMATCH[1]} -eq ${starting_lists[${start_index}]} ]; then
match=1
break
fi
((start_index++))
done
if [ -z "${match}" ]; then
slug_pattern="@[^ ]+ +([^ ]+)"
[[ ${ending_lists[${index}]} =~ ${slug_pattern} ]]
list=${BASH_REMATCH[1]}
break
fi;
((index++))
done
if [ -z "${list}" ]; then
echo "There was a problem creating the new list."
exit 1;
fi
else
list=${list_name}
fi
users_to_add=()
IFS=$'\n' read -rd '' -a existing_users <<<"$(t list members "${list}")"
# lowercase all existing users
index=0;
while [ ${index} -lt ${#existing_users[@]} ]; do
existing_users[$index]=$(tr '[:upper:]' '[:lower:]' <<< ${existing_users[$index]})
((index++))
done
for found_user in "${twitter_users[@]}"; do
skip=
for user_in_list in "${existing_users[@]}"; do
#echo "$(echo ${found_user} | tr '[:upper:]' '[:lower:]') == $( echo ${user_in_list}"
if [[ ${found_user} == ${user_in_list} ]]; then
#echo "User Found - $found_user"
skip=1;
break;
fi
done
[[ -n $skip ]] || users_to_add+=("$found_user")
done
if [ ${#users_to_add[@]} -eq 0 ]; then
echo "No new Twitter users found"
exit 1
fi
echo "Attempting to add ${#users_to_add[@]} possible Twitter users to ${list_name}..."
list_added=$(t list add "${list}" ${users_to_add[@]})
echo "Done!"
echo
# Echo out the first line of the `t list add` command, which is the success or failure information
while read -r line; do
echo "${line}"
break
done <<< "${list_added}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment