Skip to content

Commit

Permalink
bottle service
Browse files Browse the repository at this point in the history
  • Loading branch information
slaporte committed May 14, 2012
1 parent 2225adb commit 0907e44
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 10 deletions.
41 changes: 41 additions & 0 deletions bottle_jsonp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Author: Samat Jain <http://samat.org/>
# License: MIT (same as Bottle)

import bottle

from bottle import request, response, route
from bottle import install, uninstall

from json import dumps as json_dumps


class JSONAPIPlugin(object):
name = 'jsonapi'
api = 1

def __init__(self, json_dumps=json_dumps):
uninstall('json')
self.json_dumps = json_dumps

def apply(self, callback, context):
dumps = self.json_dumps
if not dumps: return callback
def wrapper(*a, **ka):
r = callback(*a, **ka)

# Attempt to serialize, raises exception on failure
json_response = dumps(r)

# Set content type only if serialization succesful
response.content_type = 'application/json'

# Wrap in callback function for JSONP
callback_function = request.query.get('callback')
if callback_function:
json_response = ''.join([callback_function, '(', json_response, ')'])

return json_response
return wrapper


install(JSONAPIPlugin())
32 changes: 30 additions & 2 deletions dabnabbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from collections import namedtuple

API_URL = "http://en.wikipedia.org/w/api.php"
API_URL = 'http://en.wikipedia.org/w/api.php'
EDIT_SUMMARY = 'DAB link solved with disambiguity!'

class WikiException(Exception): pass

Expand Down Expand Up @@ -192,4 +193,31 @@ def get_random_dabblets(count=2):
page_ids = random.sample(get_dab_page_ids(count=count), count)
articles = get_articles(page_ids)
dabblets.extend(sum([get_dabblets(a) for a in articles], []))
return dabblets
return dabblets
'''
import re
def replace_nth(n, repl):
def replace(match, c=[0]):
c[0] += 1
return repl if c[0] == n else match.group(0)
return replace
def replace_dablet(dabblet, guess):
article_text = get_articles(page_id=dabblet.source_page['pageid'], parsed=False)
dab_title = dabblet.title
dab_postition = dabblet.source_order
if article_text.revisionid === dabblet.source_page['revisionid']:
return re.sub('\[\[' + title + '(.*){{Disambiguation needed.*}}, replace_nth(dab_postition, '[[' + guess + '\g<1>'), article_text.revisiontext)
else:
return 'error: the revids don't match'
def submit_solution(title, solution):
params = {'action': 'edit',
'format': 'json',
'title': title,
'text': solution,
'summary': EDIT_SUMMARY,
'token': '+\\'}
resp = api_req('query', params)
return resp
'''
18 changes: 13 additions & 5 deletions dabserver.py

Large diffs are not rendered by default.

70 changes: 67 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src='js/dab.js'></script>
<script type="text/javascript">

var global_timeout = 5000;

function random_int (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function do_query(url, complete_callback, kwargs) {
var all_kwargs = {
url: url,
type: 'get',
dataType: 'jsonp',
timeout: global_timeout,
success: function(data) {
console.log('successful ajax query to ' + url);
complete_callback(null, data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('failed query to ' + url);
complete_callback(errorThrown, null);
},
complete: function(jqXHR, textStatus) {
// TODO: error handling (jsonp doesn't get error() calls for a lot of errors)
},
};

for (var key in kwargs) {
if(kwargs.hasOwnProperty(key)) {
all_kwargs[key] = kwargs[key];
}
}
$.ajax(all_kwargs);
}

do_query('http://localhost:8080/fake/', page_setup);



function page_setup(err, data){
var rand_choice = random_int(0, data.dabs.length - 1);
console.log(rand_choice);
var dab = data.dabs[rand_choice];
var choices = dab.options,
page_title = dab.page_title,
dab_title = dab.title,
dab_context = dab.context;

$('#page_title').html(page_title);
$('#phrase').html(dab_title);
$('#context').html(dab_context);
$('span:contains("disambiguation")"').parents('sup').hide();
for(var i = 0; i < choices.length; i++) {
$('#choices').append('<li>' + choices[i].text + '</li>');
}
}
</script>
<style>
.dab-link {
background-color: yellow;
}
</style>
</head>
<body>
<h1>DAB game</h1>
<p>Page: <span id='page_title'></span></p>
<p>Dabblet: <span id='phrase'></span></p>
<h2>context</h2>
<div id='phrase'></div>
<div id='inst'></div>
<ul id='as'></ul>
<div id='context'></div>
<ul id='choices'></ul>
</body>
</html>

0 comments on commit 0907e44

Please sign in to comment.