pandora_cms/split_keywords.py
2018-05-08 12:32:17 +01:00

63 lines
1.6 KiB
Python
Executable file

#!/usr/bin/python3
import ox
import ox.web.auth
import json
import sys
url = 'http://131.111.144.26/api/'
api = ox.API(url)
site = '131.111.144.26'
try:
credentials = ox.web.auth.get(site)
except:
credentials = {}
print('Please provide your username and password for %s:' % site)
credentials['username'] = input('Username: ')
credentials['password'] = getpass.getpass('Password: ')
update = True
r = api.signin(**credentials)
if 'errors' in r.get('data', {}):
for kv in r['data']['errors'].items():
print('%s: %s' % kv)
sys.exit(1)
if update:
ox.web.auth.update(site, credentials)
old = []
for annotation in api.findAnnotations({
'query': {
'conditions': [{
'key': 'value',
'value': ', ',
'operator': '='
}, {
'key': 'layer',
'value': 'keywords',
'operator': '=='
}],
'operator': '&'
},
'keys': ['id', 'in', 'out', 'value', 'user', 'created'],
'range': [0, 50000]
})['data']['items']:
item = annotation['id'].split('/')[0]
for v in annotation['value'].split(', '):
v = v.strip()
a = {
'in': annotation['in'],
'out': annotation['out'],
'item': item,
'layer': 'keywords',
'value': v,
}
print(a)
r = api.addAnnotation(a)
print(r.get('status'))
api.removeAnnotation({'id': annotation['id']})
old.append(annotation)
with open('old.json', 'w') as fd:
json.dump(old, fd, indent=4, ensure_ascii=False)