Skip to content

Commit

Permalink
Scraper: remove duplicate getLanguage and treat exceptions for tmdb
Browse files Browse the repository at this point in the history
enforce two letter code except for pt-br and get chinese right

try to get the default language right
  • Loading branch information
courville committed Aug 11, 2024
1 parent 7be6af6 commit 667df74
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/com/archos/mediascraper/Scraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.os.Bundle;
import android.util.Log;

import androidx.preference.PreferenceManager;

import com.archos.mediascraper.preprocess.SearchInfo;
import com.archos.mediascraper.preprocess.SearchPreprocessor;
import com.archos.mediascraper.xml.BaseScraper2;
Expand All @@ -27,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;

public class Scraper {
private static final Logger log = LoggerFactory.getLogger(Scraper.class);

Expand Down Expand Up @@ -104,4 +108,27 @@ public ScrapeDetailResult getAutoDetails(SearchInfo info) {
return mMovieScraper.search(info);
}

public static String getLanguage(Context context) {
String defaultLanguage = Locale.getDefault().getLanguage();
String result;
// if defaultLanguage is of the form xx-yy take result as xx
if (defaultLanguage.length() > 2 && defaultLanguage.charAt(2) == '-') {
result = defaultLanguage.substring(0, 2);
} else {
result = defaultLanguage;
}
// zh = Mandarin -> Chinese Simplified (zh-cn) or Chinese
// cn = Cantonese -> Chinese Traditional (zh-tw)
if (defaultLanguage.toLowerCase().startsWith("zh-tw")) {
log.warn("getLanguage: curing defaultScraperLanguage=" + defaultLanguage + " zh-tw -> to cn");
result = "cn"; // Traditional Chinese
}
// treat brazilian portuguese as an exception, tmdb understands pt-br
if (defaultLanguage.toLowerCase().startsWith("pt-br")) {
log.warn("getLanguage: curing defaultScraperLanguage=" + defaultLanguage + ", keeping pt-br");
result = "pt-br"; // Brazilian Portuguese
}
return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", result);
}

}
9 changes: 3 additions & 6 deletions src/com/archos/mediascraper/xml/MovieScraper3.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.archos.mediascraper.ScrapeDetailResult;
import com.archos.mediascraper.ScrapeSearchResult;
import com.archos.mediascraper.ScrapeStatus;
import com.archos.mediascraper.Scraper;
import com.archos.mediascraper.ScraperCache;
import com.archos.mediascraper.SearchResult;
import com.archos.mediascraper.preprocess.MovieSearchInfo;
Expand Down Expand Up @@ -96,7 +97,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) {
if (tmdb == null) reauth();
if (searchService == null) searchService = tmdb.searchService();
// get configured language
String language = getLanguage(mContext);
String language = Scraper.getLanguage(mContext);
log.debug("movie search:" + searchInfo.getName() + " year:" + searchInfo.getYear() + " language:" + language);
SearchMovieResult searchResult = SearchMovie2.search(searchInfo.getName(), language, searchInfo.getYear(), maxItems, searchService, adultScrape);
// TODO: this triggers scrape for all search results, is this intended?
Expand All @@ -112,7 +113,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) {
@Override
protected ScrapeDetailResult getDetailsInternal(SearchResult result, Bundle options) {
// TODO: why it searches every first level result?
String language = getLanguage(mContext);
String language = Scraper.getLanguage(mContext);
log.debug("getDetailsInternal: language=" + language);

long movieId = result.getId();
Expand Down Expand Up @@ -168,10 +169,6 @@ protected ScrapeDetailResult getDetailsInternal(SearchResult result, Bundle opti
return new ScrapeDetailResult(tag, true, null, ScrapeStatus.OKAY, null);
}

public static String getLanguage(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", Locale.getDefault().getLanguage());
}

@Override
protected String internalGetPreferenceName() {
return PREFERENCE_NAME;
Expand Down
6 changes: 1 addition & 5 deletions src/com/archos/mediascraper/xml/ShowScraper4.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) {
}
TvShowSearchInfo searchInfo = (TvShowSearchInfo) info;
// get configured language
String language = getLanguage(mContext);
String language = Scraper.getLanguage(mContext);
log.debug("getMatches2: tvshow search:" + searchInfo.getShowName()
+ " s:" + searchInfo.getSeason()
+ " e:" + searchInfo.getEpisode() + ", maxItems=" + maxItems + ", language=" + language);
Expand Down Expand Up @@ -429,10 +429,6 @@ private Bundle buildBundle(Map<String, EpisodeTags> allEpisodes, Bundle options)
return bundle;
}

public static String getLanguage(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", Locale.getDefault().getLanguage());
}

public static boolean isShowAlreadyKnown(Integer showId, Context context) {
ContentResolver contentResolver = context.getContentResolver();
String[] baseProjection = {ScraperStore.Show.ONLINE_ID};
Expand Down

0 comments on commit 667df74

Please sign in to comment.