mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-25 09:18:51 -05:00
Move CachedResource2
This commit is contained in:
parent
a46a3e48a8
commit
c5c8525b49
@ -1,19 +1,20 @@
|
|||||||
package net.filebot;
|
package net.filebot;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.*;
|
import static java.nio.charset.StandardCharsets.*;
|
||||||
|
import static net.filebot.CachedResource2.*;
|
||||||
import static net.filebot.Logging.*;
|
import static net.filebot.Logging.*;
|
||||||
import static net.filebot.web.CachedResource2.*;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import net.filebot.web.CachedResource2;
|
import net.filebot.CachedResource2.Fetch;
|
||||||
import net.filebot.web.CachedResource2.Source;
|
import net.filebot.CachedResource2.Source;
|
||||||
|
import net.filebot.CachedResource2.Transform;
|
||||||
import net.filebot.web.FloodLimit;
|
import net.filebot.web.FloodLimit;
|
||||||
import net.filebot.web.Resource;
|
|
||||||
import net.sf.ehcache.Element;
|
import net.sf.ehcache.Element;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
@ -21,20 +22,25 @@ import org.w3c.dom.Document;
|
|||||||
public class Cache {
|
public class Cache {
|
||||||
|
|
||||||
public static final Duration ONE_DAY = Duration.ofDays(1);
|
public static final Duration ONE_DAY = Duration.ofDays(1);
|
||||||
|
public static final Duration ONE_WEEK = Duration.ofDays(7);
|
||||||
|
|
||||||
public static Cache getCache(String name, CacheType type) {
|
public static Cache getCache(String name, CacheType type) {
|
||||||
return CacheManager.getInstance().getCache(name.toLowerCase(), type);
|
return CacheManager.getInstance().getCache(name.toLowerCase(), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Resource<String> text(String url, Duration expirationTime, FloodLimit limit) {
|
public <R> CachedResource2<String, R> resource(String key, Source<String> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime) {
|
||||||
|
return new CachedResource2<String, R>(key, source, fetch, parse, cast, expirationTime, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CachedResource2<String, String> text(String url, Duration expirationTime, FloodLimit limit) {
|
||||||
return new CachedResource2<String, String>(url, URL::new, withPermit(fetchIfModified(), r -> limit.acquirePermit() != null), getText(UTF_8), String.class::cast, expirationTime, this);
|
return new CachedResource2<String, String>(url, URL::new, withPermit(fetchIfModified(), r -> limit.acquirePermit() != null), getText(UTF_8), String.class::cast, expirationTime, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Resource<Document> xml(String key, Source<String> source, Duration expirationTime) {
|
public CachedResource2<String, Document> xml(String key, Source<String> source, Duration expirationTime) {
|
||||||
return new CachedResource2<String, Document>(key, source, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this);
|
return new CachedResource2<String, Document>(key, source, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Resource<Object> json(String key, Source<String> source, Duration expirationTime) {
|
public CachedResource2<String, Object> json(String key, Source<String> source, Duration expirationTime) {
|
||||||
return new CachedResource2<String, Object>(key, source, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this);
|
return new CachedResource2<String, Object>(key, source, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package net.filebot.web;
|
package net.filebot;
|
||||||
|
|
||||||
import static net.filebot.Logging.*;
|
import static net.filebot.Logging.*;
|
||||||
|
|
||||||
@ -10,8 +10,8 @@ import java.nio.charset.Charset;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import net.filebot.Cache;
|
|
||||||
import net.filebot.util.JsonUtilities;
|
import net.filebot.util.JsonUtilities;
|
||||||
|
import net.filebot.web.WebRequest;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
@ -20,19 +20,19 @@ public class CachedResource2<K, R> implements Resource<R> {
|
|||||||
public static final int DEFAULT_RETRY_LIMIT = 2;
|
public static final int DEFAULT_RETRY_LIMIT = 2;
|
||||||
public static final Duration DEFAULT_RETRY_DELAY = Duration.ofSeconds(2);
|
public static final Duration DEFAULT_RETRY_DELAY = Duration.ofSeconds(2);
|
||||||
|
|
||||||
protected final K key;
|
private K key;
|
||||||
|
|
||||||
protected final Source<K> source;
|
private Source<K> source;
|
||||||
protected final Fetch fetch;
|
private Fetch fetch;
|
||||||
protected final Transform<ByteBuffer, ? extends Object> parse;
|
private Transform<ByteBuffer, ? extends Object> parse;
|
||||||
protected final Transform<? super Object, R> cast;
|
private Transform<? super Object, R> cast;
|
||||||
|
|
||||||
protected final Duration expirationTime;
|
private Duration expirationTime;
|
||||||
|
|
||||||
protected final int retryCountLimit;
|
private int retryCountLimit;
|
||||||
protected final long retryWaitTime;
|
private long retryWaitTime;
|
||||||
|
|
||||||
protected final Cache cache;
|
private final Cache cache;
|
||||||
|
|
||||||
public CachedResource2(K key, Source<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime, Cache cache) {
|
public CachedResource2(K key, Source<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime, Cache cache) {
|
||||||
this(key, source, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache);
|
this(key, source, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache);
|
||||||
@ -59,7 +59,7 @@ public class CachedResource2<K, R> implements Resource<R> {
|
|||||||
debug.fine(format("Fetch %s (If-Modified-Since: %tc)", resource, lastModified));
|
debug.fine(format("Fetch %s (If-Modified-Since: %tc)", resource, lastModified));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ByteBuffer data = retry(() -> fetch.fetch(resource, lastModified), retryCountLimit, lastModified);
|
ByteBuffer data = retry(() -> fetch.fetch(resource, lastModified), retryCountLimit, retryWaitTime);
|
||||||
|
|
||||||
// 304 Not Modified
|
// 304 Not Modified
|
||||||
if (data == null && element != null && element.getObjectValue() != null) {
|
if (data == null && element != null && element.getObjectValue() != null) {
|
@ -1,4 +1,4 @@
|
|||||||
package net.filebot.web;
|
package net.filebot;
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface Resource<R> {
|
public interface Resource<R> {
|
@ -1,52 +0,0 @@
|
|||||||
package net.filebot.web;
|
|
||||||
|
|
||||||
import static net.filebot.util.JsonUtilities.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import net.sf.ehcache.Cache;
|
|
||||||
import net.sf.ehcache.CacheManager;
|
|
||||||
|
|
||||||
public class CachedJsonResource extends AbstractCachedResource<String, String> {
|
|
||||||
|
|
||||||
public CachedJsonResource(String resource) {
|
|
||||||
super(resource, String.class, ONE_DAY, 2, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Cache getCache() {
|
|
||||||
return CacheManager.getInstance().getCache("web-datasource-lv3");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getJsonObject() throws IOException {
|
|
||||||
try {
|
|
||||||
return readJson(get());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(String.format("Error while loading JSON resource: %s (%s)", getResourceLocation(resource), e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String process(String data) throws IOException {
|
|
||||||
try {
|
|
||||||
readJson(get()); // make sure JSON is valid
|
|
||||||
return data;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(String.format("Malformed JSON: %s (%s)", getResourceLocation(resource), e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String fetchData(URL url, long lastModified) throws IOException {
|
|
||||||
ByteBuffer data = WebRequest.fetchIfModified(url, lastModified);
|
|
||||||
|
|
||||||
if (data == null)
|
|
||||||
return null; // not modified
|
|
||||||
|
|
||||||
return StandardCharsets.UTF_8.decode(data).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -15,6 +15,7 @@ import javax.swing.Icon;
|
|||||||
|
|
||||||
import net.filebot.Cache;
|
import net.filebot.Cache;
|
||||||
import net.filebot.CacheType;
|
import net.filebot.CacheType;
|
||||||
|
import net.filebot.Resource;
|
||||||
import net.filebot.ResourceManager;
|
import net.filebot.ResourceManager;
|
||||||
|
|
||||||
public class TVMazeClient extends AbstractEpisodeListProvider {
|
public class TVMazeClient extends AbstractEpisodeListProvider {
|
||||||
|
@ -12,7 +12,6 @@ import java.io.Serializable;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
@ -29,6 +28,7 @@ import javax.swing.Icon;
|
|||||||
|
|
||||||
import net.filebot.Cache;
|
import net.filebot.Cache;
|
||||||
import net.filebot.CacheType;
|
import net.filebot.CacheType;
|
||||||
|
import net.filebot.Resource;
|
||||||
import net.filebot.ResourceManager;
|
import net.filebot.ResourceManager;
|
||||||
import net.filebot.util.FileUtilities;
|
import net.filebot.util.FileUtilities;
|
||||||
import net.filebot.web.TheTVDBClient.BannerDescriptor.BannerProperty;
|
import net.filebot.web.TheTVDBClient.BannerDescriptor.BannerProperty;
|
||||||
|
Loading…
Reference in New Issue
Block a user