-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
164 lines (138 loc) · 5.25 KB
/
main.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
""" Script based on libturpial to handle bot account in Twitter to adopt
puppies in Venezuela """
#
# Authors: Wil Alvarez (aka satanas), Carlos Guerrero (aka guerrerocarlos)
# Dic 16, 2011
# Setup the Django environment
import os
import sys
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from adoptave.webui.models import Tweet
import time
import logging
from optparse import OptionParser
from libturpial.api.core import Core
from libturpial.common import ColumnType
POLLING_TIME = 5 #min
ACCOUNT = 'AdoptaVe-twitter'
MAX_FOLLOW = 3
class Adopta:
def __init__(self):
parser = OptionParser()
parser.add_option('-d', '--debug', dest='debug', action='store_true',
help='show debug info in shell during execution', default=False)
(options, args) = parser.parse_args()
if options.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
self.core = Core()
self.followers = []
self.following = []
self.last_dm = None
self.log = logging.getLogger('Server')
self.start_login()
def start_login(self):
self.core.register_account(ACCOUNT.split('-')[0], ACCOUNT.split('-')[1], '')
response = self.core.login(ACCOUNT)
if response.code > 0:
print "Login error:", response.errmsg
return
auth_obj = response.items
if auth_obj.must_auth():
print "Please visit %s, authorize Turpial and type the pin returned" % auth_obj.url
pin = self.user_input('Pin: ')
self.core.authorize_oauth_token(ACCOUNT, pin)
rtn = self.core.auth(ACCOUNT)
if rtn.code > 0:
print rtn.errmsg
else:
self.log.debug('Logged in with account %s' % ACCOUNT.split('-')[0])
self.main()
def user_input(self, message, blank=False):
while 1:
text = raw_input(message)
if text == '' and not blank:
print "You can't leave this field blank"
continue
break
return text
def get_followers(self):
response = self.core.get_followers(ACCOUNT, True)
if response.code > 0:
self.log.error("Error getting followers list:", response.errmsg)
else:
self.followers = response.items
def get_following(self):
response = self.core.get_following(ACCOUNT, True)
if response.code > 0:
self.log.error("Error getting following list:", response.errmsg)
else:
self.following = response.items
def process_follow_back(self):
temp = []
for item in self.followers:
if item not in self.following:
if len(temp) < MAX_FOLLOW:
temp.append(item)
else:
break
for item in temp:
response = self.core.follow(ACCOUNT, str(item), by_id=True)
if response.code > 0:
self.log.error("Error following to %s: %s" % (item, response.errmsg))
else:
self.log.debug("Follow back to %s" % item)
self.following.append(item)
def process_dms(self):
response = self.core.get_column_statuses(ACCOUNT, ColumnType.DIRECTS, 200)
if response.code > 0:
self.log.error("Error fetching DMs: %s" % response.errmsg)
else:
for dm in response.items:
msg_id = dm.id_
valid = self.validate_dm(dm)
if not valid:
continue
via = ' (via @%s)' % dm.username
text = dm.text
length = len(text) + len(via)
if length > 140:
text = text[:len(text) - len(via)]
message = text + via
message.encode('utf-8')
rtn = self.core.update_status(ACCOUNT, message)
if rtn.code > 0:
self.log.error("Error posting message '%s': %s" % (message, rtn.errmsg))
else:
self.register_message(dm)
def validate_dm(self, dm):
# TODO: Search in database for msg_id, if exist then return False
# otherwiser return True
if len(Tweet.objects.filter(status_id=dm.id_)) > 0:
return False
else:
return True
def register_message(self, dm):
# TODO: Add dm to database
t = Tweet(status_id=dm.id_,username=dm.username,content=dm.text)
t.save()
pass
def main(self):
while True:
try:
# Processing followbacks
self.get_followers()
self.get_following()
self.process_follow_back()
# Processing DMs
self.process_dms()
time.sleep(POLLING_TIME * 60)
except KeyboardInterrupt:
break
self.log.debug('Bye')
if __name__ == "__main__":
adopta = Adopta()