Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created December 15, 2013 05:27
Show Gist options
  • Select an option

  • Save dheaney/7969266 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/7969266 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, time
import wikipedia
from datetime import datetime
import smtplib
user = '[email protected]'
pwd = 'SECRET'
path = '/Users/dejay/Dropbox/IFTTT/SMS/shell.iftt.txt'
class Command:
def __init__(self, command, arg):
self.command = command
self.arg = arg
def __call__(self):
self.command(self.arg)
def send_email(msg):
result = wikipedia.search(msg)
chars = wikipedia.summary(result[0])[:140]
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (user, ", ".join([user]), chars, "IFTT")
while True:
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(user, pwd)
server.sendmail(user, [ user ], message)
server.close()
break
except:
pass
commands = {
'shutdown' : Command(os.system, "shutdown -h now"),
'restart' : Command(os.system, "shutdown -r now"),
'wiki' : send_email
}
while True:
try:
with open(path) as f: # Read the file
data = f.read()
f.close()
os.remove(path) # Delete the file
try: # File from SMS contains more than one word
command, arg = data.strip().lower().split(' ', 1)
print command, arg
commands[command](arg)
except ValueError: # Contains only one word
command = data.strip().lower()
commands[command]()
except IOError:
time.sleep(30) # Sleep for 30 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment