add cleanup example
This commit is contained in:
parent
1940d6b3dc
commit
c0cff7c541
2 changed files with 48 additions and 0 deletions
47
cleanup_collection.py
Executable file
47
cleanup_collection.py
Executable file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/python3
|
||||
import sys
|
||||
import re
|
||||
import ox.api
|
||||
|
||||
if len(sys.argv) != 3:
|
||||
print('usage: %s <site> <collection>' % sys.argv[0])
|
||||
sys.exit(1)
|
||||
|
||||
site = sys.argv[1]
|
||||
collection = sys.argv[2]
|
||||
|
||||
api = ox.api.signin(site)
|
||||
|
||||
for d in api.findDocuments({
|
||||
'query': {
|
||||
'conditions': [{'key': 'collection', 'value': collection, 'operator': '=='}]
|
||||
},
|
||||
'keys': ['id', 'title'],
|
||||
'sort': [{'key': 'title', 'operator': '+'}],
|
||||
'range': [0, 10000]
|
||||
})['data']['items']:
|
||||
|
||||
document = api.getDocument({
|
||||
'id': d['id']
|
||||
})['data']
|
||||
# all edits here
|
||||
edit = {}
|
||||
title = document['title']
|
||||
|
||||
title = title.replace('_ocr', '')
|
||||
title = title.replace('_', ' ').strip()
|
||||
if title != document['title']:
|
||||
edit['title'] = title
|
||||
|
||||
year = re.compile('\d{4}').findall(document['title'])
|
||||
if year and not document.get('date'):
|
||||
document['date'] = edit['date'] = year[0]
|
||||
|
||||
if document.get('date') and document['date'] in title:
|
||||
title = title.replace(document['date'], '').strip()
|
||||
edit['title'] = title
|
||||
|
||||
if edit:
|
||||
edit['id'] = d['id']
|
||||
print('update', d['id'], d['title'])
|
||||
api.editDocument(edit)
|
|
@ -4,6 +4,7 @@ import ox.api
|
|||
|
||||
if len(sys.argv) != 4:
|
||||
print('usage: %s <site> <old title prefix> <new title prefix>' % sys.argv[0])
|
||||
sys.exit(1)
|
||||
|
||||
site = sys.argv[1]
|
||||
old_prefix = sys.argv[2]
|
||||
|
|
Loading…
Reference in a new issue