-
Notifications
You must be signed in to change notification settings - Fork 0
/
replier.py
91 lines (82 loc) · 2.91 KB
/
replier.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
# -*- coding: utf-8 -*-
import oauth, tweepy
import time, sqlite3
import logging
def init_api():
consumer_key = ""
consumer_secret = ""
access_key=""
access_secret=""
proxy_h = ""
proxy_p = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# return tweepy.API(auth, proxy_host = proxy_h, proxy_port = proxy_p)
return tweepy.API(auth, secure=True)
def init_db():
path = "pref.db"
conn = sqlite3.connect(path)
try:
conn.execute('select * from replied')
except sqlite3.OperationalError, e:
conn.execute('create table replied (id int)')
conn.commit()
return conn
def get_since(conn):
(mx,) = conn.execute('select max(id) from replied').fetchone()
return mx
def save_id(conn, id):
conn.execute('insert into replied values(' + str(id) + ')')
conn.commit()
def is_replied(conn, id):
(count,) = conn.execute('select count(id) from replied where id = ' + str(id)).fetchone()
return count > 0
def format_message(mention):
user = '@' + mention.author.screen_name
text = mention.text.strip()
if text.find('@elephant_trader') == 0:
text = text[16:].strip()
maxl = 140 - 45 - len(user)
if len(text) > (maxl):
text = text[0:maxl-3] + '...'
message = user + unicode(" все говорят «", "utf-8") + text + unicode("». А ты возьми, и купи слона!", "utf-8")
return message
def can_tweet(conn, mention):
ex = not is_replied(conn, mention.id)
me = not (mention.author.id == 372156581)
rt = not mention.retweeted
return ex and me and rt
def work(api, conn):
update_time = 60
while True:
try:
since = get_since(conn)
if since:
m = api.mentions(since_id = since)
else:
m = api.mentions()
for i in m:
if can_tweet(conn, i):
try:
save_id(conn, i.id)
api.update_status(status = format_message(i), in_reply_to_status_id = i.id)
logging.info('send for @' + i.author.screen_name + ': ' + str(i.id))
except tweepy.error.TweepError, e:
logging.info('publish error for @' + i.author.screen_name + ' - ' + str(i.id) + ': ' + str(e))
except Exception, e:
logging.info('cannot get mentions: ' + str(e))
time.sleep(update_time)
if __name__ == '__main__':
logging.basicConfig(filename = 'replier.log',
level = logging.INFO,
format = '%(asctime)s %(levelname)-8s %(message)s',
datefmt = '%d.%m.%Y %H:%M:%S')
api = init_api()
conn = init_db()
try:
work(api, conn)
except KeyboardInterrupt, e:
logging.info('interrupted')
finally:
conn.close()
exit()