def creator(): weibo_accounts = WeiboAccounts.query.filter( WeiboAccounts.status == 1, WeiboAccounts.friends_count < 1800, WeiboAccounts.expires_in > int(time.time()) ).all() for account in weibo_accounts: users = Users.query.filter(Users.followers_count >= 5000, Users.follower == 0).limit(3) for user in users: try: friendships_create(uid=user.uid, access_token=account.access_token, expires_in=account.expires_in) store_friendships(uid=account.uid, target_id=user.uid) update_followers_count(uid=account.uid) print account.uid, user.uid except APIError as e: # TODO User not exist if e.error_code in [10013, 10014, 20003]: store_friendships(uid=account.uid, target_id=user.uid) print account.uid, user.uid if e.error_code in [20506]: if user.uid: store_friendships(uid=account.uid, target_id=user.uid) update_followers_count(uid=account.uid) print account.uid, user.uid print traceback.format_exc() break except Exception as e: print traceback.format_exc() finally: print "[CURRENT_TIME: %s]" % when.now() time.sleep(random.randint(60, 360)) sys.stdout.flush()
def store_friendships(uid, follower_id): user = Users.collection.find_one(uid) if user: followers = user.get('followers', []) if not isinstance(followers, list) and not isinstance(followers, tuple): followers = [followers] followers.append(follower_id) user.followers = followers user.save() friendship = Friendships(uid=uid, follower_id=follower_id, status=1, created_at=when.now()) friendship.save()