Skip to content
Browse files

Repurpose app for showing dustball info

  • Loading branch information...
1 parent 0939d62 commit c2a86dd688b8b7877ba7dd11807977d781775516 @dustball dustball committed Mar 29, 2011
Showing with 43 additions and 18 deletions.
  1. +0 −1 index.yaml
  2. +43 −17 main.py
View
1 index.yaml
@@ -9,4 +9,3 @@ indexes:
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
-
View
60 main.py
@@ -1,27 +1,53 @@
#!/usr/bin/env python
-#
-# Copyright 2007 Google Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
+
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
+from google.appengine.api import memcache, users
+
+from keys import KEY
+CACHE_TIME = 60 * 60 # 1 hour
+
+import cookielib
+import os
+import urllib2
class MainHandler(webapp.RequestHandler):
def get(self):
- self.response.out.write('Hello world!')
-
+
+ user = users.get_current_user()
+ if not user:
+ login_url = users.create_login_url('/')
+ self.redirect(login_url)
+ return
+
+ page = memcache.get("page")
+ if page is not None:
+ self.response.out.write("<font color='white'>HIT</font>"+page)
+ else:
+ cj = cookielib.CookieJar()
+ opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
+ urllib2.install_opener(opener)
+ theurl = KEY
+ txdata = None
+ txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
+
+ try:
+ req = urllib2.Request(theurl, txdata, txheaders)
+ handle = urllib2.urlopen(req)
+ except IOError, e:
+ print 'We failed to open "%s".' % theurl
+ if hasattr(e, 'code'):
+ print 'We failed with error code - %s.' % e.code
+ elif hasattr(e, 'reason'):
+ print "The error object has the following 'reason' attribute :", e.reason
+ print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
+ sys.exit()
+ else:
+ page = handle.read()
+ memcache.add("page", page, CACHE_TIME)
+ self.response.out.write("<font color='white'>MISS</font>"+page)
+
def main():
application = webapp.WSGIApplication([('/', MainHandler)],

0 comments on commit c2a86dd

Please sign in to comment.
Something went wrong with that request. Please try again.