Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Reading from Ophan
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Rees committed Nov 16, 2012
1 parent 0fcfa7d commit ede6f60
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
22 changes: 4 additions & 18 deletions app.py
@@ -1,21 +1,7 @@
import webapp2
import jinja2
import os
import json
import logging
from urllib import quote, urlencode
from google.appengine.api import urlfetch
import handlers

jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))

class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')

template_values = {}

self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([('/', MainPage)],
app = webapp2.WSGIApplication([('/', handlers.MainPage),
('/api/most-viewed', handlers.MostViewed),
],
debug=True)
2 changes: 1 addition & 1 deletion app.yaml
@@ -1,4 +1,4 @@
application: your-app-id
application: gu-most-popular
version: 1
runtime: python27
api_version: 1
Expand Down
29 changes: 29 additions & 0 deletions handlers.py
@@ -0,0 +1,29 @@
import webapp2
import jinja2
import os
import json
import logging

from urllib import quote, urlencode
from google.appengine.api import urlfetch

import headers
import ophan

jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))

class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')

template_values = {}

self.response.out.write(template.render(template_values))

class MostViewed(webapp2.RequestHandler):
def get(self):
headers.json(self.response)

ophan_json = ophan.popular()
self.response.out.write(ophan_json)
5 changes: 4 additions & 1 deletion headers.py
Expand Up @@ -6,4 +6,7 @@ def set_cors_headers(response):

def set_cache_headers(response, cache_time):
response.headers['Cache-Control'] = 'public, max-age=' + str(cache_time) + ', stale-if-error=604800, stale-while-revalidate=60'
response.headers['Date'] = str(time.time())
response.headers['Date'] = str(time.time())

def json(response):
response.headers['Content-Type'] = 'application/javascript'
17 changes: 17 additions & 0 deletions ophan.py
@@ -0,0 +1,17 @@
from google.appengine.api.urlfetch import fetch
from google.appengine.api import memcache

import settings
import logging

client = memcache.Client()


def popular(section_id = None):
most_read_url = "http://%s/api/mostread" % client.get("OPHAN_HOST")

result = fetch(most_read_url)

if result.status_code == 200:
return result.content
return None
Empty file added settings.py
Empty file.
20 changes: 9 additions & 11 deletions templates/index.html
Expand Up @@ -6,26 +6,24 @@
<div class="fluid-wrap">
<div class="fluid-row">
<div class="col-12">
<h1 class="article-heading">Simple Python app for Google Appengine</h1>
<h1 class="article-heading">Most Popular on the Guardian</h1>

<blockquote>Making it easy to make simple apps</blockquote>
<blockquote>Surfacing what other people think, so you can think it to.</blockquote>
</div>
</div>

<div class="fluid-row margin-top">
<div class="col-6">
<h2 class="sub-heading">What is this?</h2>
<p>This is a template app to provide a basic scaffold for a Python app deployed on Google App Engine.</p>
<p>It is meant to provide a good basis for relatively uncomplicated micro apps.</p>
<h2 class="sub-heading">Endpoints</h2>
<ul>
<li><a href="/api/most-popular">Most popular</a></li>
<li><a href="/api/most-popular/football">Most popular in football</a></li>
</ul>
</div>
<div class="col-6">
<h2 class="sub-heading">How do I use it?</h2>

<ol>
<li>Add the git repo as a remote to your project repo and pull the template into your new project.</li>
<li>That's it.</li>
</ol>
<h2 class="sub-heading">Most popular data</h2>

<p>This is the most popular content according to Ophan.</p>

</div>
</div>
Expand Down

0 comments on commit ede6f60

Please sign in to comment.