helper scripts to import documents
This commit is contained in:
parent
8f86180f8f
commit
314e465588
2 changed files with 134 additions and 0 deletions
81
import_raw.py
Executable file
81
import_raw.py
Executable file
|
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# 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
|
||||
|
||||
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)))
|
||||
cache = unicode(cache)
|
||||
jpg = glob(u'%s/*.jpg' % cache)
|
||||
if jpg:
|
||||
jpg = jpg[0]
|
||||
else:
|
||||
jpg = None
|
||||
for path in client.path(oshash):
|
||||
if os.path.exists(path):
|
||||
jpg = os.path.join(cache, u'%s.jpg' % os.path.basename(path).split('.')[0])
|
||||
convert_cr2(path, jpg)
|
||||
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'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = pandora_client.Client(os.path.expanduser('~/.ox/client.json'), False)
|
||||
import_raw(client)
|
||||
Loading…
Add table
Add a link
Reference in a new issue