├── LICENSE.txt ├── README.md └── unfollows.sh /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Nyr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## unfollows 2 | Get email notifications about Twitter unfollowers 3 | 4 | 5 | ### Requeriments 6 | ##### cURL 7 | `apt-get install curl` (if you don't already have it). 8 | 9 | #### oauth_sign 10 | An OAuth library for the CLI. 11 | 12 | Get the [tarball](http://acme.com/software/oauth_sign/), compile and move the binary to wherever `unfollows.sh` is. 13 | 14 | #### jq 15 | A JSON parser for the CLI. 16 | 17 | Just get the [binary](http://stedolan.github.io/jq/download/) and you are ready to go. Place it along with `unfollows.sh` too. 18 | 19 | 20 | ### Installation 21 | Install the dependencies and edit the head of `unfollows.sh` with your configuration. You will need to [register](https://apps.twitter.com/app/new) a read-only app with Twitter and obtain one token and key. 22 | 23 | ### Known limitations 24 | - The script will work with Twitter accounts up to 5000 followers. 25 | - The script will not notify you about deleted or suspended accounts. 26 | - The `-a` flag of mail works on Debian but doesn't in Mac OS X, so you may need to remove it. 27 | -------------------------------------------------------------------------------- /unfollows.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Get email notifications about Twitter unfollowers 3 | # This script will work for Twitter accounts with up to 5000 followers 4 | 5 | # Set your configuration 6 | mail="you@example.com" # Where should notifications arrive? 7 | frommail="noreply@example.com" # Where should notifications come from? 8 | screen_name="stoya" # Your Twitter username 9 | # Fill with your own key and token: 10 | # https://apps.twitter.com/app/new 11 | consumer_key="xxxxxxxxxxxxxxxxxxxxxxxxx" 12 | consumer_key_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 13 | token="xxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 14 | token_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 15 | 16 | 17 | # You can stop editing here :) 18 | 19 | 20 | method="GET" 21 | basedir=$(dirname $0) 22 | mkdir $basedir/out 2>/dev/null 23 | newids="$basedir/out/newids.txt" 24 | oldids="$basedir/out/oldids.txt" 25 | unfollowerids="$basedir/out/unfollowerids.txt" 26 | unfollowernames="$basedir/out/unfollowernames.txt" 27 | unfollowers="$basedir/out/unfollowers.txt" 28 | mailtmp="$basedir/out/mailtmp.txt" 29 | 30 | # Get our follower's ids 31 | followers_url="https://api.twitter.com/1.1/followers/ids.json?screen_name=$screen_name" 32 | followers_oauth_sign=$($basedir/oauth_sign $consumer_key $consumer_key_secret $token $token_secret $method $followers_url) 33 | curl -s --request $method $followers_url --header "Authorization: $followers_oauth_sign" | $basedir/jq '.ids' | tr -d '[], ' | grep . > $newids 34 | 35 | # Check if the query was successful 36 | # A bit hackish, basically checks if there is something which looks like an ID 37 | if ! grep -E -q -i -o "[0-9]{7,999}" $newids ; then 38 | exit 39 | fi 40 | 41 | # If it's the first time, we don't want to spam our inbox 42 | if [[ ! -f $oldids ]]; then 43 | cp $newids $oldids 44 | exit 45 | fi 46 | 47 | # Get our unfollowers 48 | cat $newids $newids $oldids | sort | uniq -u > $unfollowerids 49 | 50 | # Prepare for the next run 51 | rm -f $oldids 52 | cp $newids $oldids 53 | 54 | # If we got unfollowers, match their IDs with screen names 55 | if [[ -s $unfollowerids ]] ; then 56 | # cleanup 57 | rm -f $unfollowernames 58 | while read line; do 59 | # lookup the screen_name of the ids 60 | lookup_url="https://api.twitter.com/1.1/users/lookup.json?user_id=$line" 61 | lookup_oauth_sign=$($basedir/oauth_sign $consumer_key $consumer_key_secret $token $token_secret $method $lookup_url) 62 | curl -s --request $method $lookup_url --header "Authorization: $lookup_oauth_sign" | $basedir/jq .[]'.screen_name' | tr -d '"' >> $unfollowernames 63 | done < $unfollowerids 64 | else 65 | # If we haven't unfollowers, we're done. 66 | exit 67 | fi 68 | 69 | # jq will cry if we pass the json from a deleted user. Remove blank lines too. 70 | grep -v 'jq: error: Cannot index array with string' $unfollowernames | grep . > $unfollowers 71 | 72 | # If we matched some ids to screen names, send the mail 73 | if [[ -s $unfollowers ]] ; then 74 | echo "Hi $screen_name", > $mailtmp 75 | echo >> $mailtmp 76 | echo "The following people isn't following you any longer:" >> $mailtmp 77 | echo >> $mailtmp 78 | cat $unfollowers | sed -e 's#^#https://twitter.com/#' >> $mailtmp 79 | mail -a "From: $frommail" -s "Someone has unfollowed you" $mail < $mailtmp 80 | fi 81 | --------------------------------------------------------------------------------