Added {camera} bindings and allow associative lookup for enum properties for {camera} and {location} bindings
This commit is contained in:
parent
2a5ebbc7f1
commit
d6b33e5f0f
|
@ -0,0 +1,171 @@
|
|||
package net.filebot.format;
|
||||
|
||||
import static net.filebot.util.RegularExpressions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import groovy.lang.GroovyObjectSupport;
|
||||
|
||||
public class AssociativeEnumObject extends GroovyObjectSupport implements List<Object> {
|
||||
|
||||
private final Map<?, ?> values;
|
||||
|
||||
public AssociativeEnumObject(Map<?, ?> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
protected String definingKey(Object key) {
|
||||
// letters and digits are defining, everything else will be ignored
|
||||
return NON_WORD.matcher(key.toString()).replaceAll("").toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getProperty(String name) {
|
||||
return getValue(definingKey(name)).orElseGet(() -> super.getProperty(name));
|
||||
}
|
||||
|
||||
private Optional<Object> getValue(String key) {
|
||||
return values.keySet().stream().filter(k -> key.equals(definingKey(k))).findFirst().map(values::get).map(Object.class::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperty(String name, Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Set<?> keySet() {
|
||||
return values.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return values.values().toString();
|
||||
}
|
||||
|
||||
public List<Object> toList() {
|
||||
return new ArrayList<Object>(values.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Object> iterator() {
|
||||
return toList().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(int index) {
|
||||
return toList().get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> subList(int fromIndex, int toIndex) {
|
||||
return toList().subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return values.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return values.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return values.values().contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return values.values().toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return values.values().toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Object e) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return values.values().containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends Object> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends Object> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object set(int index, Object element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, Object element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object remove(int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
return toList().indexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
return toList().lastIndexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Object> listIterator() {
|
||||
return toList().listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<Object> listIterator(int index) {
|
||||
return toList().listIterator(index);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
package net.filebot.format;
|
||||
|
||||
import static net.filebot.util.RegularExpressions.*;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.AbstractSet;
|
||||
|
@ -13,17 +14,14 @@ import java.util.TreeSet;
|
|||
|
||||
import groovy.lang.GroovyObjectSupport;
|
||||
|
||||
|
||||
public class AssociativeScriptObject extends GroovyObjectSupport implements Iterable<Entry<Object, Object>> {
|
||||
|
||||
private final Map<Object, Object> properties;
|
||||
|
||||
|
||||
public AssociativeScriptObject(Map<?, ?> properties) {
|
||||
this.properties = new LenientLookup(properties);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the property with the given name.
|
||||
*
|
||||
|
@ -37,26 +35,22 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
|
|||
return properties.get(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setProperty(String name, Object value) {
|
||||
// ignore, object is immutable
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<Object, Object>> iterator() {
|
||||
return properties.entrySet().iterator();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// all the properties in alphabetic order
|
||||
return new TreeSet<Object>(properties.keySet()).toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map allowing look-up of values by a fault-tolerant key as specified by the defining key.
|
||||
*
|
||||
|
@ -65,7 +59,6 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
|
|||
|
||||
private final Map<String, Entry<?, ?>> lookup = new HashMap<String, Entry<?, ?>>();
|
||||
|
||||
|
||||
public LenientLookup(Map<?, ?> source) {
|
||||
// populate lookup map
|
||||
for (Entry<?, ?> entry : source.entrySet()) {
|
||||
|
@ -73,19 +66,16 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
protected String definingKey(Object key) {
|
||||
// letters and digits are defining, everything else will be ignored
|
||||
return key.toString().replaceAll("[^\\p{Alnum}]", "").toLowerCase();
|
||||
return NON_WORD.matcher(key.toString()).replaceAll("").toLowerCase();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return lookup.containsKey(definingKey(key));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object get(Object key) {
|
||||
Entry<?, ?> entry = lookup.get(definingKey(key));
|
||||
|
@ -96,7 +86,6 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<Entry<Object, Object>> entrySet() {
|
||||
return new AbstractSet<Entry<Object, Object>>() {
|
||||
|
@ -106,7 +95,6 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
|
|||
return (Iterator) lookup.values().iterator();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return lookup.size();
|
||||
|
|
|
@ -844,9 +844,14 @@ public class MediaBindingBean {
|
|||
return new AssociativeScriptObject(new ImageMetadata(getMediaFile()).snapshot(t -> t.getTagName()));
|
||||
}
|
||||
|
||||
@Define("camera")
|
||||
public AssociativeEnumObject getCamera() throws Exception {
|
||||
return new ImageMetadata(getMediaFile()).getCameraModel().map(AssociativeEnumObject::new).orElse(null);
|
||||
}
|
||||
|
||||
@Define("location")
|
||||
public List<String> getLocation() throws Exception {
|
||||
return new ImageMetadata(getMediaFile()).getLocationTaken().orElse(null);
|
||||
public AssociativeEnumObject getLocation() throws Exception {
|
||||
return new ImageMetadata(getMediaFile()).getLocationTaken().map(AssociativeEnumObject::new).orElse(null);
|
||||
}
|
||||
|
||||
@Define("artist")
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package net.filebot.mediainfo;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static net.filebot.Logging.*;
|
||||
import static net.filebot.util.JsonUtilities.*;
|
||||
|
||||
|
@ -14,9 +12,7 @@ import java.time.ZoneOffset;
|
|||
import java.time.ZonedDateTime;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
@ -77,39 +73,62 @@ public class ImageMetadata {
|
|||
});
|
||||
}
|
||||
|
||||
public Optional<List<String>> getLocationTaken() {
|
||||
public Optional<Map<CameraProperty, String>> getCameraModel() {
|
||||
return extract(m -> {
|
||||
ExifIFD0Directory directory = m.getFirstDirectoryOfType(ExifIFD0Directory.class);
|
||||
String maker = directory.getDescription(ExifIFD0Directory.TAG_MAKE);
|
||||
String model = directory.getDescription(ExifIFD0Directory.TAG_MODEL);
|
||||
|
||||
Map<CameraProperty, String> camera = new EnumMap<CameraProperty, String>(CameraProperty.class);
|
||||
if (maker != null) {
|
||||
camera.put(CameraProperty.maker, maker);
|
||||
}
|
||||
if (model != null) {
|
||||
camera.put(CameraProperty.model, model);
|
||||
}
|
||||
return camera;
|
||||
}).filter(m -> m.size() > 0);
|
||||
}
|
||||
|
||||
public enum CameraProperty {
|
||||
maker, model;
|
||||
}
|
||||
|
||||
public Optional<Map<AddressComponent, String>> getLocationTaken() {
|
||||
return extract(m -> m.getFirstDirectoryOfType(GpsDirectory.class).getGeoLocation()).map(this::locate);
|
||||
}
|
||||
|
||||
protected List<String> locate(GeoLocation location) {
|
||||
protected Map<AddressComponent, String> locate(GeoLocation location) {
|
||||
try {
|
||||
// e.g. https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611
|
||||
Cache cache = Cache.getCache("geocode", CacheType.Monthly);
|
||||
Object json = cache.json(location.getLatitude() + "," + location.getLongitude(), pos -> new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + pos)).get();
|
||||
|
||||
Map<AddressComponents, String> address = new EnumMap<AddressComponents, String>(AddressComponents.class);
|
||||
Map<AddressComponent, String> address = new EnumMap<AddressComponent, String>(AddressComponent.class);
|
||||
|
||||
streamJsonObjects(json, "results").limit(1).forEach(r -> {
|
||||
streamJsonObjects(r, "address_components").forEach(a -> {
|
||||
String name = getString(a, "long_name");
|
||||
for (Object type : getArray(a, "types")) {
|
||||
stream(AddressComponents.values()).filter(c -> c.name().equals(type)).findFirst().ifPresent(c -> {
|
||||
address.putIfAbsent(c, name);
|
||||
});
|
||||
if (name != null) {
|
||||
for (Object type : getArray(a, "types")) {
|
||||
stream(AddressComponent.values()).filter(c -> c.name().equals(type)).findFirst().ifPresent(c -> {
|
||||
address.putIfAbsent(c, name);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return address.values().stream().filter(Objects::nonNull).collect(toList()); // enum set is always in natural order
|
||||
return address;
|
||||
} catch (Exception e) {
|
||||
debug.warning(e::toString);
|
||||
}
|
||||
|
||||
return emptyList();
|
||||
return null;
|
||||
}
|
||||
|
||||
private enum AddressComponents {
|
||||
country, administrative_area_level_1, administrative_area_level_2, administrative_area_level_3, administrative_area_level_4, sublocality, neighborhood;
|
||||
public enum AddressComponent {
|
||||
country, administrative_area_level_1, administrative_area_level_2, administrative_area_level_3, administrative_area_level_4, sublocality, neighborhood, route;
|
||||
}
|
||||
|
||||
protected <T> Optional<T> extract(Function<Metadata, T> extract) {
|
||||
|
|
|
@ -2,4 +2,4 @@
|
|||
parameter.exclude: ^StreamKind|^UniqueID|^StreamOrder|^ID|Count$
|
||||
|
||||
# preview expressions (keys are tagged so they can be sorted alphabetically)
|
||||
expressions: n, y, s, e, sxe, s00e00, t, d, startdate, absolute, es, e00, sy, sc, di, dc, age, special, episode, series, primaryTitle, alias, movie, tmdbid, imdbid, pi, pn, lang, subt, plex, az, type, anime, regular, music, album, artist, albumArtist, actors, director, collection, genre, genres, languages, runtime, certification, rating, votes, vc, ac, cf, vf, hpi, af, channels, resolution, dim, bitdepth, bitrate, kbps, khz, ws, sdhd, source, tags, s3d, group, original, info, info.network, info.status, info.productionCompanies, info.productionCountries, info.certifications, info.certifications.DE, omdb.rating, omdb.votes, localize.deu.n, localize.deu.t, localize.zho.n, localize.zho.t, order.airdate.sxe, order.dvd.sxe, fn, ext, mediaType, mediaPath, file, file.name, folder, folder.name, mediaTitle, audioLanguages, textLanguages, duration, seconds, minutes, bytes, megabytes, gigabytes, crc32, media.title, media.collection, media.season, media.part, media.partID, media.genre, media.contentType, media.description, media.lyrics, video[0].codecID, video[0].frameRate, video[0].displayAspectRatioString, video[0].scanType, audio.language, audio[0].bitRateString, audio[0].language, text.language, text[0].language, text[0].codecInfo, exif.make, exif.model, location
|
||||
expressions: n, y, s, e, sxe, s00e00, t, d, startdate, absolute, es, e00, sy, sc, di, dc, age, special, episode, series, primaryTitle, alias, movie, tmdbid, imdbid, pi, pn, lang, subt, plex, az, type, anime, regular, music, album, artist, albumArtist, actors, director, collection, genre, genres, languages, runtime, certification, rating, votes, vc, ac, cf, vf, hpi, af, channels, resolution, dim, bitdepth, bitrate, kbps, khz, ws, sdhd, source, tags, s3d, group, original, info, info.network, info.status, info.productionCompanies, info.productionCountries, info.certifications, info.certifications.DE, omdb.rating, omdb.votes, localize.deu.n, localize.deu.t, localize.zho.n, localize.zho.t, order.airdate.sxe, order.dvd.sxe, fn, ext, mediaType, mediaPath, file, file.name, folder, folder.name, mediaTitle, audioLanguages, textLanguages, duration, seconds, minutes, bytes, megabytes, gigabytes, crc32, media.title, media.collection, media.season, media.part, media.partID, media.genre, media.contentType, media.description, media.lyrics, video[0].codecID, video[0].frameRate, video[0].displayAspectRatioString, video[0].scanType, audio.language, audio[0].bitRateString, audio[0].language, text.language, text[0].language, text[0].codecInfo, camera, camera.maker, camera.model, location, location.country
|
||||
|
|
|
@ -8,6 +8,7 @@ public class RegularExpressions {
|
|||
|
||||
public static final Pattern DIGIT = compile("\\d+");
|
||||
public static final Pattern NON_DIGIT = compile("\\D+");
|
||||
public static final Pattern NON_WORD = compile("[\\P{Alnum}]+");
|
||||
|
||||
public static final Pattern PIPE = compile("|", LITERAL);
|
||||
public static final Pattern EQUALS = compile("=", LITERAL);
|
||||
|
|
Loading…
Reference in New Issue