-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
37 lines (29 loc) · 1.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from flask import Flask, request, jsonify
from modules.parser import ArticleParser
from threading import Thread
import queue
app = Flask(__name__)
def process_article(url, result_queue):
try:
article_parser = ArticleParser(url)
result_queue.put({'article': article_parser.article_data})
except Exception as e:
result_queue.put({'error': str(e)})
@app.route('/health')
def healthcheck():
return jsonify({'ok': True})
@app.route('/', methods=['POST'])
def scrape():
data = request.get_json()
if 'url' not in data or not isinstance(data['url'], str):
return jsonify({'error': 'Invalid JSON format or missing URL'}), 400
result_queue = queue.Queue()
thread = Thread(target=process_article, args=(data['url'], result_queue))
thread.start()
thread.join() # Wait for the thread to finish execution
result = result_queue.get()
if 'error' in result:
return jsonify({'error': result['error']}), 500
return jsonify(result), 200
if __name__ == '__main__':
app.run(debug=True, port=7732, threaded=True)