-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
153 lines (132 loc) · 4.35 KB
/
bot.py
File metadata and controls
153 lines (132 loc) · 4.35 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
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
import tweepy
import boto3
import requests
import psycopg2
import sys
import datetime
import time
from pytz import timezone
from dotenv import load_dotenv
from os import path, environ
dotenv_path = path.join(path.dirname(__file__), '.env')
load_dotenv(dotenv_path)
try:
AKEY = environ.get('AKEY')
ASKEY = environ.get('ASKEY')
ATKEY = environ.get('ATKEY')
ATSKEY = environ.get('ATSKEY')
AWSID = environ.get('AWSID')
AWSSID = environ.get('AWSSID')
db = environ.get("DATABASE_URL")
except KeyError:
print("Config not accessible.")
sys.exit(1)
# Authentication
auth = tweepy.OAuthHandler(AKEY, ASKEY)
auth.set_access_token(ATKEY, ATSKEY)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()
# Set interval and limit
ntweets = 500
keywords = ["Sunset", "Sunrise", "Sunlight", "Dusk"]
antikeywords = ["Person", "Human"]
# Search for keyword tweets
def search():
searchResults = tweepy.Cursor(api.search, q='sunset OR sunsets -filter:retweets', lang="en", include_entities=True)
for tweet in searchResults.items(ntweets):
for media in tweet.entities.get("media",[{}]):
if media.get("type",None) == "photo":
imageurl = tweet.entities["media"][0]["media_url_https"]
image = getimagebytes(imageurl)
confidence = define(image)
# When detection confidence is sufficient
if confidence > 60:
try:
retweet(tweet, imageurl)
return True
except tweepy.TweepError as e:
pass
except StopIteration:
pass
# Twitter interaction and update database
def retweet(tweet, imageurl):
tweet.retweet()
tweetimage = uploadimage(imageurl)
tweetloc = tweet.user.location
tweettime = tweet.created_at
tweettext = tweet.text
# Update database
updateDb(tweetloc, tweettime, tweettext, tweetimage)
print(tweetloc, tweettime, tweettext)
# Database update
def updateDb(tweetloc, tweettime, tweettext, tweetimage):
try:
hour = gethour()
conn = psycopg2.connect(db)
cur = conn.cursor()
query = '''
INSERT INTO tweets (id, location, time, text, image)
VALUES (%s, %s, %s, %s, %s)
ON CONFLICT (id) DO UPDATE SET
(location, time, text, image) = (EXCLUDED.location, EXCLUDED.time, EXCLUDED.text, EXCLUDED.image)
'''
cur.execute(query, (hour, tweetloc, tweettime, tweettext, tweetimage))
conn.commit()
conn.close()
except:
print("Error updating DB")
sys.exit()
# Define image with AWS
def define(image):
client = boto3.client('rekognition',
aws_access_key_id=AWSID,
aws_secret_access_key=AWSSID,
region_name='us-east-2')
results = client.detect_labels(
Image={
'Bytes': image
}
)
labels = results["Labels"]
confidence = 0
for label in labels:
print(label["Name"], label["Confidence"])
if label["Name"] in keywords:
confidence = label["Confidence"]
elif label ["Name"] in antikeywords and label["Confidence"] > 60:
print("Image not appropriate.")
return False
return confidence
# Get hour of day(GMT)
def gethour():
london = timezone('Europe/London')
date = datetime.datetime.now(london)
hour = date.hour
return hour
# Get image bytes
def getimagebytes(imageurl):
response = requests.get(imageurl)
imagebytes = response.content
return imagebytes
# Upload image for current hour
def uploadimage(imageurl):
hour = gethour()
r = requests.get(imageurl, stream=True)
# S3 connection
session = boto3.Session(aws_access_key_id=AWSID,
aws_secret_access_key=AWSSID)
s3 = session.resource("s3")
bucket_name = "permanent-sunset"
filename = "%s.jpg" % hour
bucket = s3.Bucket(bucket_name)
bucket.upload_fileobj(r.raw, filename)
imagepath = "https://%s.s3.us-east-2.amazonaws.com/%s" % (bucket_name, filename)
return imagepath
# Search for tweets until there is a hit
def start():
while True:
hit = search()
print("Sleeping...")
time.sleep(3600)
if __name__ == "__main__":
start()