* api support for thetvdb banners

This commit is contained in:
Reinhard Pointner 2011-12-20 02:37:36 +00:00
parent f8c746f075
commit 70adfa0b0f
4 changed files with 161 additions and 10 deletions

View File

@ -40,6 +40,13 @@ List.metaClass.mapByFolder = { mapByFolder(delegate) }
List.metaClass.mapByExtension = { mapByExtension(delegate) }
// WebRequest utility methods
import static net.sourceforge.filebot.web.WebRequest.*
URL.metaClass.parseHtml = { new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(delegate))) };
URL.metaClass.saveAs = { f -> writeFile(fetch(delegate), f) }
// Shell helper
import static com.sun.jna.Platform.*;

View File

@ -361,4 +361,100 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
}
/**
* Search for a series banner matching the given parameters
*
* @see http://thetvdb.com/wiki/index.php/API:banners.xml
*/
public Map<BannerProperty, Object> getBanner(TheTVDBSearchResult series, String bannerType, String bannerType2, Integer season, String language) throws Exception {
// build selector
Map<BannerProperty, Object> selector = new EnumMap<BannerProperty, Object>(BannerProperty.class);
if (bannerType != null)
selector.put(BannerProperty.BannerType, bannerType);
if (bannerType2 != null)
selector.put(BannerProperty.BannerType2, bannerType2);
if (season != null)
selector.put(BannerProperty.Season, new Double(season));
if (language != null)
selector.put(BannerProperty.Language, language);
// search for a banner matching the selector
for (Map<BannerProperty, Object> it : getBannerDescriptor(series.seriesId)) {
if (it.entrySet().containsAll(selector.entrySet())) {
return it;
}
}
return null;
}
public List<Map<BannerProperty, Object>> getBannerDescriptor(int seriesid) throws Exception {
Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + seriesid + "/banners.xml"));
List<Node> nodes = selectNodes("//Banner", dom);
List<Map<BannerProperty, Object>> banners = new ArrayList<Map<BannerProperty, Object>>();
for (Node node : nodes) {
try {
EnumMap<BannerProperty, Object> item = new EnumMap<BannerProperty, Object>(BannerProperty.class);
// copy values from xml
for (BannerProperty key : BannerProperty.values()) {
String value = getTextContent(key.name(), node);
if (value != null && value.length() > 0) {
item.put(key, getTextContent(key.name(), node));
}
}
// parse numbers
for (BannerProperty key : BannerProperty.numbers()) {
if (item.get(key) != null) {
item.put(key, new Double(item.get(key).toString()));
}
}
// resolve relative urls
for (BannerProperty key : BannerProperty.urls()) {
if (item.get(key) != null) {
item.put(key, getResource(MirrorType.BANNER, "/banners/" + item.get(key)));
}
}
banners.add(item);
} catch (Exception e) {
// log and ignore
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Invalid banner descriptor", e);
}
}
return banners;
}
public static enum BannerProperty {
id,
BannerPath,
BannerType,
BannerType2,
Season,
Colors,
Language,
Rating,
RatingCount,
SeriesName,
ThumbnailPath,
VignettePath;
public static BannerProperty[] numbers() {
return new BannerProperty[] { id, Season, Rating, RatingCount };
}
public static BannerProperty[] urls() {
return new BannerProperty[] { BannerPath, ThumbnailPath, VignettePath };
}
}
}

View File

@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
@ -27,6 +28,15 @@ import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
@ -461,6 +471,18 @@ public final class FileUtilities {
}
public static String getXmlString(Document dom) throws TransformerException {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from dom
StringWriter buffer = new StringWriter();
tr.transform(new DOMSource(dom), new StreamResult(buffer));
return buffer.toString();
}
public static final long KILO = 1024;
public static final long MEGA = KILO * 1024;
public static final long GIGA = MEGA * 1024;

View File

@ -4,15 +4,18 @@ package net.sourceforge.filebot.web;
import static org.junit.Assert.*;
import java.net.URL;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import net.sf.ehcache.CacheManager;
import net.sourceforge.filebot.web.TheTVDBClient.BannerProperty;
import net.sourceforge.filebot.web.TheTVDBClient.MirrorType;
import net.sourceforge.filebot.web.TheTVDBClient.TheTVDBSearchResult;
@ -21,7 +24,7 @@ public class TheTVDBClientTest {
private TheTVDBClient thetvdb = new TheTVDBClient("BA864DEE427E384A");
@Test
public void search() throws Exception {
// test default language and query escaping (blanks)
@ -35,7 +38,7 @@ public class TheTVDBClientTest {
assertEquals(70726, first.getSeriesId());
}
@Test
public void searchGerman() throws Exception {
List<SearchResult> results = thetvdb.search("Buffy the Vampire Slayer", Locale.GERMAN);
@ -48,7 +51,7 @@ public class TheTVDBClientTest {
assertEquals(70327, first.getSeriesId());
}
@Test
public void getEpisodeListAll() throws Exception {
List<Episode> list = thetvdb.getEpisodeList(new TheTVDBSearchResult("Buffy the Vampire Slayer", 70327));
@ -76,7 +79,7 @@ public class TheTVDBClientTest {
assertEquals(null, last.airdate());
}
@Test
public void getEpisodeListSingleSeason() throws Exception {
List<Episode> list = thetvdb.getEpisodeList(new TheTVDBSearchResult("Wonderfalls", 78845), 1);
@ -94,7 +97,7 @@ public class TheTVDBClientTest {
assertEquals("2004-03-12", first.airdate().toString());
}
@Test
public void getEpisodeListNumbering() throws Exception {
List<Episode> list = thetvdb.getEpisodeList(new TheTVDBSearchResult("Firefly", 78874), 1);
@ -111,19 +114,19 @@ public class TheTVDBClientTest {
assertEquals("2002-12-20", first.airdate().toString());
}
@Test
public void getEpisodeListLink() {
assertEquals("http://www.thetvdb.com/?tab=seasonall&id=78874", thetvdb.getEpisodeListLink(new TheTVDBSearchResult("Firefly", 78874)).toString());
}
@Test
public void getEpisodeListLinkSingleSeason() {
assertEquals("http://www.thetvdb.com/?tab=season&seriesid=73965&seasonid=6749", thetvdb.getEpisodeListLink(new TheTVDBSearchResult("Roswell", 73965), 3).toString());
}
@Test
public void getMirror() throws Exception {
assertNotNull(thetvdb.getMirror(MirrorType.XML));
@ -131,7 +134,7 @@ public class TheTVDBClientTest {
assertNotNull(thetvdb.getMirror(MirrorType.ZIP));
}
@Test
public void resolveTypeMask() {
// no flags set
@ -144,7 +147,30 @@ public class TheTVDBClientTest {
assertEquals(EnumSet.allOf(MirrorType.class), MirrorType.fromTypeMask(7));
}
@Test
public void getBanner() throws Exception {
Map<BannerProperty, Object> banner = thetvdb.getBanner(new TheTVDBSearchResult("Buffy the Vampire Slayer", 70327), "season", "seasonwide", 7, "en");
assertEquals(857660, (Double) banner.get(BannerProperty.id), 0);
assertEquals("season", banner.get(BannerProperty.BannerType));
assertEquals("seasonwide", banner.get(BannerProperty.BannerType2));
assertEquals("http://thetvdb.com/banners/seasonswide/70327-7.jpg", banner.get(BannerProperty.BannerPath).toString());
assertEquals(99712, WebRequest.fetch((URL) banner.get(BannerProperty.BannerPath)).remaining(), 0);
}
@Test
public void getBannerDescriptor() throws Exception {
List<Map<BannerProperty, Object>> banners = thetvdb.getBannerDescriptor(70327);
assertEquals(106, banners.size());
assertEquals("fanart", banners.get(0).get(BannerProperty.BannerType));
assertEquals("1280x720", banners.get(0).get(BannerProperty.BannerType2));
assertEquals(486993, WebRequest.fetch((URL) banners.get(0).get(BannerProperty.BannerPath)).remaining(), 0);
}
@BeforeClass
@AfterClass
public static void clearCache() {