images: improved support of scryfall download (added timeout for compatibility with api rate limits)

This commit is contained in:
Oleg Agafonov 2023-11-23 14:00:26 +04:00
parent a5c1b851eb
commit 4ba3e1fec5
3 changed files with 22 additions and 3 deletions

View file

@ -47,6 +47,11 @@ public interface CardImageSource {
return CardLanguage.ENGLISH;
}
/**
* Pause before each http request (must use for services with rate limits)
*
* @param httpImageUrl
*/
void doPause(String httpImageUrl);
default List<String> getSupportedSets() {
@ -59,4 +64,8 @@ public interface CardImageSource {
default boolean isTokenImageProvided(String setCode, String cardName, Integer tokenNumber) {
return false;
}
default int getDownloadTimeoutMs() {
return 0;
}
}

View file

@ -30,6 +30,7 @@ public enum ScryfallImageSource implements CardImageSource {
private final Map<CardLanguage, String> languageAliases;
private CardLanguage currentLanguage = CardLanguage.ENGLISH; // working language
private final Map<CardDownloadData, String> preparedUrls = new HashMap<>();
private final int DOWNLOAD_TIMEOUT_MS = 100;
ScryfallImageSource() {
// LANGUAGES
@ -170,6 +171,7 @@ public enum ScryfallImageSource implements CardImageSource {
String jsonUrl = null;
for (String currentUrl : needUrls) {
// connect to Scryfall API
waitBeforeRequest();
URL cardUrl = new URL(currentUrl);
URLConnection request = (proxy == null ? cardUrl.openConnection() : cardUrl.openConnection(proxy));
request.connect();
@ -321,7 +323,16 @@ public enum ScryfallImageSource implements CardImageSource {
@Override
public void doPause(String httpImageUrl) {
waitBeforeRequest();
}
private void waitBeforeRequest() {
// scryfall recommends 50-100 ms timeout per each request to API to work under a rate limit
// possible error: 429 Too Many Requests
try {
Thread.sleep(DOWNLOAD_TIMEOUT_MS);
} catch (InterruptedException ignored) {
}
}
private String formatSetName(String setName, boolean isToken) {

View file

@ -41,7 +41,6 @@ import static org.mage.plugins.card.utils.CardImageUtils.getImagesDir;
*/
public class DownloadPicturesService extends DefaultBoundedRangeModel implements DownloadServiceInfo, Runnable {
// don't forget to remove new sets from ignore.urls to download (properties file in resources)
private static DownloadPicturesService instance;
private static final Logger logger = Logger.getLogger(DownloadPicturesService.class);
@ -689,7 +688,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
this.urls = urls;
this.count = count;
this.actualFilename = "";
useSpecifiedPaths = false;
this.useSpecifiedPaths = false;
}
DownloadTask(CardDownloadData card, String baseUrl, String actualFilename, int count) {
@ -697,7 +696,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
this.urls = new CardImageUrls(baseUrl, null);
this.count = count;
this.actualFilename = actualFilename;
useSpecifiedPaths = true;
this.useSpecifiedPaths = true;
}
@Override