# -*- encoding: utf-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Created by Gean Yoshio Iwamoto - Stiod.com
# Email: gean at stiod.com
# Twitter: @yoshioiwamoto
#

from urllib2 import HTTPError
import twitter

# Para meros mortais o Twitter permite apenas 150 requests por hora.
REQUESTS_LIMIT = 150

# Informe aqui os seu usuário e senha do Twitter.
# Pode-se colocar mais de um usuários.
your_users = [
    ('seu_usuario', 'senha',), # Ex: ('joao', 'asd123'),
    # ('outro usuario', 'senha'),
    # ...
]

# Escreva entre as aspas os usuários que você considera relevante para seguir.
twitter_users = '''
yoshioiwamoto
rafaelsdm
ricardoperez
gabrielverta
unaghii
'''

twitter_users = [tuser.strip() for tuser in twitter_users.split('\n') if tuser.strip() != '']
total = len(twitter_users)

print total, 'usuários para seguir'

requests = 0
for username, password in your_users:
    requests += 1
    print 'Realizando login no Twitter com usuário "%s"'%(username)
    api = twitter.Api(username=username, password=password)
    print '-'*70
    for idx, twitter_user in enumerate(twitter_users):
        if (requests + 2) > REQUESTS_LIMIT:
            print 'Não é possível continuar pois irá atingir o limite de %d requests permitidos pelo Twitter'%REQUESTS_LIMIT
            break
        try:
            requests += 1
            fuser_ret = api.DestroyFriendship(twitter_user)
        except HTTPError, e:
            # Ocorre um erro ao tentar dar unfollow em alguém que você ainda não
            # está seguindo, mas isto é normal.
            pass

        print '[%d/%d] Seguindo "%s"'%(idx + 1, total, twitter_user)
        try:
            requests += 1
            fuser = api.CreateFriendship(twitter_user)
        except HTTPError, e:
            print 'Ops: %s'%e

print 'Total de requests realizados:', requests
