-
Notifications
You must be signed in to change notification settings - Fork 0
/
OofnanBot.py
161 lines (118 loc) · 5.39 KB
/
OofnanBot.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
# OofnanBot
import tweepy
import os
import time
import requests
import cv2
from facedetection import detectFaces
from dotenv import load_dotenv
load_dotenv()
# Authenticating to Twitter
def createApi():
'''Function to connect and authenticate to the Twitter API'''
# Getting the env variables
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_SECRET = os.getenv("CONSUMER_KEY")
CONSUMER_KEY = os.getenv("CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("CONSUMER_SECRET")
ACCESS_KEY = os.getenv("ACCESS_KEY")
ACCESS_SECRET= os.getenv("ACCESS_SECRET")
# Authorizing
auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY,ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
# Checking the connection
try:
api.verify_credentials()
print("Authorized!")
except:
print("An Error occurred during authentication")
return api
def infoAboutMe(user):
'''Function that returns a string with user's name, description and location.'''
location_present = True # Flag Variable to check if the user has publicly set their location.
if user.location == '':
location_present = False
if location_present:
user_details = f" 𝐍𝐚𝐦𝐞: {user.name} \n 𝐃𝐞𝐬𝐜𝐫𝐢𝐩𝐭𝐢𝐨𝐧: {user.description} \n 𝐋𝐨𝐜𝐚𝐭𝐢𝐨𝐧: {user.location}"
else:
user_details = f" 𝐍𝐚𝐦𝐞: {user.name} \n 𝐃𝐞𝐬𝐜𝐫𝐢𝐩𝐭𝐢𝐨𝐧: {user.description} \n 𝐋𝐨𝐜𝐚𝐭𝐢𝐨𝐧: Unknown 🤫"
return user_details
def getLastSeenID(file_name):
fobj = open(file_name, 'r')
last_seen_id = int(fobj.read().strip())
fobj.close()
return last_seen_id
def storeLastSeenID(last_seen_id, file_name):
fobj = open(file_name, 'w')
fobj.write(str(last_seen_id))
fobj.close()
return
def replyToTweets():
# Getting the last_seen_ID
last_seen_id = getLastSeenID("last_seen_id.txt")
# Grabbing the mentions timeline
timeline = api.mentions_timeline(since_id = last_seen_id, tweet_mode='extended') # Extended to allow for a tweet's full_text
for tweet in reversed(timeline):
print(f"Tweet ID: {tweet.id} - {tweet.user.name} said {tweet.full_text}")
storeLastSeenID(tweet.id, "last_seen_id.txt")
# 1541814691629977600
if '#helloworld' in tweet.full_text.lower():
reply_tweet = " #HelloWorld back to you!! " + "@" + tweet.user.screen_name
api.update_status(reply_tweet, in_reply_to_status_id = tweet.id)
print("A Hello World Response Has Been Sent.")
elif '#aboutme' in tweet.full_text.lower():
reply_tweet = infoAboutMe(tweet.user) + '\n' + "@" + tweet.user.screen_name
api.update_status(reply_tweet, in_reply_to_status_id = tweet.id)
print("An About Me Response Has Been Sent.")
elif '#facedetection' in tweet.full_text.lower():
# Grabbing any media in the tweet
media_files = []
media = tweet.entities.get('media', [])
if(len(media) == 0):
# No picture attached with tweet
reply_tweet = " I could not find an image with your tweet to analyse, " + "@" + tweet.user.screen_name
api.update_status(reply_tweet, in_reply_to_status_id = tweet.id)
print("A Face Detection Response Has Been Sent.")
else:
media_files.append(media[0]['media_url'])
# Downloading the image for further processing
image_link = requests.get(media_files[-1])
f = open('that_image.jpg','wb')
f.write(image_link.content)
f.close()
# Detecting faces
final_image_path, number_of_faces = detectFaces('that_image.jpg')
# Uploading the image to twitter
uploaded_media = api.media_upload(final_image_path)
# Posting the tweet with the image
reply_tweet = f"I found {number_of_faces} faces in this image! " + "@" + tweet.user.screen_name
api.update_status(status=reply_tweet, media_ids=[uploaded_media.media_id],in_reply_to_status_id = tweet.id)
print("A Face Detection Response Has Been Sent.")
# def getMedia():
# # Grabbing the mentions timeline
# timeline = api.mentions_timeline( #since_id = last_seen_id,
# tweet_mode='extended') # Extended to allow for a tweet's full_text
# media_files = []
# for tweet in reversed(timeline):
# # check for hashtag
# media = tweet.entities.get('media', [])
# if(len(media) > 0):
# media_files.append(media[0]['media_url'])
# print(media_files)
# Downloading the image for further processing
# image_link = requests.get(media_files[-1])
# f = open('that_image.jpg','wb')
# f.write(image_link.content)
# f.close()
# Detecting faces
# final_image_path = detectFaces('that_image.jpg')
# # Uploading the image to twitter
# uploaded_media = api.media_upload(final_image_path)
# # Post tweet with image
# api.update_status(status='', media_ids=[uploaded_media.media_id]) #in_reply_to_status_id = tweet.id
if __name__=="__main__":
api = createApi()
while True:
replyToTweets()
time.sleep(15)