Skip to content

Instantly share code, notes, and snippets.

@skoppula
Last active November 3, 2017 04:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save skoppula/ff9848abcef896cc8f7f to your computer and use it in GitHub Desktop.
EmailBot: a script that queries for random noun/adjective, related image, and emails out the results every hour.
0 * * * * cd /homes/skoppula/random-image && python /homes/skoppula/random-image/random-image.py > /homes/skoppula/random-image/random-image.log 2>&1
import requests
import re
import shutil
import os
import smtplib
import random
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Query random adjective and random noun generator
resp = requests.get("https://www.randomlists.com/random-adjectives")
lst = [m.start() for m in re.finditer('crux', resp.text)]
adjective = resp.text[lst[0]:lst[0]+20].split('<')[0].split('>')[1]
resp = requests.get("https://www.randomlists.com/random-nouns")
lst = [m.start() for m in re.finditer('crux', resp.text)]
noun = resp.text[lst[0]:lst[0]+20].split('<')[0].split('>')[1]
random_phrase = adjective + ' ' + noun
print random_phrase
# Query image
resp = requests.get("https://www.google.com/search?site=&tbm=isch&q="+adjective+"+"+noun)
img_urls = [block.split(" ")[2][5:-1] for block in filter(lambda x: 'gstatic.com' in x, filter(lambda x: 'img' in x, resp.text.split("<")))]
print img_urls[0]
temp_img_path = './img.png'
response = requests.get(img_urls[0], stream=True)
with open(temp_img_path, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
response.close()
# Send image
me = 'the-president-of-funny@bentry.com'
recipients = ['XXX@gmail.com', 'XXX@mit.edu']
msg = MIMEMultipart()
msg['Subject'] = 'Your hourly funny! ' + random_phrase
msg['From'] = me
msg['To'] = ', '.join(recipients)
msg.preamble = random_phrase
with open(temp_img_path, 'rb') as imgf:
img = MIMEImage(imgf.read())
msg.attach(img)
phrases = ["don't you love this?", "fantastic! Isn't that wild?", "holy smokes, that's charming, isn't it?", "kowabunga! Bowie the Burrito loves that!", "aw yeah! Totally rad!", "no way! It's a dream come true!", "almost better than a burrito!"]
the_text = MIMEText(random_phrase + ': ' + random.choice(phrases) + "\n\n Brought to you by Burrito Funnies and Pics. Ain't no unsubscribing from me!!!", 'plain')
msg.attach(the_text)
with open('g_creds.txt','r') as f:
line = f.readlines()[0].split(',')
u = line[0].split(':')[1]
p = line[1].split(':')[1]
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.ehlo()
server.login(u, p)
server.sendmail(me, recipients, msg.as_string())
server.quit()
@skoppula
Copy link
Author

skoppula commented Nov 3, 2017

The result of a friendly (and funny) challenge: a small script that would query the a random word site for a random adjective and noun, query Google images with the word pair, and email out the word pair and image to registered parties. The script would run (and email out once) every hour, courtesy of crontab. Hilarity ensued:

email1
email2

The only machine I had access to (which I could keep powered on throughout the entire day) was a machine to which I had no root access and with very few already available binaries. The machine was running Python 2.6. I adjusted my script to 2.6, and installed the requests Python package to the local Python path. I let EmailBot run for about a day, before we collectively decided it might be too much email.

If you would like to keep Emailbot running, have computing resources to donate, and/or become an Emailbot recipient, do feel free to reach out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment