Skip to content

Commit

Permalink
Add ability to serve JSON, also added a small example HTML page to il…
Browse files Browse the repository at this point in the history
…lustrate how to use it.
  • Loading branch information
pmoor committed Mar 12, 2011
1 parent 950bbe6 commit 96bd8f4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 28 deletions.
6 changes: 5 additions & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ api_version: 1

handlers:
- url: /user/([^/]+)
script: user.py
script: main.py

- url: /json/([^/]+)
script: main.py

- url: /
script: root.py
Expand All @@ -37,3 +40,4 @@ skip_files:
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
- ^google_appengine$
- ^examples$
62 changes: 62 additions & 0 deletions examples/slideshow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!--
rndpic - an App Engine app to display random pictures from Picasa Web.
Copyright (C) 2011 Patrick Moor <patrick@moor.ws>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<html>
<head>
<title>sample slideshow using rndpic.appspot.com</title>
</head>
<body>
<p>This is a sample slideshow using <a href="http://rndpic.appspot.com">rndpic.appspot.com</a>.</p>
<div>
<a href="#" id="rndpic_link"><img id="rndpic_image"/></a>
</div>

<script type="text/javascript">

function loadNewImage() {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var image = eval("(" + xmlhttp.responseText + ")");

function switchImage() {
var linkTag = document.getElementById("rndpic_link");
var imageTag = document.getElementById("rndpic_image");
linkTag.href = image.target_url;
imageTag.width = image.width;
imageTag.height = image.height;
imageTag.src = image.thumbnail_url;
}

var preloadedThumbnail = new Image();
preloadedThumbnail.src = image.thumbnail_url;
preloadedThumbnail.onload = switchImage

// reload every 5 seconds
window.setTimeout("loadNewImage()", 5 * 1000);
}
};
xmlhttp.open("GET", "http://rndpic.appspot.com/json/patrick.moor?size=400u", true);
xmlhttp.send();
}

loadNewImage();

</script>

</body>
</html>
39 changes: 39 additions & 0 deletions json_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# rndpic - an App Engine app to display random pictures from Picasa Web.
# Copyright (C) 2011 Patrick Moor <patrick@moor.ws>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import webapp
from django.utils import simplejson

class JsonHandler(webapp.RequestHandler):

def __init__(self, picker):
self._picker = picker

def get(self, user_name):
self.response.headers["Content-Type"] = "application/json"
self.response.headers["Access-Control-Allow-Origin"] = "*"
size = self.request.get("size", "200u")
picture = self._picker.Pick(user_name, size)
if picture:
content = {
"height": picture.GetHeight(),
"width": picture.GetWidth(),
"thumbnail_url": picture.GetThumbnailUrl(),
"target_url": picture.GetLink(),
}
simplejson.dump(content, self.response.out)
else:
self.response.set_status(204)
53 changes: 53 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# rndpic - an App Engine app to display random pictures from Picasa Web.
# Copyright (C) 2011 Patrick Moor <patrick@moor.ws>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from feed import FeedRepository
from album_repository import AlbumRepository
from picker import RandomPicturePicker

from user_handler import UserHandler
from json_handler import JsonHandler


repository = AlbumRepository(FeedRepository())
picker = RandomPicturePicker(repository)


def _ConstructUserHandler():
return UserHandler(picker)


def _ConstructJsonHandler():
return JsonHandler(picker)


application = webapp.WSGIApplication(
[
(r'/user/([^/]+)', _ConstructUserHandler),
(r'/json/([^/]+)', _ConstructJsonHandler),
],
debug=False)


def main():
run_wsgi_app(application)


if __name__ == "__main__":
main()
32 changes: 5 additions & 27 deletions user.py → user_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# rndpic - an App Engine app to display random pictures from Picasa Web.
# Copyright (C) 2010 Patrick Moor <patrick@moor.ws>
# Copyright (C) 2011 Patrick Moor <patrick@moor.ws>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -15,23 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from feed import FeedRepository
from album_repository import AlbumRepository
from picker import RandomPicturePicker


repository = AlbumRepository(FeedRepository())
picker = RandomPicturePicker(repository)


class UserHandler(webapp.RequestHandler):

def __init__(self, picker):
self._picker = picker

def get(self, user_name):
size = self.request.get("size", "200u")
link = bool(int(self.request.get("link", 1)))
picture = picker.Pick(user_name, size)
picture = self._picker.Pick(user_name, size)
if picture:
html = """<img src="%s" width="%d" height="%d" id="rndpic-img"/>""" % (
picture.GetThumbnailUrl(),
Expand All @@ -45,18 +38,3 @@ def get(self, user_name):
self.response.out.write("document.write('%s');" % html)
else:
self.response.set_status(204)




application = webapp.WSGIApplication(
[(r'/user/([^/]+)', UserHandler)],
debug=False)


def main():
run_wsgi_app(application)


if __name__ == "__main__":
main()

0 comments on commit 96bd8f4

Please sign in to comment.