-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelatedWordsBot_search&combine.py
108 lines (80 loc) · 3.04 KB
/
RelatedWordsBot_search&combine.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
import requests
__author__ = 'Nicholas'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
import urllib2
import simplejson
import cStringIO
from PIL import Image
from PyDictionary import PyDictionary
import sbi
# Initialize the dictionary
dictionary=PyDictionary()
# Links this bot to the Twitter account: RelatedWordsBot
CONSUMER_KEY = 'I9UkSxDNjbvYDtUQnmJGF2SAH'
CONSUMER_SECRET = 'YkEv5PXQtaux1yf8duN1CUYWPaMpyroBQSNXmN24fmHiAQtDMd'
ACCESS_KEY = '4340105542-dBRVFG4Z4MFjsHmZq7gBHO98rH2rHfyxMsjftJp'
ACCESS_SECRET = 'oUEbFlcQBCYnp1fRKbGNefegyLPsEs3DwjVVCqx9RdnGF'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# Access most recent Tweet from account
tweets = api.user_timeline('RelatedWordsBot')
mostRecent = tweets[0]
contents = str(mostRecent.text)
index = 0
for x in range(0, len(contents)):
if (contents[x] == " "):
index = x
# Word to generate related word from
searchWords = contents.split(" ")
searchWord = searchWords[len(searchWords) - 2].replace(" ", "")
relatedWords = dictionary.synonym(searchWord)
relatedWord = relatedWords[0]
# Word previously generated by bot
prevWord = contents[0 : index]
# Get the image previously generated by the bot
for media in mostRecent.entities.get("media",[{}]):
#checks if there is any media-entity
if media.get("type",None) == "photo":
# checks if the entity is of the type "photo"
imgUrl = media["media_url"]
# save to file etc.
result = sbi.search_by(url=imgUrl)
fetcher = urllib2.build_opener()
searchTerm = relatedWord
# Download image using Bing Search
bing_url = 'https://api.datamarket.azure.com/Bing/Search/Image'
bing_api_key = 'b+otGGjd4HkdQTANFMjLpgaOEmTGDtR38z5JBlCwGPw'
bing_auth = requests.auth.HTTPBasicAuth(bing_api_key, bing_api_key)
bing_page_count = 1
bing_headers = {'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'}
def getFirstImg(subject):
payload = {
'Query':('\'' + subject + '\''),
'$format': 'json',
'$top':bing_page_count,
}
search_response = requests.get(bing_url, auth=bing_auth, params=payload, headers=bing_headers)
try:
search_response_json = search_response.json()
return search_response_json['d']['results'][0]['MediaUrl']
except:
print('Search error: ' + search_response.text)
return None
imageUrl = getFirstImg(relatedWord)
file = cStringIO.StringIO(urllib2.urlopen(imageUrl).read())
img = Image.open(file)
file2 = cStringIO.StringIO(urllib2.urlopen(imgUrl).read())
img2 = Image.open(file2)
### Combine Images ###
sameSize = img2.resize(img.size)
background = img.convert("RGB")
sameSize = sameSize.convert("RGB")
new_img = Image.blend(background, sameSize, 0.5)
img_loc = "/home/nbond/Python/TwitterBots/new.jpg"
new_img.save(img_loc)
status = prevWord + " " + searchTerm.capitalize()
# Update status with related word and merged image
api.update_with_media(filename=img_loc, status=status)