-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
203 lines (170 loc) · 6.92 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import tweepy
import re
import config
import time
import requests
import random
from datetime import timezone, timedelta
from requests_oauthlib import OAuth1
import logging
import os.path
import html
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
logfile = os.path.join(BASE_DIR, 'test.log')
logging.basicConfig(
filename=logfile,
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s"
)
class TwitterBot:
# initialization
def __init__(self, checked = 0, posted = 0, notsent = 0, dms = 0):
self.auth = tweepy.OAuthHandler(config.consumer_key, config.consumer_secret)
self.auth.set_access_token(config.access_token, config.access_token_secret)
self.api = tweepy.API(self.auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
self.me = self.api.me()
self.posted = posted #number of dm posted
self.checked = checked #number of dm checked
self.notsent = notsent #number of incoming dm not sent
self.dms = dms
def checkdm(self):
api = self.api
dms = api.list_direct_messages()
for i, dm in enumerate(reversed(dms)):
logging.info(f"DM No. {i+1}: {dm.message_create['message_data']['text'].encode('utf-8')}")
self.dms = dms
self.checked = len(dms)
return self.dms
def post_all(self, dms, checked=0, posted=0, notsent=0):
"""Post all incoming DMs that contain the trigger word.
Arg:
dms (list of direct messages objects), from checkdm() function
"""
api = self.api
logging.info(f'Total DM = {len(dms)}')
for i, dm in enumerate(reversed(dms)):
dmsender = int(dm.message_create['sender_id'])
urls = dm.message_create['message_data']['entities']['urls']
tweet = html.unescape(dm.message_create['message_data']['text'])
if dmsender == self.me.id:
continue
elif config.trigger in tweet:
linkcontent = None
media_ids = None
if 'attachment' in dm.message_create['message_data']:
if dm.message_create['message_data']['attachment']['media']['type'] == 'photo':
media_url = dm.message_create['message_data']['attachment']['media']['media_url']
media_id = self.tweet_attachment(media_url)
media_ids = []
media_ids.append(media_id)
tweet = ' '.join(re.sub("(@[A-Za-z0-9]+)|(\w+:\/\/\S+)", " ",tweet).split())
else:
self.senddm(i, dmsender, status='wrong attachment')
continue
elif len(urls) >0:
linkcontent = urls[0]['expanded_url']
if 'photo' in linkcontent or 'video' in linkcontent:
linkcontent = None
elif 'twitter.com' in linkcontent:
tweet = ' '.join(re.sub("(\w+:\/\/\S+)", " ",tweet).split())
else: linkcontent = None
complete = 0
while len(tweet) > 280:
leftcheck = 260
left = 0
right = 272
check = tweet[leftcheck:right].split(' ')
separator = len(check[-1])
tweet1 = tweet[left:right-separator] + '(cont..)'
if complete == 0:
try:
sendtwt = api.update_status(tweet1, media_ids=media_ids, attachment_url=linkcontent)
time.sleep(10)
except tweepy.error.TweepError as e:
logging.error(e)
continue
posted += 1
complete = sendtwt.id
postid = complete
rttime = sendtwt.created_at.replace(tzinfo=timezone.utc)
else: complete = api.update_status(tweet1, in_reply_to_status_id = complete, auto_populate_reply_metadata = True).id
time.sleep(10)
tweet = tweet[right-separator:len(tweet)]
if complete == 0:
try:
sendtwt = api.update_status(tweet, media_ids=media_ids, attachment_url=linkcontent)
except tweepy.error.TweepError as e:
logging.error(e)
continue
rttime = sendtwt.created_at.replace(tzinfo=timezone.utc)
postid = sendtwt.id
posted +=1
else:
api.update_status(tweet, in_reply_to_status_id = complete, auto_populate_reply_metadata = True)
self.senddm(i, dmsender, status='sent', postid=postid, rttime=rttime)
api.destroy_direct_message(int(dm.id))
logging.info('Finished.')
time.sleep(60+random.randrange(0,60,15))
else:
self.senddm(i, dmsender, status='notsent')
api.destroy_direct_message(int(dm.id))
notsent +=1
self.posted = posted
self.checked = len(dms)
self.notsent = notsent
self.dms = dms
logging.info('DM has been checked')
time.sleep(3)
return
# Notifies the DM sender. Modify your message here.
def senddm(self, i, dmsender, status, postid=None, rttime=None):
api = self.api
url = 'https://twitter.com/'+self.me.screen_name+'/status/'+str(postid)
if status == 'sent':
message = {'sent': 'Post was successfully sent at '+rttime.astimezone(timezone(timedelta(hours=config.timezone))).strftime("%Y-%m-%d %H:%M")+' WIB. Check your post here: '+url}
elif status == 'notsent':
message = {'notsent' : 'Post was not sent. Use the trigger '+config.trigger+' to send post.'}
else: message = {'wrong attachment' : 'Post was not sent. Send only picture attachment (not gif/video).'}
notifdm = api.send_direct_message(recipient_id=dmsender, text=message[status])
#api.destroy_direct_message(int(notifdm.id))
logging.info(f'DM No {i+1} was sent. Status = {status}')
time.sleep(10)
return
def tweet_attachment(self, media_url):
api = self.api
oauth = OAuth1(client_key = config.consumer_key, client_secret = config.consumer_secret, resource_owner_key = config.access_token, resource_owner_secret = config.access_token_secret)
r = requests.get(media_url, auth = oauth)
filename = 'temp.jpg'
if r.status_code == 200:
with open(filename, 'wb') as image:
for chunk in r:
image.write(chunk)
media_ids = api.media_upload(filename).media_id
return media_ids
def delete_dm(self, dms):
api = self.api
for dm in dms:
if dm.id == self.me.id:
try: api.destroy_direct_message(dm.id)
except tweepy.error.TweepError as e:
logging.error(e)
continue
return
def delete_all(self, dms):
api = self.api
for dm in dms:
try: api.destroy_direct_message(dm.id)
except tweepy.error.TweepError as e:
logging.error(e)
continue
return
def __str__(self):
logging.info(f"DM counts: {self.checked}, DM posted: {self.posted}, DM not sent: {self.notsent}")
dmcontent = """
DM counts : {}
DM posted : {}
DM not sent : {}
""".format(self.checked, self.posted, self.notsent)
return dmcontent
def __repr__(self):
return f"DM counts: {self.checked}, DM posted: {self.posted}, DM not sent: {self.notsent}"