mirror of
https://github.com/magefree/mage.git
synced 2025-12-24 20:41:58 -08:00
images: improved support of scryfall download (added timeout for compatibility with api rate limits)
This commit is contained in:
parent
a5c1b851eb
commit
4ba3e1fec5
3 changed files with 22 additions and 3 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue