-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdrawReplyUserBot.py
More file actions
executable file
·82 lines (60 loc) · 2.39 KB
/
drawReplyUserBot.py
File metadata and controls
executable file
·82 lines (60 loc) · 2.39 KB
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
#!/usr/bin/python3.8
import tweepy
import logging
from config import create_api
from resources.drawImage import drawImage
import matplotlib.pyplot as plot
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def tweet_image(api, tweet):
try:
logger.info(f"{tweet.user.name} has replied to your status, let's go!")
tweet_message = f"Hello {tweet.user.name}, I hope you enjoy your drawing"
image_path = get_profile_picture_path(api, tweet.user)
image = drawImage(image_path)
# save image to temp file
plot.imsave('temp.png', image, cmap='gray', vmin=0, vmax=255)
media_list = get_image(api)
logger.info(f"replying to status: {tweet.id}")
# send reply with drawn image of user
api.update_status(status = tweet_message,
in_reply_to_status_id = tweet.id,
auto_populate_reply_metadata=True,
media_ids=media_list)
logger.info("Tweet sent successfully! :) ")
except:
logger.info("failed to draw image")
def get_profile_picture_path(api, user):
# get url of profile picture
image_path = user.profile_image_url_https
# original image path is the above with "_normal" removed from the url
original_image_path = image_path.replace('_normal', '')
logger.info(f"image url: {original_image_path}")
return original_image_path
def get_image(api):
media_list = list()
# read image from temp file (probably a better way to do this)
response = api.media_upload("temp.png")
media_list.append(response.media_id_string)
return media_list
def check_replies(api, since_id):
logger.info("Retrieving mentions")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
logger.info(f"new mention found from {tweet.user.name}...")
new_since_id = max(tweet.id, new_since_id)
if ("draw me" in tweet.text.lower()):
logger.info(f"sending drawing...")
tweet_image(api, tweet)
return new_since_id
def main():
api = create_api()
since_id = 1
while True:
since_id = check_replies(api, since_id)
logger.info("Waiting...")
time.sleep(60)
if __name__ == "__main__":
main()