PyJSONProxy

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

commit
34413c3e322b6d4de7b17ea4665e9f6019f0e72e
parent
54b647a34371e49896344dcb358da7c8e3536fc5
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2017-06-03 09:41
adapt to python3.5

Diffstat

M jsonproxy/__init__.py 26 +++++++++++++-------------

1 files changed, 13 insertions, 13 deletions


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

@@ -1,4 +1,3 @@
    1    -1 import asyncio
    2     1 import os
    3     2 import sys
    4     3 
@@ -29,30 +28,31 @@ def async_cache(maxsize=128):
   29    28 	cache = {}
   30    29 
   31    30 	def decorator(fn):
   32    -1 		def wrapper(*args):
   -1    31 		async def wrapper(*args):
   33    32 			key = ':'.join(args)
   34    33 			if key not in cache:
   35    34 				if len(cache) >= maxsize:
   36    35 					del cache[cache.keys().next()]
   37    -1 				cache[key] = yield from fn(*args)
   -1    36 				cache[key] = await fn(*args)
   38    37 			return cache[key]
   39    38 		return wrapper
   40    39 	return decorator
   41    40 
   42    41 
   43    42 @async_cache()
   44    -1 def _request(method, url):
   -1    43 async def _request(method, url):
   45    44 	app.logger.info('{}:{}'.format(method, url))
   46    -1 	response = yield from aiohttp.request(method, url)
   47    -1 	if response.status != 200:
   48    -1 		abort(response.status)
   49    -1 	else:
   50    -1 		return response
   -1    45 	async with aiohttp.request(method, url) as response:
   -1    46 		if response.status != 200:
   -1    47 			abort(response.status)
   -1    48 		else:
   -1    49 			# get response before closing the connection
   -1    50 			await response.read()
   -1    51 			return response
   51    52 
   52    53 
   53    54 @app.route('/{endpoint}/{path:.+}', methods=['GET', 'HEAD', 'OPTIONS'])
   54    -1 @asyncio.coroutine
   55    -1 def handle(request):
   -1    55 async def handle(request):
   56    56 	endpoint = request.match_info['endpoint']
   57    57 
   58    58 	config = get_config(endpoint)
@@ -64,8 +64,8 @@ def handle(request):
   64    64 	if request.query_string:
   65    65 		url += '?' + request.query_string
   66    66 
   67    -1 	remote = yield from _request(request.method, url)
   68    -1 	body = yield from remote.read()
   -1    67 	remote = await _request(request.method, url)
   -1    68 	body = await remote.read()
   69    69 
   70    70 	if 'fields' in config and request.method == 'GET':
   71    71 		response = jsonify(scrape(url, body, config), status=remote.status)