Insights on Ruby, Git, jQuery, Cappuccino, WordPress, Debian and OS X. Please subscribe if you find something useful!

See Which Twitterers Don’t Follow You Back In Less Than 15 Lines of Ruby

Posted: May 12th, 2009 | Author: Jerod | Filed under: Ruby | Tags: , | View Comments

The asynchronous nature of Twitter is one of its keys to success. No friend requests. Awesome.

A lot of the people I follow on Twitter have no business following me. I did’t get upset when DHH, _why, and alexalbrecht did not reciprocate interest. Why would they? They don’t know me from Adam (even though I’m waayyy cooler than that dude…).

However, sometimes it is interesting to see all the jerks people who you follow that do not follow you back. There’s probably a web application out there that does this, but who needs a web app when this is a perfectly good excuse to play with Ruby?

The twitter gem by John Nunemaker makes this task so easy it’s retarded. First, get the gem if you don’t already have it:

sudo gem install twitter

Now. The script:

Isn’t that simple? Are you with me on the retarded comment now? How it works:

The ‘friend_ids‘ and ‘follower_ids‘ methods each return an array of ids and we subtract the followers array from the friends array. All that is left over after subtraction are the ids of users in your friends list (people you follow) that are not in your followers list. Then we loop over the new array of “guilty” parties and fetch their user account information based on their user_id, printing the details each time.

Just replace ‘username’ and ‘password’ with your own information and you’re off to the races. Run the script from your command line and you should see output similar to this:

no_follows

NOTE: Twitter only allows (currently) 100 API requests per hour. Each user account lookup requires an API request, so if your “guilty” array is quite large, you’ll probably get an error before the script terminates (Maybe its time to un-follow a few peeps!).

What other cool tricks can we do using the Twitter gem?


View Comments on “See Which Twitterers Don’t Follow You Back In Less Than 15 Lines of Ruby”

  1. 1 Bryan Roth said at 6:44 am on May 13th, 2009:

    Nice! I'm relatively new to Ruby so this is a nice little treat. Thanks for posting this!

  2. 2 Joe Zack said at 6:52 am on May 13th, 2009:

    I wanted to write a little script using this library that would sniff out people I ought to follow, based on who my friends follow. But the 100 API requests per hour is a bummer.

  3. 3 sant0sk1 said at 6:58 am on May 13th, 2009:

    That could be a fun project.

    What you could do is limit the API requests by sleeping the script for awhile so that you don't overuse the hourly limit. It wouldn't take too many requests to have a list of user_ids that all your friends follow. Then you could cross-reference them and identify accounts that overlap.

  4. 4 Daniel Hepper said at 7:49 am on May 13th, 2009:

    Nice little script!

    Keep in mind that if you have lots of followers and friends (think aplusk), you will run out of API requests before you even fetched all ids.

  5. 5 khelll said at 9:25 am on May 13th, 2009:

    cool!

  6. 6 oriste said at 9:46 am on May 13th, 2009:

    Nifty!

  7. 7 dziegler said at 5:08 pm on May 13th, 2009:

    Very cool. My Python version was 14 ;)
    http://blog.davidziegler.net/post/107429458/see...

  8. 8 Zen»Blog Archive » See Which Twitterers Don’t Follow You Back (and vice-versa) In Less Than 15 Lines of…Perl said at 8:42 pm on May 13th, 2009:

    [...] idea was recently demonstrated in Ruby and was neatly done in 15 lines or less(you might have noticed something familiar about my post [...]

  9. 9 Jasperiel said at 12:35 am on May 14th, 2009:

    puts guilty.map { |user_id| user = base.user(user_id)
    “#{user.name} follows #{user.friends_count} and has #{user.followers_count} followers.”}.join(“n”)}

    Gain two lines by using the much underused “map” iterator.
    Why are most Rubyists neglecting “map” so much ? :(

    Gain one more if you don't declare “guilty” and inline (base.friend_ids – base.follower_ids) before the “map”.
    But this would require to put the counting line after and using “_”, which is quite ugly and less readable.

  10. 10 sant0sk1 said at 5:39 am on May 14th, 2009:

    Thanks for the input! I do use map a lot but I wasn't going for absolute minimum LOC. It was more of a “look how easy it is” script, so brevity & readability were key.

    Still, I agree that map if very cool.

  11. 11 Trouver des followers communs sur Twitter en Ruby « 29 minutes par jour : un blog anti-procrastination said at 11:32 am on May 14th, 2009:

    [...] quelqu’un.” Hey, un challenge! J’avais justement vu un post la veille pour voir les gens que tu follow mais qui ne te followent pas. C’est en Ruby aussi et ça utilise la gem [...]

  12. 12 好妙的創業模式!創業家硬將Twitter扯上男女交友、人力銀行、域名購買等「大題目」 | TechNow 當 said at 8:03 pm on May 14th, 2009:

    [...] 好妙的創業模式!創業家硬將Twitter扯上男女交友、人力銀行、域名購買等「大題目」 標籤: Plurk    Twitter    人力銀行    創業家    創業模式    域名購買    男女交友 今天舉行「NET-MBA開創網站之陷阱、風險、危機一日研討會」,一大早就要到會場,早上時間不夠,提個好玩的案例就收筆── Twitter和Plurk最大的不同在,前者是幾乎開放的,而Twitter的開放重點之一是在於它可以讓外界網站和它進行許多互動。 譬如,美國的技客界最近傳出一個「用15行Ruby on Rails程式算出你follow的哪個人決定不跟著follow你」,然後又有另一個技客再挑戰它,推出「用15行以內的Perl」來做到以上同樣的事情,蠻有意思的。目前已有許多所謂的「Twitter App」出來,大家已經把Twitter當作是一個可以發想點子的平台,點子架在上面,不愁沒人來,人們立即一個拉一個點進來。 但有趣的是,Twitter和Facebook不同,它一直不是一間原本就打算吸引眾多外掛應用程式的網站(儘管它該提供的都有提供出來),現在有些創業家已經開始黏著這樣的「新網站」,想辦法推出一些更大格局的「老玩意」──譬如TechCrunch前天報導了一個令人尊敬的創業家家,這傢伙叫Philip Kaplan,他曾創過兩家公司FuckedCompany和Adbrite,看這兩家就知道這傢伙看得還蠻準的、做得也夠快的。現在,他竟然一口氣又要開三個網站,這三個網站個個都是朝著Twitter而來,都是Twitter的外掛── 第一個叫Tweetname,據說是將「申請域名」這件事和twitter綁在一塊,申請域名是網路上最重要的東西之一。第二件事是要開「Kaplan Index」,據說又是要將「徵才/求職」與twitter綁在一起,不用說,徵才與求職網站當然也是網路上最重要的一塊之一。而今天主要介紹的第三樣東西是「Flirt140」,這是將男女交友和Twitter合在一起,當然,又是網路上最重要的「老玩意」! 到了「Flirt140」的首頁,可看到右手邊就是男女交友網站標準的介面:我是一個「男、女」,正在尋求「男、女」,地點要在「XXX」附近……。按下「開始尋找」,它竟然就開始,在Twitter裡面幫你找對象了! 你說,怎麼可能? 就算Twitter裡面的交談資料大多都「公開」,但它畢竟本來就不是一個交友網站,怎麼弄的?這實在太扯了,難怪以前從來沒人敢想過這件事!所以,你不瞭解Twitter嗎?你沒在玩Twitter嗎?無所謂,我們現在欣賞的不是它怎麼讓Twitter好玩,而是它怎麼利用Twitter,來取得一個通往最大市場的直達電梯? 想想,其實也對。「Flirt140」基本上只要做到三件事情,就可以架著Twitter的脖子,綁架它,去和真正的男女交友網站競爭了:第一,它必須可以「判斷」Twitter每個網友是男是女,以及他們大約的地理位置。這點是可以從網友的設定與話語中得到答案的(這周技客們還在討論著有哪些方式決定此人是男是女,還找來了一張論文與一個工具:請見這裡、這裡)。第二:Flirt140必須能夠從網友的話語中,萃取出一些形容這個人的「關鍵字」,這樣的話,我就可以說,我希望認識「在舊金山地區的大學畢業的女性」,這類的訊息經過Flirt140,它會幫我找到一堆符合以上Twitter帳號。 至於三點的最後一點,則是Flirt140必須讓我可以悄悄的通知這個「推友」,我想和你約會!總不能在 Twitter上面公開的寄一則訊息給她吧!這點Flirt140也很容易做,只要送一個超連結給「她」,讓她點進去確認是「她」後才可以看這封私人訊息。這位創業家信心滿滿的說,透過以上三點機制,Flirt140這個交友網站,將可以完全利用Twitter已經龐大的資源,瞬間超過最大的交友網站 Match.com! 聽起來有沒有很心動呢? 這位叫Kaplan的創業家所做的事情,讓我們突然看到一些機會,看,為何沒人去想到Twitter + 交友網站?因為大家覺得「那是不一樣的東西」。不一樣,所以就不能加在一起,是吧?但,就是因為兩者不一樣,才可以加在一起;因為兩站不同,加在一起才會變成更有趣的新產品,不是嗎? 不一樣,正是加在一起的「第一個條件」! 而加在一起的最後一個條件,則是想辦法去克服這個「不一樣」,一旦克服了,這個創業家就像Kaplan這樣,瞬間做出了一個現成的主流題材的大網站。當然,並不是每件事都是這樣整合在一塊的,但是往往也就因為大家都這麼悲觀的認為「不可能」,所以真的沒人去想過這件事。等到Kaplan這樣輕輕鬆鬆的做出了「Twitter + 男女交友」,我們才突然想到:喔,可以這樣做!沒錯! 我們幾乎可以感受到,Kaplan這位創業家他的出發點是這樣的,他天天看著Twitter,天天就想一個很簡的問題:「OK,看,Twitter在美國這麼紅,我現在就來想辦法,把它扯到『男女交友』方面!」 「把它扯到『人力銀行』方面!」 「把它扯到『域名購買』方面!」 讓人想到,今年,還有哪樣很紅的東西的?譬如,今年流行iPhone,那就想辦法把iPhone扯到徵才求職、男女交友、甚至域名購買上面,這方面很多人都在想了。還有哪些很紅的東西? Flirt140也很吸引我的地方是,坦白說,我對「男女交友」這塊一直都有著高度興趣,覺得這塊還可以被經營得更好一點。哪天大家看到Mr.6部落格、NET-MBA網路成功課程開始在幫忙人家作「婚友配對」,可別怪我沒有先行通知了! Source 標籤: Plurk    Twitter    人力銀行    創業家    創業模式    域名購買    男女交友 [...]

  13. 13 Dinesh said at 9:53 pm on May 14th, 2009:

    nice script
    Love that

  14. 14 lowell said at 11:26 pm on May 14th, 2009:

    you can ask to get white listed: http://twitter.com/help/request_whitelisting

  15. 15 Twitted by iamkhayyam said at 11:27 pm on May 14th, 2009:

    [...] This post was Twitted by iamkhayyam – Real-url.org [...]

  16. 16 BlogMouth » Twitter Tweet Links May 2009 said at 11:59 am on May 31st, 2009:

    [...] See Which Twitterers Don’t Follow You Back In Less Than 15 Lines of Ruby [...]

  17. 17 Mark SMith said at 9:42 am on October 13th, 2009:

    Great Read

  18. 18 Mark SMith said at 4:42 pm on October 13th, 2009:

    Great Read

  19. 19 Twitter Ruby UnFollow Code | Havadan Sudan Bir Blog said at 6:18 am on January 23rd, 2010:

    [...] Orginal code only lists people which are not following you. http://blog.jerodsanto.net/2009/05/see-which-twitterers-dont-follow-youback-in-less-than-15-lines-of... [...]

blog comments powered by Disqus