build(server): modernize backend with python http.server and requirements
This commit is contained in:
parent
5ed9fdf2c1
commit
57c729f991
3 changed files with 117 additions and 10 deletions
|
|
@ -1,20 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Dit script wordt gebruikt om de methodes in de scraper aan te roepen via JavaScript. Zo kan dit toch nog redelijk
|
||||
onafhankelijk gebeuren en kan er gemakkelijker ge-debugged worden.
|
||||
De bestandsextensie werd aangepast om te kunnen debuggen op zowel Windows als Linux.
|
||||
Dit script wordt gebruikt om de methodes in de scraper aan te roepen via CLI of CGI.
|
||||
De bestandsextensie werd behouden voor achterwaartse compatibiliteit.
|
||||
"""
|
||||
|
||||
import cgi
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from urllib.parse import parse_qs, unquote
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from scraper import run
|
||||
|
||||
parameters = cgi.FieldStorage()
|
||||
data = json.loads(parameters.getvalue('data'))
|
||||
antwoord = run(data['taal'], data['start'], data['einde'])
|
||||
|
||||
print("Content-Type: application/json")
|
||||
print() # Lege lijn na headers
|
||||
print(json.dumps(antwoord))
|
||||
def get_input_data():
|
||||
query_string = os.environ.get('QUERY_STRING', '')
|
||||
if query_string:
|
||||
params = parse_qs(query_string)
|
||||
if 'data' in params:
|
||||
return json.loads(unquote(params['data'][0]))
|
||||
|
||||
# Fallback to stdin or args
|
||||
if len(sys.argv) > 1:
|
||||
return json.loads(sys.argv[1])
|
||||
|
||||
return {'taal': 'en', 'start': 'Special:Random', 'einde': 'Philosophy'}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
data = get_input_data()
|
||||
antwoord = run(data.get('taal', 'en'), data.get('start', 'Special:Random'), data.get('einde', 'Philosophy'))
|
||||
except Exception as err:
|
||||
antwoord = {'error': f"Fout bij verwerken: {str(err)}"}
|
||||
|
||||
print("Content-Type: application/json")
|
||||
print() # Lege lijn na headers
|
||||
print(json.dumps(antwoord))
|
||||
|
|
|
|||
Reference in a new issue