Permalink
Browse files

Suggested Followers initial script added

  • Loading branch information...
1 parent e1f015b commit cab2237438b115adfd65dfade305c99b3b9bb4b3 @robraux committed Mar 8, 2012
Showing with 101 additions and 0 deletions.
  1. +101 −0 suggested_followers.php
View
@@ -0,0 +1,101 @@
+<?
+/**
+ * 0. Setup a new twitter application to use API - https://dev.twitter.com/apps/new
+ * 1. Make sure you twitter application has POST (read and write) access, not just GET (read) access.
+ * 1. Install twurl - https://github.com/marcel/twurl
+ * 2. Authorize twurl
+ * 3. Now script can be run as is.
+ *
+ * Notes: If you authorize twurl w/read only access, and then change it to read/write, you need to revoke the credentials and reauth with the new perms.
+ */
+
+date_default_timezone_set('America/New_York'); // just used to print the start time of the application run
+echo date("Y-m-d G:i:s") . "\n";
+
+$a = new SuggestedFollower();
+$a->start_following();
+
+/**
+* Determine who to follow based on twitter's suggested user feed, and follow them
+*/
+class SuggestedFollower
+{
+ protected $rec_users = '/1/users/recommendations.xml';
+ protected $friend_user = '/1/friendships/create.xml';
+
+ function __construct()
+ {
+
+ }
+ function start_following()
+ {
+ $who_to_follow = $this->exec_twurl($this->rec_users);
+
+ if(!$who_to_follow) {
+ $this->_log_error("No data returned from twitter");
+ return;
+ }
+ $followers = simplexml_load_string(str_replace('NONAMESPACESCHEMALOCATION', 'noNamespaceSchemaLocation', str_replace('XMLNS:XSI', 'xmlns:xsi', $who_to_follow)));
+
+ foreach($followers->userrecommendation as $follower) {
+ $user = $follower->user;
+
+ // make sure you only follow folks who are legit
+ $should_skip = $this->should_skip_follow($user);
+ if(!$should_skip) {
+ $this->_log_success("Following:". $user->id. " (" . $user->screen_name .")");
+ }
+ else {
+ $this->_log_error("Skipping:". $user->id. " (" . $user->screen_name .") because $should_skip");
+ continue;
+ }
+
+
+ $friend_attempt = $this->exec_twurl($this->friend_user, 'POST', "user_id=". $user->id);
+ }
+
+ }
+
+ protected function exec_twurl($fragment, $type = 'GET', $params = '')
+ {
+ $p = '';
+ if($type != 'GET' && $params != '') {
+ $p = "-d '$params'";
+ }
+
+ $cmd = "twurl -X $type $p '$fragment'";
+ return `$cmd`;
+ }
+
+ protected function should_skip_follow($user)
+ {
+ if(!(string)$user->id) {
+ return 'no user';
+ }
+ if((string)$user->protected == 'true'){
+ return 'is protected';
+ }
+ if((int)$user->friends_count < 75) {
+ return 'not enough friends';
+ }
+ if((string)$user->following == 'true'){
+ return 'already following';
+ }
+ if((string)$user->follow_request_sent == 'true') {
+ return 'already requested follow';
+ }
+
+ // no need to skip
+ return false;
+ }
+
+ private function _log_error($str)
+ {
+ echo "Error: $str\n";
+ }
+ private function _log_success($str)
+ {
+ echo "Success: $str\n";
+ }
+}
+

0 comments on commit cab2237

Please sign in to comment.