#!/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+) no (\d+)', [' Vol ', ' no ']], ['Vol:(\d+) #(\d+)', ['Vol:', '#']], ): query = { 'conditions': [ {'key': 'title', 'operator': '=', 'value': fragment} for fragment in fragments ] + conditions, 'operator': '&' } for doc in api.findDocuments({ 'query': query, 'keys': ['id', 'title', 'volume', 'issue'], 'range': [0, 10000] })['data']['items']: if not doc.get('volume', ''): m = re.compile(regex).findall(doc['title']) if m: print(m[0], doc['id'], doc['title']) volume = m[0][0] issue = m[0][1] api.editDocument({ 'id': doc['id'], 'volume': volume, 'issue': issue, })