Skip to content

Commit

Permalink
Create index only if no exists
Browse files Browse the repository at this point in the history
  • Loading branch information
nonylene committed Dec 31, 2023
1 parent c062365 commit 4941e14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
13 changes: 12 additions & 1 deletion els/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import urllib.request
import urllib.parse
import urllib.request


class ElsClient:
def __init__(self, endpoint, index):
self.endpoint = endpoint
self.index = index

def get_index(self, index=None):
if not index: index = self.index

req = urllib.request.Request(
urllib.parse.urljoin(self.endpoint, "/" + self.index),
headers={'content-type': 'application/json'},
method="GET"
)
return self._request(req)

def add_index(self, index_json_string, index=None):
if not index: index = self.index

Expand Down
26 changes: 14 additions & 12 deletions pukiwiki-crawler.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import urllib.parse
import os
from functools import reduce
from datetime import date
import time
import argparse

import json
import glob
import urllib.request
import json
import os
import urllib.parse

from els.client import ElsClient
import urllib.request
from urllib.error import HTTPError

from config import pukiwiki as config
from els.client import ElsClient

client = ElsClient(config.ELASTIC_SEARCH_ENDPOINT, config.INDEX)

def add_index(args):
with open(config.INDEX_FILE) as f:
print(client.add_index(f.read()).read().decode("utf-8"))
# Add index if not exists
try:
client.get_index()
except HTTPError as e:
if e.status == 404:
with open(config.INDEX_FILE) as f:
print(client.add_index(f.read()).read().decode("utf-8"))
else:
raise


def delete_index(args):
Expand Down

0 comments on commit 4941e14

Please sign in to comment.