2017-05-31 11:36:08 +00:00
|
|
|
#!/usr/bin/python3
|
2016-11-05 15:13:13 +00:00
|
|
|
#
|
|
|
|
# apt install convert ufraw-batch
|
|
|
|
#
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
from glob import glob
|
|
|
|
|
|
|
|
import ox
|
|
|
|
import pandora_client
|
|
|
|
from pandora_client.utils import hash_prefix
|
|
|
|
|
|
|
|
def convert_raw(raw, jpg):
|
|
|
|
ox.makedirs(os.path.dirname(jpg))
|
|
|
|
cmd = ['convert', raw, jpg]
|
|
|
|
print(' '.join(cmd))
|
|
|
|
subprocess.call(cmd)
|
|
|
|
|
|
|
|
def get_raw(client):
|
|
|
|
files = []
|
|
|
|
for volume in client.active_volumes():
|
|
|
|
query = {
|
|
|
|
'conditions': [
|
|
|
|
{'key': 'list', 'value': volume, 'operator': '=='},
|
|
|
|
{
|
|
|
|
'conditions': [
|
|
|
|
{'key': 'filename', 'value': '.cr2', 'operator': ''},
|
|
|
|
{'key': 'filename', 'value': '.nef', 'operator': ''},
|
|
|
|
],
|
|
|
|
'operator': '|'
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'operator': '&'
|
|
|
|
}
|
|
|
|
n = client.api.findMedia({'query': query})['data']['items']
|
|
|
|
if n:
|
|
|
|
o = 0
|
|
|
|
chunk = 5000
|
|
|
|
while o < n:
|
|
|
|
files += client.api.findMedia({
|
|
|
|
'query': query,
|
|
|
|
'keys': ['item', 'id', 'extension'],
|
|
|
|
'range': [o, o+chunk]
|
|
|
|
})['data']['items']
|
|
|
|
o += chunk
|
|
|
|
files = [f for f in files if f['extension'].lower() in ('cr2', 'nef')]
|
|
|
|
return files
|
|
|
|
|
2017-05-31 11:36:08 +00:00
|
|
|
def raw_exists(client, oshash):
|
|
|
|
r = client.api.findDocuments({
|
|
|
|
'keys': ['id', 'description'],
|
|
|
|
'query': {
|
|
|
|
'conditions': [
|
|
|
|
{'key': 'description', 'value': '[%s]' % oshash, 'operator': '='}
|
|
|
|
],
|
|
|
|
'operator': '&'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if r['data']['items'] and \
|
|
|
|
'Converted from' in r['data']['items'][0]['description']:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-11-05 15:13:13 +00:00
|
|
|
def import_raw(client):
|
|
|
|
files = get_raw(client)
|
|
|
|
print('got', len(files), 'raw files')
|
|
|
|
for f in files:
|
|
|
|
oshash = f['id']
|
|
|
|
print(oshash)
|
|
|
|
cache = os.path.join(client.media_cache(), os.path.join(*hash_prefix(oshash)))
|
2017-05-31 11:36:08 +00:00
|
|
|
if isinstance(cache, bytes):
|
|
|
|
cache = cache.decode()
|
|
|
|
jpg = glob('%s/*.jpg' % cache)
|
2016-11-05 15:13:13 +00:00
|
|
|
if jpg:
|
2017-05-31 11:36:08 +00:00
|
|
|
continue
|
|
|
|
elif raw_exists(client, oshash):
|
|
|
|
continue
|
2016-11-05 15:13:13 +00:00
|
|
|
else:
|
|
|
|
jpg = None
|
|
|
|
for path in client.path(oshash):
|
|
|
|
if os.path.exists(path):
|
2017-05-31 11:36:08 +00:00
|
|
|
jpg = os.path.join(cache, '%s.jpg' % os.path.basename(path).split('.')[0])
|
2016-11-06 13:41:38 +00:00
|
|
|
convert_raw(path, jpg)
|
2016-11-05 15:13:13 +00:00
|
|
|
break
|
|
|
|
if jpg and os.path.exists(jpg):
|
|
|
|
r = client._add_document(jpg, f['item'])
|
|
|
|
doc = client.find_document(ox.oshash(jpg))
|
|
|
|
if doc:
|
|
|
|
client.api.editDocument({
|
|
|
|
'id': doc,
|
|
|
|
'description': 'Converted from %s [%s]' % (os.path.basename(path), oshash)
|
|
|
|
})
|
|
|
|
print('added', oshash, 'to', f['item'])
|
|
|
|
|
2017-05-31 11:36:08 +00:00
|
|
|
|
2016-11-05 15:13:13 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
client = pandora_client.Client(os.path.expanduser('~/.ox/client.json'), False)
|
|
|
|
import_raw(client)
|