1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2025-01-31 19:51:34 +01:00

API server implementation using http.server

This commit is contained in:
Mike Fährmann 2019-01-13 13:26:53 +01:00
parent 89df37a173
commit 8662e72bdd
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 77 additions and 0 deletions

View File

@ -258,6 +258,9 @@ def main():
if hasattr(extr, "test") and extr.test:
print("Example :", extr.test[0][0])
print()
elif args.start_server:
from . import server
server.run()
else:
if not args.urls and not args.inputfile:
parser.error(

View File

@ -93,6 +93,11 @@ def build_parser():
metavar="URL", action=ConfigAction, dest="proxy",
help="Use the specified proxy",
)
general.add_argument(
"--server",
dest="start_server", action="store_true",
help="Start server",
)
output = parser.add_argument_group("Output Options")
output.add_argument(

69
gallery_dl/server.py Normal file
View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
from http.server import HTTPServer, BaseHTTPRequestHandler
from . import extractor, config, text
import json
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
"""Handle GET requests"""
path, _, query = self.path.partition("?")
args = text.parse_query(query)
if path == "/api/json":
self._send_json(self.api_json(args))
else:
self._send_json({"error": "Not Found"}, 404)
def api_json(self, args):
urls = args.get("url", "").split(",")
data = [self._extract_data(url) for url in urls]
return {"data": data, "urls": urls}
def _extract_data(self, url):
extr = extractor.find(url)
if not extr:
return url, None, {"error": "No extractor found"}
data = []
try:
for message in extr:
data.append([
value.copy() if hasattr(value, "copy") else value
for value in message
])
except Exception as exc:
data.append((exc.__class__.__name__, str(exc)))
return url, data, None
def _send_json(self, data, status=200):
"""Send 'data' as JSON response"""
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data, separators=(',', ':')).encode())
def run():
bind = config.get(("server", "bind"), "127.0.0.1")
port = config.get(("server", "port"), 6412)
httpd = HTTPServer((bind, port), RequestHandler)
try:
httpd.serve_forever()
except BaseException:
httpd.server_close()
raise
if __name__ == '__main__':
run()