forked from nealamendoza/CodeChella
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (68 loc) · 2.31 KB
/
main.py
File metadata and controls
82 lines (68 loc) · 2.31 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
import keys as s
import tweepy
import json
import webbrowser
import time
from pprint import pprint
import requests
from auto_ml.predict import *
from calscaper.calscraper import *
from google.protobuf.json_format import MessageToJson
#Replace Keys with Final Twitter Account
auth = tweepy.OAuthHandler(s.consumer_key, s.consumer_secret)
auth.set_access_token(s.access_token, s.access_token_secret)
api = tweepy.API(auth)
"""
Function used to download images
path, url are passed in from the searchForImages()
"""
def download_image(url , project_id, model_id):
path = 'image.png'
with requests.get(url, stream=True) as r:
content = b''
for chunk in r.iter_content(chunk_size=8192):
content = content + chunk
name = get_prediction(content, project_id, model_id)
desc = parse_sentence(name)
link = get_webpage(name)
resp = {
'name': name,
'desc': desc,
'link': link
}
return(resp)
"""
Function is used to check if a given id is in the replied_tweets.txt file
if it is found it will return True otherwise return false
"""
def check_id_in_file(id):
with open('replied_tweets.txt' , 'r') as read_obj:
for line in read_obj:
if(id in line):
return True
return False
"""
Grabs the picture from the last mentioned tweet
and then returns data to about that plant to the twitter user
"""
def searchForImages():
mentions = api.mentions_timeline()
myFile = open('replied_tweets.txt', 'a')
for mention in mentions:
mention_id = str(mention.id)
if(('#roots' in mention.text) and ('media' in mention.entities) and (check_id_in_file(mention_id)== False)):
ent = (mention.entities)
image = (ent['media'][0]['media_url'])
plant = download_image(image, s.project_id, s.model_id)
myFile.write(str(mention.id))
myFile.write("\n")
api.update_status(("@" + mention.user.screen_name + " " + plant['desc']), mention.id)
api.update_status(("@" + mention.user.screen_name + " Learn more about it here! " + plant['link']), mention.id)
print("Replied")
myFile.close()
def main():
while(True):
print("Searching for Images")
searchForImages()
time.sleep(5)
main()