Commits

Eric Larson  committed dab92d1

Updated heyfollower to not send DMs on first run

  • Participants
  • Parent commits d7a52a5

Comments (0)

Files changed (1)

File twitterbot/heyfollower.py

             try:
                 c.execute('select * from followers')
             except sqlite3.OperationalError, e:
-                c.execute('create table followers(name varchar(255), extra text)')
+                c.execute('create table followers (name varchar(255), extra text)')
+                c.execute('create table follower_state (initialize boolean, numfollowers int)')
+                c.execute('insert into follower_state values (?, ?)', (False, 0))
+
+    @property
+    def initialized(self):
+        with Transaction(self.db) as c:
+            rows = c.execute('select * from follower_state').fetchmany()
+            if not rows:
+                return False
+            return rows[0]
 
     def save(self, name, extra):
-        print 'saving: ', name, extra
         with Transaction(self.db) as c:
             c.execute('insert into followers VALUES (?, ?)',
                       (name, simplejson.dumps(extra)))
         self.api = api
         self.conf = conf
         self.followers = Followers(self.conf['follower_db'])
-        new = self.update_followers()
-        for u in new:
-            if not conf['debug']:
-                self.api.CreateFriendship(u)
-                self.api.PostDirectMessage(u, 'Thanks for following!')
-            else:
-                print 'created a friends and dmsgd ', u.screen_name
+        inited, numfollowers = self.followers.initialized
+        followers = self.api.GetFollowers()
 
-    def update_followers(self):
-        followers = self.api.GetFollowers()
         new = []
-        for follower in followers:
-            if follower.screen_name not in self.followers:
-                new.append(follower)
-                self.followers.save(follower.screen_name, follower.AsDict())
-            else:
-                print follower.screen_name
-        return new
+    
+        if numfollowers < len(followers):
+            for follower in followers:
+                if follower.screen_name not in self.followers:
+                    new.append(follower)
+                    self.followers.save(follower.screen_name, follower.AsDict())
+
+        if inited:
+            for u in new:
+                if not conf['debug']:
+                    self.api.CreateFriendship(u)
+                    self.api.PostDirectMessage(u, 'Thanks for following!')
+                else:
+                    print 'created a friends and dmsgd ', u.screen_name
+        else:
+            print 'First time running'
 
 
 job = HeyFollower()