pandora_cms/split_keywords.py

65 lines
1.6 KiB
Python
Raw Normal View History

2018-05-08 11:31:41 +00:00
#!/usr/bin/python3
2018-05-09 15:46:04 +00:00
import getpass
2018-05-08 11:31:41 +00:00
import json
import sys
2018-05-09 15:46:04 +00:00
import ox
import ox.web.auth
2018-05-08 11:31:41 +00:00
2018-05-09 15:46:04 +00:00
site = 'pandora.cinemusespace.com'
api = ox.API('https://%s/api/' % site)
update = False
2018-05-08 11:31:41 +00:00
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)
2018-05-09 15:46:04 +00:00
2018-05-08 11:31:41 +00:00
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)