Код> Добавить | Пролистать [ Twitter autofollower v1]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Обратите внимание на ограничения твиттера.
# Пользователь может добавить в друзья 1000 пользователей в сутки
# Без фоловеров, пользователь может добавить в друзья не более 2000 пользователей.
# Зависимости: python-twitter
# (в Debian Gnu/Linux: apt-get install python-twitter)

import twitter,sys, time, random
import simplejson
from optparse import OptionParser

class MyTwitApi(twitter.Api):
    def GetFriendIDs(self, user=None, page=None):
        if not user and not self._username:
            raise TwitterError("twitter.Api instance must be authenticated")
        if user:
            url = 'http://twitter.com/friends/ids/%s.json' % user 
        else:
            url = 'http://twitter.com/friends/ids.json'
        parameters = {}
        if page:
            parameters['page'] = page
        json = self._FetchUrl(url, parameters=parameters)
        data = simplejson.loads(json)
        self._CheckForTwitterError(data)
        return data
      
def follow(username, password, user, debug):
    """
    добавляет в друзья всех пользователей из
    списка follow пользователя user
    """
    api = MyTwitApi(username=username, password=password)

    if debug:
        print "Логин %s, Пользователь для фолофинга: %s" %(username, user)
    print "Username: ", username

    u = api.GetFriendIDs(user)
    if debug:
        print "Всего %s пользователей" %len(u)
    j = 50
    for user in u:
        i = random.randint(1,5)
        if debug:        
            print "Following: ",user
        if i % j==0:
            print "50 users"
        try:
            r = api.CreateFriendship(user)
        except Exception, e:
            print "Error while following ",user
            print sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
            i = random.randint(10, 30)
        time.sleep(i)        
    
    return result

if __name__=="__main__":

    parser = OptionParser()
    parser.add_option("-u", "--username", dest="username", help=u"Ваш логин в твиттере")
    parser.add_option("-p", "--password", dest="password", help=u"Ваш пароль в твиттере")    
    parser.add_option("-f", "--follow", dest="follow_user", help=u"Username (или id) Твиттер пользователя, для анализа и фоловинга за его пользователями")
    parser.add_option("-d", "--debug", action="store_true", dest='debug', default=False, help=u"Отображать подробно ход процесса выполнения, диагностика")    
    (options, args) = parser.parse_args()
    if options.username is None or options.password is None or options.follow_user is None:
        parser.print_help()
        sys.exit(0)
    else:
        follow(options.username, options.password, options.follow_user, options.debug)