Skip to content

Commit

Permalink
Finished
Browse files Browse the repository at this point in the history
  • Loading branch information
supthunder authored Jan 18, 2017
1 parent fdaf8c3 commit cb2b5b2
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
username = ""
tweetId = ""


# Tweet with new image
def tweet(imageName):
tweet = "@"+username+"\n"
tweet += "Converted Image:\n"
Expand All @@ -24,48 +26,68 @@ def tweet(imageName):
api.update_with_media(imageName,status=tweet,in_reply_to_status_id=tweetId)
os.remove(imageName)


# Returns file name from an url
def getImageName(imageLink):
nameRegex = re.compile('[^/]*$')
imageName = "./"
imageName += re.search(nameRegex, imageLink).group(0)
return imageName

def loadLog():
# # open file, read in log
with open('mentionLog.txt', 'r') as outfile:
mentionLog = json.load(outfile)
return mentionLog

def writeLog(mentionLog):
# Remove previous user
while(len(mentionLog) > 100):
mentionLog.pop()
# open file, write in log
with open('mentionLog.txt', 'w') as outfile:
json.dump(mentionLog, outfile)

# Get latest twitter mention
def getMentions():
mentionLog = loadLog()
imageUrl = ""
mentions = api.mentions_timeline(count=1)
global username
global tweetId
for mention in mentions:
imageUrl = mention.entities['media'][0]['media_url']
username = mention.user.screen_name
tweetId = mention.id
if str(mention.created_at) not in mentionLog:
imageUrl = mention.entities['media'][0]['media_url']
username = mention.user.screen_name
tweetId = mention.id
mentionLog.append(str(mention.created_at))
writeLog(mentionLog)
else:
print("No new Tweets!")
exit(1)

imageName = getImageName(imageUrl)
urllib.request.urlretrieve(imageUrl, imageName)

return imageName



# upload image to text-image.com
def uploadImage(url, image):

file = {'image': open(image,'rb')}
r = requests.post(url, files=file)
soup = BeautifulSoup(r.text,"html.parser")

imageLink = (soup.find(bgcolor='black')).find('img')['src']
# nameRegex = re.compile('[^/]*$')
# imageName = "./"


# imageName += re.search(nameRegex, imageLink).group(0)
imageName = getImageName(imageLink)
urllib.request.urlretrieve(imageLink, imageName)
tweet(imageName)

def main():
url = "http://www.text-image.com/convert/matrix.cgi"
image = getMentions()
uploadImage(url,image)
os.remove(image)
try:
uploadImage(url,image)
os.remove(image)
except:
print("Error")
exit(1)
main()

0 comments on commit cb2b5b2

Please sign in to comment.