/
main.py
executable file
·177 lines (148 loc) · 5.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
import os
import sys
import logging
import datetime
from os.path import dirname, join as join_path
import wsgiref.handlers
# Why epylint.py doesnot recognize the webapp module?
# /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
APP_DIRECTORY = dirname(__file__)
sys.path.insert(0, join_path(APP_DIRECTORY, 'third_party'))
INVALID_ACCOUNT = 1
INVALID_FEED = 2
# I use relative imports...
from gae_twitter import GAETwitter
from models import Bot, bots_by_user, bots_to_update
import feedparser
def debug(message):
"""Debug function using logging module"""
logging.getLogger().debug(message)
def template_path(name):
"""Absolute path to a template file"""
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
return os.path.join(template_dir, (name + ".html"))
class MainHandler(webapp.RequestHandler):
"""Top page"""
def get(self):
user = users.get_current_user()
bots = bots_to_update()
template_values = {}
if user:
template_values = {
'username': user.nickname(),
'useremail': user.email(),
'logout_url': users.create_logout_url("/"),
'bots' : bots,
}
else:
template_values = {
'login_url': users.create_login_url("/"),
'bots' : bots,
}
path = template_path("index")
self.response.out.write(template.render(path, template_values))
# self.response.out.write("top page")
class BotCreateHandler(webapp.RequestHandler):
"""A handler that creates a Bot"""
def post(self):
"""Creates a bot"""
error_status = 0
user = users.get_current_user()
if not user:
self.response.out.write("not registered in google")
name = self.request.get('name')
password = self.request.get('password')
feed = self.request.get('feed')
# Verify the account information
gae_twitter = GAETwitter(username=name, password=password)
verify_result = gae_twitter.verify()
if (verify_result != True):
error_status = error_status + 1
# Verify the feed URL
d = feedparser.parse(feed)
if (d.bozo > 0):
error_status = error_status + 2
server_error = ""
if (error_status == 0):
bot = Bot(name=name, password=password, feed=feed)
bot.user = user
try:
bot.put()
except Exception, e:
error_status |= 4
server_error = str(e)
self.response.out.write(str(error_status) + server_error)
def get(self):
self.response.out.write("post me!")
class BotEditHandler(webapp.RequestHandler):
def post(self):
"""Edits a bot"""
key = self.request.get('key')
bot = db.get(db.Key(key))
try:
bot.update_myself(self.request)
except Exception, e:
self.response.out.write(str(e))
return
self.response.out.write("0")
def get(self):
key = self.request.get('key')
bot = Bot.get(key)
template_values = {
'bot': bot,
'logout_url': users.create_logout_url("/"),
}
path = template_path("edit")
self.response.out.write(template.render(path, template_values))
class BotShowHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
greeting = ("<a href=\"%s\">Sign in or register</a>." %
users.create_login_url("/"))
self.response.out.write("<html><body>%s</body></html>" % greeting)
return
bots = bots_by_user(user)
template_values = {
'bots': bots,
'logout_url': users.create_logout_url("/"),
}
path = template_path("show")
self.response.out.write(template.render(path, template_values))
class BotCronHandler(webapp.RequestHandler):
"""Cron job handler of bots"""
def get(self):
bots = bots_to_update()[:1]
feeds = ""
for bot in bots:
logging.debug(bot.name)
feeds = feeds + bot.feed + "\n"
post_count = bot.postfeedentry()
self.response.out.write(feeds + str(post_count))
class HatenaTestHandler(webapp.RequestHandler):
"""Test hatena antenna RSS response time"""
def get(self):
before = datetime.datetime.now()
feedparser.parse('http://www.hatena.ne.jp/suztomo/antenna.rss')
after = datetime.datetime.now()
self.response.out.write(after - before)
def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/create/', BotCreateHandler),
('/show/', BotShowHandler),
('/test_hatena/', HatenaTestHandler),
('/edit/', BotEditHandler),
('/cron/', BotCronHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()