Created
April 1, 2011 13:17
-
-
Save r-plus/898125 to your computer and use it in GitHub Desktop.
@freeappnow の最新1個のTLからプレゼントの開始Tweetかどうかを判定してメールするGoogleAppEngine用スクリプト
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import wsgiref.handlers | |
import xml.etree.cElementTree as etree | |
from google.appengine.ext import webapp | |
from google.appengine.ext import db | |
from google.appengine.api import mail | |
from google.appengine.api import urlfetch | |
Config = { | |
'Sender' : 'XXXXXXXX@gmail.com', | |
'ToAddr' : 'XXXXXXXX@gmail.com', | |
} | |
class Appdb(db.Model): | |
appname = db.StringProperty() | |
applimit = db.StringProperty() | |
class MainPage(webapp.RequestHandler): | |
def get(self): | |
self.response.headers['Content-Type'] = 'text/plain;charset=UTF-8' | |
#load and delete prev data | |
datas = Appdb.all().fetch(1,0) | |
if datas: | |
prevName = datas[0].appname | |
prevLimit = datas[0].applimit | |
datas[0].delete() | |
else: | |
prevName = "NULL" | |
prevLimit = "NULL" | |
url = 'http://twitter.com/statuses/user_timeline/freeappnow.xml?count=1' | |
xml = urlfetch.fetch(url).content | |
dom = etree.fromstring(xml) | |
for st in dom.findall('./status'): | |
msg = st.findtext('text')#.encode('utf-8') | |
if u'「' in msg: | |
nowName = msg[msg.find(u'「')+1:msg.find(u'」')] | |
else: | |
nowName = 'NULL' | |
if u'先着' in msg: | |
nowLimit = msg[msg.find(u'先着')+1:msg.find('名全員')] | |
else: | |
nowLimit = 'NULL' | |
obj = Appdb(appname=nowName,applimit=nowLimit) | |
obj.put() | |
#send mail | |
if prevName != nowName and u'【プレゼント】' in msg: | |
subject_text = '[FreeAppNow] %s' % nowName | |
body_text = 'App:%s Limit:%s' % (nowName, nowLimit) | |
mail.send_mail(sender = Config['Sender'], | |
to = Config['ToAddr'], | |
subject = subject_text, | |
body = body_text, | |
) | |
def main(): | |
application = webapp.WSGIApplication([('/', MainPage)], | |
debug=True) | |
wsgiref.handlers.CGIHandler().run(application) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment