commit 6f31a3d31a010fe4d409b91e970ee582d0d2fc60
parent 1c3890e837dbbb6b65c74b42182cdb0fb7775328
Author: Simon Kornblith <simon@simonster.com>
Date: Sat, 26 Jan 2013 02:06:00 -0500
Limit Google Scholar queries to one per 1.5 seconds
Hopefully, this will stop our users from getting locked out.
Diffstat:
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js
@@ -28,10 +28,6 @@
*/
Components.utils.import("resource://zotero/q.js");
-const SUCCESS_IMAGE = "chrome://zotero/skin/tick.png";
-const FAILURE_IMAGE = "chrome://zotero/skin/cross.png";
-const LOADING_IMAGE = "chrome://global/skin/icons/loading_16.png";
-
/**
* Front end for recognizing PDFs
* @namespace
@@ -73,6 +69,7 @@ var Zotero_RecognizePDF = new function() {
*/
this.recognize = function(file, libraryID) {
const MAX_PAGES = 7;
+ const GOOGLE_SCHOLAR_QUERY_DELAY = 1500; // in ms
return _extractText(file, MAX_PAGES).then(function(lines) {
// Look for DOI - Use only first 80 lines to avoid catching article references
@@ -173,9 +170,15 @@ var Zotero_RecognizePDF = new function() {
Zotero.debug("RecognizePDF: Query string "+queryString);
- // pass query string to Google Scholar and translate
- var url = "http://scholar.google.com/scholar?q="+encodeURIComponent(queryString)+"&hl=en&lr=&btnG=Search";
- return Zotero.HTTP.promise("GET", url, {"responseType":"document"})
+ var url = "http://scholar.google.com/scholar?q="+encodeURIComponent(queryString)+"&hl=en&lr=&btnG=Search",
+ delay = GOOGLE_SCHOLAR_QUERY_DELAY - (Date.now() - Zotero.HTTP.lastGoogleScholarQueryTime);
+
+ // Delay
+ return (delay > 0 ? Q.delay(delay) : Q.when())
+ .then(function() {
+ Zotero.HTTP.lastGoogleScholarQueryTime = Date.now();
+ return Zotero.HTTP.promise("GET", url, {"responseType":"document"})
+ })
.then(function(xmlhttp) {
var deferred = Q.defer();
@@ -186,7 +189,7 @@ var Zotero_RecognizePDF = new function() {
if(detected.length) {
deferred.resolve(_promiseTranslate(translate, libraryID));
} else {
- deferred.reject("Detection with Google Scholar failed");
+ deferred.reject(new Zotero.Exception.Alert("recognizePDF.noMatches"));
}
});
translate.getTranslators();
@@ -194,7 +197,7 @@ var Zotero_RecognizePDF = new function() {
return deferred.promise;
}, function(e) {
if(e instanceof Zotero.HTTP.UnexpectedStatusException && e.status == 403) {
- throw new Zotero.Exception.Alert("recognizePDF.recognizePDF.limit");
+ throw new Zotero.Exception.Alert("recognizePDF.limit");
}
throw e;
});
@@ -421,6 +424,10 @@ Zotero_RecognizePDF.ItemRecognizer.prototype._onWindowLoaded = function() {
* @private
*/
Zotero_RecognizePDF.ItemRecognizer.prototype._recognizeItem = function() {
+ const SUCCESS_IMAGE = "chrome://zotero/skin/tick.png";
+ const FAILURE_IMAGE = "chrome://zotero/skin/cross.png";
+ const LOADING_IMAGE = "chrome://global/skin/icons/loading_16.png";
+
if(!this._items.length) {
this._done();
return;
diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js
@@ -3,6 +3,8 @@
* @namespace
*/
Zotero.HTTP = new function() {
+ this.lastGoogleScholarQueryTime = 0;
+
/**
* Exception returned for unexpected status when promise* is used
* @constructor