Check Download-Quota HTTP header when downloading subtitles and abort if quota has been reached.

This commit is contained in:
Reinhard Pointner 2016-04-06 12:01:43 +00:00
parent c79896f827
commit e4e5c01cd7
3 changed files with 17 additions and 7 deletions

View File

@ -391,7 +391,6 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
if (!xmlrpc.isLoggedOn()) {
xmlrpc.login(username, password, "en");
}
logoutTimer.set(10, TimeUnit.MINUTES, true);
}

View File

@ -117,20 +117,28 @@ public class OpenSubtitlesSubtitleDescriptor implements SubtitleDescriptor, Seri
private static int DOWNLOAD_QUOTA = 1000;
public static synchronized void checkDownloadQuota() throws IllegalStateException {
if (DOWNLOAD_QUOTA <= 0) {
throw new IllegalStateException("Download-Quota has been exceeded");
}
}
private static synchronized void setAndCheckDownloadQuota(int quota) throws IllegalStateException {
DOWNLOAD_QUOTA = quota;
checkDownloadQuota();
}
@Override
public ByteBuffer fetch() throws Exception {
if (DOWNLOAD_QUOTA <= 0) {
throw new IOException("Download-Quota has been exceeded");
}
checkDownloadQuota();
URLConnection c = new URL(getProperty(Property.SubDownloadLink)).openConnection();
try (InputStream in = c.getInputStream()) {
// check download quota
String quota = c.getHeaderField("Download-Quota");
if (quota != null) {
if ((DOWNLOAD_QUOTA = Integer.parseInt(quota)) <= 0) {
throw new IOException("Download-Quota has been exceeded");
}
setAndCheckDownloadQuota(Integer.parseInt(quota));
debug.finest(format("Download-Quota: %d", DOWNLOAD_QUOTA));
}

View File

@ -91,6 +91,9 @@ public class OpenSubtitlesXmlRpc {
}
public List<OpenSubtitlesSubtitleDescriptor> searchSubtitles(Collection<Query> queryList) throws XmlRpcFault {
// abort immediately if download quota has been exceeded
OpenSubtitlesSubtitleDescriptor.checkDownloadQuota();
List<OpenSubtitlesSubtitleDescriptor> subtitles = new ArrayList<OpenSubtitlesSubtitleDescriptor>();
Map<?, ?> response = invoke("SearchSubtitles", token, queryList);