2009-01-18

newLISP: Twitter Followers/Friends/Common

Here's what I came up with. It does the following:

  1. Builds the HTTP Basic Auth header
  2. Fetches all your followers (100 at a time until no more are found)
  3. Fetches all your friends (again, 100 at a time)
  4. Looks for each of your followers in your friends list
  5. Prints each follower that you haven't marked as a friend
  6. Adds commons (friends who follow you back) to a list
  7. Prints each friend that's not following you
  8. Prints the list of commons
I'm still a newLISP neophyte, and there are some things I know I could have done much more efficiently and "lispy" but this is about the best I could come up with. I also would normally use much longer lines, but I broke it up so that it would show here without breaking the format of the page too badly. Oh, and mega-props to Code2HTML for colorizing this.
#!/usr/bin/newlisp
# Build an HTTP Basic Auth header for get-url.
# Set 'user and 'pass manually
(set 'user "YourTwitterName" 'pass "NotMyPasswd")
(set 'hedr (append "Authorization: Basic "
(base64-enc (append user ":" pass)) "\r\n\r\n"))

# Grabs followers until results list has less than 100
(set 'page 0) (do-until (< numthese 100) (inc page)
(set 'xml (get-url (append
"http://twitter.com/statuses/followers.xml?page="(string page))
5000 hedr))
(set 'these (find-all "<screen_name>(.*)</screen_name>" xml $1))
(cond ((= page 1) (set 'fol these))
((> page 1) (set 'fol (append fol these))))
(set 'numthese (length these))
)

# Grabs friends until results list has less than 100
(set 'page 0) (do-until (< numthese 100) (inc page)
(set 'xml (get-url (append
"http://twitter.com/statuses/friends.xml?page="(string page))
5000 hedr))
(set 'these (find-all "<screen_name>(.*)</screen_name>" xml $1))
(cond ((= page 1) (set 'fri these))
((> page 1) (set 'fri (append fri these))))
(set 'numthese (length these)))

# Iterate followers against friends list
(dolist (follower fol)
(if (nil? (find follower fri))
(print (append follower " is not a friend\r\n"))
# If follower and friend, push to com list
(if (nil? com) (set 'com (list follower))
(push follower com))))

# Iterate friends against followers list
(dolist (friend fri)
(if (nil? (find friend fol))
(print (append friend " is not following\r\n")) (inc notfol)))

# Print list of common (friend & follow)
(dolist (common com)
(print (append common " is following back.\r\n")))
(exit)


Why'd I do this? Why not use Twitter Karma or some other online tool?
  1. I didn't want to enter my username and password into someone else's web app.
  2. I wanted a quick programming project that could take up part of my weekend.
  3. It was fun.

blog comments powered by Disqus