PyJSONProxy

simple proxy and scraper
git clone https://git.ce9e.org/PyJSONProxy.git

commit
f20e16c35a8763f43e001e39c06f371010feee60
parent
66cba37b0d1a77a6b24d9afa2521593ee87de39a
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2015-02-05 16:01
autodoc

Diffstat

M jsonproxy/api.py 38 +++++++++++++++++++++++++++++++++++++-
A jsonproxy/templates/index.html 33 +++++++++++++++++++++++++++++++++

2 files changed, 70 insertions, 1 deletions


diff --git a/jsonproxy/api.py b/jsonproxy/api.py

@@ -11,6 +11,7 @@ from flask import current_app
   11    11 from flask import abort
   12    12 from flask import jsonify
   13    13 from flask import make_response
   -1    14 from flask import render_template
   14    15 
   15    16 from bs4 import BeautifulSoup
   16    17 
@@ -29,9 +30,44 @@ def get_attribute(html, selector):
   29    30 	return get_attribute_list(html, selector)[0]
   30    31 
   31    32 
   -1    33 def _doc(endpoint):
   -1    34 	config = current_app.config['ENDPOINTS'][endpoint]
   -1    35 	url_doc = 'url of the scraped page'
   -1    36 
   -1    37 	data = {
   -1    38 		'title': endpoint,
   -1    39 		'doc': config.get('doc', ''),
   -1    40 		'type': config.get('type', 'proxy'),
   -1    41 		'fields': [],
   -1    42 	}
   -1    43 
   -1    44 	if config['type'] == 'scrape_item':
   -1    45 		fields_doc = config.get('fields_doc', {})
   -1    46 		data['fields'].append(('url', url_doc))
   -1    47 		for key in config['fields']:
   -1    48 			doc = fields_doc.get(key, '')
   -1    49 			data['fields'].append((key, doc))
   -1    50 
   -1    51 	if config['type'] == 'scrape_list':
   -1    52 		data['fields'] = [
   -1    53 			('url', url_doc),
   -1    54 			('l', 'list of results'),
   -1    55 		]
   -1    56 
   -1    57 	return data
   -1    58 
   -1    59 
   32    60 @api.route('/', methods=['GET'])
   33    61 def main():
   34    -1 	return jsonify()
   -1    62 	data = [_doc(endpoint) for endpoint in current_app.config['ENDPOINTS']]
   -1    63 	return render_template('index.html', endpoints=data)
   -1    64 
   -1    65 
   -1    66 @api.route('/<endpoint>/', methods=['GET'])
   -1    67 def doc(endpoint):
   -1    68 	if endpoint not in current_app.config['ENDPOINTS']:
   -1    69 		abort(404)
   -1    70 	return render_template('index.html', endpoints=[_doc(endpoint)])
   35    71 
   36    72 
   37    73 @api.route('/<endpoint>/<path:path>', methods=['GET'])

diff --git a/jsonproxy/templates/index.html b/jsonproxy/templates/index.html

@@ -0,0 +1,33 @@
   -1     1 <!DOCTYPE html>
   -1     2 <html xmlns="http://www.w3.org/1999/xhtml">
   -1     3 	<head>
   -1     4 		<meta charset="UTF-8">
   -1     5 		<title>jsonproxy</title>
   -1     6 	</head>
   -1     7 	<body>
   -1     8 		{% for endpoint in endpoints %}
   -1     9 		<section>
   -1    10 			<h2>{{ endpoint.title }} ({{ endpoint.type }})</h2>
   -1    11 			<p>{{ endpoint.doc }}</p>
   -1    12 			{% if endpoint.fields %}
   -1    13 			<table>
   -1    14 				<thead>
   -1    15 					<tr>
   -1    16 						<th>name</th>
   -1    17 						<th>description</th>
   -1    18 					</tr>
   -1    19 				</thead>
   -1    20 				<tbody>
   -1    21 					{% for name, doc in endpoint.fields %}
   -1    22 					<tr>
   -1    23 						<td>{{ name }}</td>
   -1    24 						<td>{{ doc }}</td>
   -1    25 					</tr>
   -1    26 					{% endfor %}
   -1    27 				</tbody>
   -1    28 			</table>
   -1    29 			{% endif %}
   -1    30 		</section>
   -1    31 		{% endfor %}
   -1    32 	</body>
   -1    33 </html>