plutopluto

git clone https://git.ce9e.org/plutopluto.git

commit
2d01cab7b13b8a3ae693629edc3e473945ae513b
parent
b49d7bf2e0f99279e8fc8ee947b7ea178daaf82e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-12-21 10:06
switch to aiohttp

Diffstat

M plutopluto/__init__.py 59 ++++++++++++++++++++++++++++++-----------------------------
M setup.py 1 -

2 files changed, 30 insertions, 30 deletions


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

@@ -2,6 +2,8 @@
    2     2 
    3     3 import argparse
    4     4 import datetime
   -1     5 import json
   -1     6 import logging
    5     7 import sys
    6     8 from pathlib import Path
    7     9 from time import mktime
@@ -12,14 +14,13 @@ import aiohttp
   12    14 import feedparser
   13    15 from aiohttp import web
   14    16 from feedparser.sanitizer import _sanitize_html
   15    -1 from flask import Flask
   16    -1 from flask import request
   17    -1 from flask import jsonify
   18    -1 from flask import abort
   19    17 
   20    18 __version__ = '1.2.0'
   21    19 
   22    -1 app = Flask(__name__)
   -1    20 logger = logging.getLogger(__name__)
   -1    21 
   -1    22 BASE_DIR = Path(__file__).parent
   -1    23 URLS = []
   23    24 
   24    25 
   25    26 async def fetch(url, *, raw=False, **kwargs):
@@ -127,12 +128,11 @@ async def parse_activity_stream(url):
  127   128     }
  128   129 
  129   130 
  130    -1 @app.route('/parse', methods=['GET'])
  131    -1 async def _parse():
  132    -1     if 'url' not in request.values:
  133    -1         abort(400)
   -1   131 async def route_parse(request):
   -1   132     if 'url' not in request.query:
   -1   133         raise web.HTTPBadRequest
  134   134 
  135    -1     url = request.values['url']
   -1   135     url = request.query['url']
  136   136 
  137   137     try:
  138   138         if 'outbox' in url:
@@ -140,23 +140,21 @@ async def _parse():
  140   140         else:
  141   141             data = await parse_feed(url)
  142   142     except Exception as err:
  143    -1         app.logger.warning('%s: %s' % (url, err))
  144    -1         abort(500)
   -1   143         logger.warning('%s: %s' % (url, err))
   -1   144         raise web.HTTPInternalServerError from err
  145   145 
  146    -1     return jsonify(data)
   -1   146     body = json.dumps(data, sort_keys=True)
   -1   147     return web.Response(body=body, content_type='application/json')
  147   148 
  148   149 
  149    -1 @app.route('/', methods=['GET'])
  150    -1 def index():
  151    -1     with open(os.path.join(app.root_path, 'index.html')) as fh:
  152    -1         return fh.read()
   -1   150 def route_index(request):
   -1   151     with open(BASE_DIR / 'index.html') as fh:
   -1   152         return web.Response(body=fh.read(), content_type='text/html')
  153   153 
  154   154 
  155    -1 @app.route('/config', methods=['GET'])
  156    -1 def config():
  157    -1     return jsonify({
  158    -1         'urls': app.config['URLS']
  159    -1     })
   -1   155 def route_config(request):
   -1   156     body = json.dumps({'urls': URLS}, sort_keys=True)
   -1   157     return web.Response(body=body, content_type='application/json')
  160   158 
  161   159 
  162   160 def parse_config(path):
@@ -188,22 +186,25 @@ def get_config(args, name='.plutopluto.cfg'):
  188   186 def main():
  189   187     parser = argparse.ArgumentParser(description='simple feed aggregator')
  190   188     parser.add_argument('--version', '-V', action='version', version=__version__)
  191    -1     parser.add_argument('-d', '--debug', action='store_true')
  192   189     parser.add_argument('-c', '--config', metavar='FILE', type=Path)
  193   190     parser.add_argument('urls', metavar='URL', nargs='*',
  194   191         help='full feed url, optionally with a {page} placeholder')
  195   192     parser.add_argument('--port', type=int, default=8000)
  196   193     args = parser.parse_args()
  197   194 
  198    -1     app.debug = args.debug
  199    -1     app.config['URLS'] = get_config(args)
  200    -1 
  201    -1     if not app.config['URLS']:
  202    -1         print("Error: No urls provided")
   -1   195     global URLS
   -1   196     URLS = get_config(args)
   -1   197     if not URLS:
   -1   198         print('Error: No urls provided')
  203   199         parser.print_usage()
  204   200         sys.exit(1)
  205   201 
  206    -1     app.run('localhost', args.port)
   -1   202     app = web.Application()
   -1   203     app.router.add_static('/static', BASE_DIR / 'static')
   -1   204     app.router.add_route('GET', '', route_index)
   -1   205     app.router.add_route('GET', '/parse', route_parse)
   -1   206     app.router.add_route('GET', '/config', route_config)
   -1   207     web.run_app(app, host='localhost', port=args.port)
  207   208 
  208   209 
  209   210 if __name__ == '__main__':

diff --git a/setup.py b/setup.py

@@ -24,7 +24,6 @@ setup(
   24    24     include_package_data=True,
   25    25     install_requires=[
   26    26         'aiohttp',
   27    -1         'flask[async]',
   28    27         'feedparser',
   29    28     ],
   30    29     entry_points={'console_scripts': [