-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
45 lines (35 loc) · 1.24 KB
/
app.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
38
39
40
41
42
43
44
45
import os
import elastic_transport
from elasticsearch import Elasticsearch
from queue import Queue
import json
import time
from scrapers.scraper import get_article
if __name__ == "__main__":
url = os.getenv("ARTICLE_URL", "https://en.wikipedia.org/wiki/Special:Random")
es_url = os.getenv("ES_URL", "http://127.0.0.1:9200")
# Connect to Elasticsearch cluster
es = Elasticsearch([es_url])
# Create a queue to hold the data
q = Queue()
while True:
# Check if the queue is empty
if q.empty():
print("Queue is empty, fetching new data...")
# Add new data to the queue
data = get_article(url)
if data:
q.put(data)
else:
# Get data from the queue
data = q.get()
try:
# Send data to Elasticsearch
res = es.index(
index="wikipedia", document=json.dumps(data), id=data["article_url"]
)
print(f"Data {res['result']} to Elasticsearch: \n", data)
except elastic_transport.ConnectionError:
print("Couldn't connected ES. Wait a bit, no need to rush")
# Sleep for a while
time.sleep(30)