Created
April 16, 2013 07:53
-
-
Save akivab/5394188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
import re | |
from google.appengine.ext import db | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp import template | |
from google.appengine.ext.webapp.util import run_wsgi_app | |
from twilio.rest import TwilioRestClient | |
from facts import catfacts | |
MAX_COUNT = 6 | |
def last_time(): | |
return datetime.datetime.now() - datetime.timedelta(hours=4) | |
class Numbers(db.Model): | |
number = db.StringProperty() | |
created = db.DateTimeProperty(auto_now_add=True) | |
last_touched = db.DateTimeProperty(auto_now=True) | |
count = db.IntegerProperty() | |
class MainPage(webapp.RequestHandler): | |
def get(self): | |
template_values = { 'fact': catfacts() } | |
path = os.path.join(os.path.dirname(__file__), 'index.html') | |
self.response.out.write(template.render(path, template_values)) | |
def post(self): | |
number=self.request.get('number').strip().replace(" ", "").replace("-","") | |
template_values = { 'return': False, 'number': number, 'error': False } | |
if re.search(r'^\d+$',number) and len(number) == 10: | |
seenBefore = Numbers.gql("WHERE number=:1",number).get() | |
if not seenBefore: | |
newbie = Numbers(number=number, count=1) | |
newbie.put() | |
self.call_people(newbie) | |
template_values['return'] = True | |
else: | |
template_values['return'] = False | |
else: | |
template_values['error'] = True | |
template_values['fact'] = catfacts() | |
path = os.path.join(os.path.dirname(__file__), 'index.html') | |
self.response.out.write(template.render(path, template_values)) | |
def call_people(self, newbie): | |
self.work_it(newbie) | |
people = Numbers.gql("WHERE last_touched < :1", last_time()).fetch(5) | |
for person in people: | |
self.work_it(person) | |
def call_person(self, person): | |
SIZE = 155 | |
fact = catfacts() | |
account = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4f" | |
token = "72xxxxxxxxxxxxxxxxxxxxxxxxxxxx3a" | |
client = TwilioRestClient(account, token) | |
num_messages = len(fact) / SIZE + 1 | |
if num_messages == 1: | |
client.sms.messages.create(to="+1"+person.number, from_="+18053228518", | |
body=fact ) | |
else: | |
for i in xrange(num_messages): | |
client.sms.messages.create(to="+1"+person.number, from_="+18053228518", | |
body="%d/%d: %s" % ((i+1), | |
num_messages, fact[i*155:((i+1)*155)] )) | |
person.count = person.count + 1 | |
if person.count >= MAX_COUNT: | |
db.delete(person) | |
return | |
person.put() | |
application = webapp.WSGIApplication([('/', MainPage)], debug=False) | |
def main(): | |
run_wsgi_app(application) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment