Created
May 20, 2015 23:12
-
-
Save dvarrazzo/934610f3d748f1e18e9e to your computer and use it in GitHub Desktop.
Find pictures rated one star or more
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Find pictures rated one star or more and print their file names. | |
The output can be used e.g. with rsync --files-from | |
""" | |
import os | |
import sys | |
from glob import glob | |
from xml.etree import cElementTree as ET | |
import logging | |
logger = logging.getLogger() | |
def main(): | |
opt = parse_cmdline() | |
logging.basicConfig( | |
level=logging.INFO if not opt.verbose else logging.DEBUG, | |
format='%(levelname)s %(message)s') | |
for d in opt.dirs: | |
fns = glob(os.path.join(d, '*.xmp')) | |
for fn in fns: | |
logger.debug("parsing %s", fn) | |
try: | |
et = ET.parse(fn) | |
except Exception, e: | |
logger.warn("failed to parse %s: %s - %s", | |
fn, e.__class__.__name__, e) | |
continue | |
descr = et.getroot()[0][0] | |
rating = descr.attrib.get('{http://ns.adobe.com/xap/1.0/}Rating') | |
if rating is None: | |
logger.debug("skipping %s: no rating", fn) | |
continue | |
try: | |
rating = int(rating) | |
except ValueError: | |
logger.warn("bad rating in %s: '%s'", fn, rating) | |
continue | |
if rating <= 0: | |
logger.debug("skipping %s: rating is %s", fn, rating) | |
continue | |
try: | |
rawfn = descr.attrib[ | |
'{http://ns.adobe.com/camera-raw-settings/1.0/}RawFileName'] | |
except KeyError: | |
logger.warn("skipping %s: raw file name not found", fn) | |
continue | |
rawfn = os.path.join(os.path.dirname(fn), rawfn) | |
if not os.path.exists(rawfn): | |
logger.warn("skipping %s: raw file not found: %s", fn, rawfn) | |
continue | |
print fn | |
print rawfn | |
def parse_cmdline(): | |
from optparse import OptionParser | |
parser = OptionParser(usage="%prog [options] [DIR [...]]", | |
description=__doc__) | |
parser.add_option('--verbose', action='store_true', default=False, | |
help="talk more") | |
opt, args = parser.parse_args() | |
opt.dirs = args or ['.'] | |
return opt | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment