77import java .awt .image .BufferedImage ;
88import java .io .File ;
99import java .io .IOException ;
10+ import java .io .InputStream ;
1011import java .net .HttpURLConnection ;
1112import java .net .URL ;
13+ import java .net .URLConnection ;
1214
1315public class SwingImageFetcher extends ImageFetcher {
1416
@@ -34,13 +36,41 @@ private boolean doFetch(String urlToDownload) throws IOException {
3436 return false ;
3537 }
3638
39+ if (inScryfallCooldown (urlToDownload )) {
40+ return false ;
41+ }
42+
3743 String newdespath = urlToDownload .contains (".fullborder.jpg" ) || urlToDownload .startsWith (ForgeConstants .URL_PIC_SCRYFALL_DOWNLOAD ) ?
3844 TextUtil .fastReplace (destPath , ".full.jpg" , ".fullborder.jpg" ) : destPath ;
3945 if (!newdespath .contains (".full" ) && !newdespath .contains (".artcrop" ) && urlToDownload .startsWith (ForgeConstants .URL_PIC_SCRYFALL_DOWNLOAD ) && !destPath .startsWith (ForgeConstants .CACHE_TOKEN_PICS_DIR ))
4046 newdespath = newdespath .replace (".jpg" , ".fullborder.jpg" ); //fix planes/phenomenon for round border options
4147 URL url = new URL (urlToDownload );
4248 System .out .println ("Attempting to fetch: " + url );
43- BufferedImage image = ImageIO .read (url );
49+ paceScryfall (urlToDownload );
50+
51+ // Read through a connection rather than ImageIO.read(URL), which discards the response
52+ // code - without it a 429 is indistinguishable from any other failure and we keep asking.
53+ final URLConnection connection = url .openConnection ();
54+ connection .setRequestProperty ("Accept" , "*/*" );
55+ connection .setRequestProperty ("User-Agent" , BuildInfo .getUserAgent ());
56+ if (connection instanceof HttpURLConnection httpConnection ) {
57+ final int responseCode = httpConnection .getResponseCode ();
58+ if (responseCode != HttpURLConnection .HTTP_OK ) {
59+ System .err .println ("Failed to fetch image. HTTP code: " + responseCode
60+ + " (" + httpConnection .getResponseMessage () + ") for URL: " + urlToDownload );
61+ if (responseCode == 429 && isScryfall (urlToDownload )) {
62+ System .err .println ("Rate limited by scryfall. Pausing image downloads." );
63+ noteScryfallRateLimited ();
64+ }
65+ httpConnection .disconnect ();
66+ return false ;
67+ }
68+ }
69+
70+ BufferedImage image ;
71+ try (InputStream is = connection .getInputStream ()) {
72+ image = ImageIO .read (is );
73+ }
4474 // First, save to a temporary file so that nothing tries to read
4575 // a partial download.
4676 File destFile = new File (newdespath + ".tmp" );
0 commit comments