* bridge Java/TheTVDB language code differences

This commit is contained in:
Reinhard Pointner 2012-07-14 14:54:07 +00:00
parent 3182f15d03
commit b0d8bd9cff
1 changed files with 49 additions and 27 deletions

View File

@ -80,6 +80,23 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
public String getLanguageCode(Locale locale) {
String code = locale.getLanguage();
// Java language code => TheTVDB language code
if (code.equals("iw")) // Hebrew
return "he";
if (code.equals("hi")) // Hungarian
return "hu";
if (code.equals("in")) // Indonesian
return "id";
if (code.equals("ro")) // Russian
return "ru";
return code;
}
@Override @Override
public ResultCache getCache() { public ResultCache getCache() {
return new ResultCache(host, CacheManager.getInstance().getCache("web-datasource")); return new ResultCache(host, CacheManager.getInstance().getCache("web-datasource"));
@ -87,9 +104,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
@Override @Override
public List<SearchResult> fetchSearchResult(String query, Locale language) throws Exception { public List<SearchResult> fetchSearchResult(String query, Locale locale) throws Exception {
// perform online search // perform online search
URL url = getResource(null, "/api/GetSeries.php?seriesname=" + encode(query) + "&language=" + language.getLanguage()); URL url = getResource(null, "/api/GetSeries.php?seriesname=" + encode(query) + "&language=" + getLanguageCode(locale));
Document dom = getDocument(url); Document dom = getDocument(url);
List<Node> nodes = selectNodes("Data/Series", dom); List<Node> nodes = selectNodes("Data/Series", dom);
@ -109,9 +126,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
@Override @Override
public List<Episode> fetchEpisodeList(SearchResult searchResult, SortOrder sortOrder, Locale language) throws Exception { public List<Episode> fetchEpisodeList(SearchResult searchResult, SortOrder sortOrder, Locale locale) throws Exception {
TheTVDBSearchResult series = (TheTVDBSearchResult) searchResult; TheTVDBSearchResult series = (TheTVDBSearchResult) searchResult;
Document seriesRecord = getSeriesRecord(series, language); Document seriesRecord = getSeriesRecord(series, getLanguageCode(locale));
// we could get the series name from the search result, but the language may not match the given parameter // we could get the series name from the search result, but the language may not match the given parameter
String seriesName = selectString("Data/Series/SeriesName", seriesRecord); String seriesName = selectString("Data/Series/SeriesName", seriesRecord);
@ -173,14 +190,16 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
public Document getSeriesRecord(TheTVDBSearchResult searchResult, Locale language) throws Exception { public Document getSeriesRecord(TheTVDBSearchResult searchResult, String languageCode) throws Exception {
URL seriesRecord = getResource(MirrorType.ZIP, "/api/" + apikey + "/series/" + searchResult.getSeriesId() + "/all/" + language.getLanguage() + ".zip"); URL seriesRecord = getResource(MirrorType.ZIP, "/api/" + apikey + "/series/" + searchResult.getSeriesId() + "/all/" + languageCode + ".zip");
try {
ZipInputStream zipInputStream = new ZipInputStream(seriesRecord.openStream()); ZipInputStream zipInputStream = new ZipInputStream(seriesRecord.openStream());
ZipEntry zipEntry; ZipEntry zipEntry;
try { try {
String seriesRecordName = language.getLanguage() + ".xml"; String seriesRecordName = languageCode + ".xml";
while ((zipEntry = zipInputStream.getNextEntry()) != null) { while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (seriesRecordName.equals(zipEntry.getName())) { if (seriesRecordName.equals(zipEntry.getName())) {
@ -193,23 +212,26 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} finally { } finally {
zipInputStream.close(); zipInputStream.close();
} }
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(String.format("Series record not found: %s [%s]: %s", searchResult.getName(), languageCode, seriesRecord));
}
} }
public TheTVDBSearchResult lookupByID(int id, Locale language) throws Exception { public TheTVDBSearchResult lookupByID(int id, Locale locale) throws Exception {
TheTVDBSearchResult cachedItem = getCache().getData("lookupByID", id, language, TheTVDBSearchResult.class); TheTVDBSearchResult cachedItem = getCache().getData("lookupByID", id, locale, TheTVDBSearchResult.class);
if (cachedItem != null) { if (cachedItem != null) {
return cachedItem; return cachedItem;
} }
try { try {
URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + language.getLanguage() + ".xml"); URL baseRecordLocation = getResource(MirrorType.XML, "/api/" + apikey + "/series/" + id + "/all/" + getLanguageCode(locale) + ".xml");
Document baseRecord = getDocument(baseRecordLocation); Document baseRecord = getDocument(baseRecordLocation);
String name = selectString("//SeriesName", baseRecord); String name = selectString("//SeriesName", baseRecord);
TheTVDBSearchResult series = new TheTVDBSearchResult(name, id); TheTVDBSearchResult series = new TheTVDBSearchResult(name, id);
getCache().putData("lookupByID", id, language, series); getCache().putData("lookupByID", id, locale, series);
return series; return series;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// illegal series id // illegal series id
@ -219,13 +241,13 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale language) throws Exception { public TheTVDBSearchResult lookupByIMDbID(int imdbid, Locale locale) throws Exception {
TheTVDBSearchResult cachedItem = getCache().getData("lookupByIMDbID", imdbid, language, TheTVDBSearchResult.class); TheTVDBSearchResult cachedItem = getCache().getData("lookupByIMDbID", imdbid, locale, TheTVDBSearchResult.class);
if (cachedItem != null) { if (cachedItem != null) {
return cachedItem; return cachedItem;
} }
URL query = getResource(null, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + language.getLanguage()); URL query = getResource(null, "/api/GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + getLanguageCode(locale));
Document dom = getDocument(query); Document dom = getDocument(query);
String id = selectString("//seriesid", dom); String id = selectString("//seriesid", dom);
@ -235,7 +257,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
return null; return null;
TheTVDBSearchResult series = new TheTVDBSearchResult(name, Integer.parseInt(id)); TheTVDBSearchResult series = new TheTVDBSearchResult(name, Integer.parseInt(id));
getCache().putData("lookupByIMDbID", imdbid, language, series); getCache().putData("lookupByIMDbID", imdbid, locale, series);
return series; return series;
} }
@ -410,7 +432,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
return cachedItem; return cachedItem;
} }
Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + searchResult.seriesId + "/" + locale.getLanguage() + ".xml")); Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + searchResult.seriesId + "/" + getLanguageCode(locale) + ".xml"));
Node node = selectNode("//Series", dom); Node node = selectNode("//Series", dom);
Map<SeriesProperty, String> fields = new EnumMap<SeriesProperty, String>(SeriesProperty.class); Map<SeriesProperty, String> fields = new EnumMap<SeriesProperty, String>(SeriesProperty.class);