-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathindex-sites.py
69 lines (54 loc) · 2 KB
/
index-sites.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/env python
import logging, os, sys
import jekyll, ijsite, tsutil
import tutorials
logger = logging.getLogger('indexer')
def load_site(siteroot):
if jekyll.is_jekyll_site(siteroot):
return jekyll.load_jekyll_site(siteroot)
if ijsite.is_imagej_website(siteroot):
return ijsite.load_site(siteroot)
if tutorials.is_imagej_tutorials(siteroot):
return tutorials.load_imagej_tutorials(siteroot)
return None
def load_sites(sites):
logger.info('Loading documents...')
documents = []
for siteid, siteroot in sites.items():
docs = load_site(siteroot)
if docs:
for doc in docs:
doc['siteid'] = siteid
documents.extend(docs)
logger.info(f'{len(documents)} documents loaded.')
return documents
def index_documents(collection, documents):
client = tsutil.connect()
if client is None:
logger.info('No typesense credentials available.')
return
logger.info('Connected to typesense.')
created = tsutil.create(client, collection, documents, force=True)
logger.info('Created new collection.' if created else 'Updating existing collection.')
logger.info(f'Indexing {len(documents)} documents...')
tsutil.update_index(client, collection, documents)
logger.info('Done!')
def main(args):
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
logging.root.setLevel(logging.INFO)
if len(args) == 1:
collection = 'imagej-wiki'
sites = {
'imagej.net': os.path.join(os.path.dirname(args[0]), '..', '..'),
'imagej.nih.gov/ij': '/var/www/mirror.imagej.net',
}
elif len(args) >= 3:
collection = args[1]
siteroot = args[2]
else:
print('Usage: index-sites.py [<collection-name> <site-id:site-root> [<another-site-id:another-site-root>...]]')
sys.exit(1)
documents = load_sites(sites)
index_documents(collection, documents)
if __name__ == '__main__':
main(sys.argv)