pandora_leftovers/cleanup/add_volume.py
2020-06-28 10:23:21 +02:00

42 lines
1.1 KiB
Python
Executable file

#!/usr/bin/python3
import re
import sys
import ox
import ox.api
site = 'archive.leftove.rs'
api = ox.api.signin('https://%s/api/' % site)
if len(sys.argv) > 1:
collection = sys.argv[1]
conditions = [
{'key': 'collection', 'operator': '==', 'value': collection}
]
else:
conditions = []
for regex, fragments in (
['Vol. \d+, No. \d+', ['Vol.', 'No.']],
['Vol \d+ no \d+', [' Vol ', ' no ']],
['Vol:\d+ #\d+', ['Vol:', '#']],
):
for doc in api.findDocuments({
'query': {
'conditions': [
{'key': 'title', 'operator': '=', 'value': fragment}
for fragment in fragments
] + conditions,
'operator': '&'
},
'keys': ['id', 'title', 'volume'],
'range': [0, 10000]
})['data']['items']:
if 'Vol' not in doc.get('volume', ''):
m = re.compile(regex).findall(doc['title'])
if m:
print(m[0], doc['id'], doc['title'])
api.editDocument({
'id': doc['id'],
'volume': m[0].strip()
})