Advertisement
stuppid_bot

Untitled

Feb 4th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (typeof Qt != "undefined") {
  2.   Qt.include("common.js");
  3.   Qt.include("http.js");
  4.   Qt.include("eventemitter.js");
  5.   var md5 = Qt.md5;
  6. } else if (typeof md5 != "function") {
  7.   function md5(str) {
  8.     console.error("MD5 encoding not supported!");
  9.   }
  10. }
  11.  
  12. var API_HOST = "api.vk.com";
  13. var API_PATH = "/method/";
  14. var API_DELAY = 334;
  15.  
  16. function Api(options) {
  17.   options = options || {};
  18.   this.accessToken = options.accessToken;
  19.   this.version = options.version;
  20.   this.delay = options.delay || API_DELAY;
  21. }
  22.  
  23. Api.prototype = {
  24.   request: null,
  25.   _processing: false,
  26.   _requestQueue: [],
  27.   _requestTime: 0,
  28.  
  29.   // public
  30.   call: function(method, params, options) {
  31.     var request = new ApiRequest(this, method, params, options);
  32.     if (this._processing) {
  33.       //
  34.     }
  35.     if (this._locked) {
  36.       console.log("Add request to queue");
  37.       this._requestQueue.push(request);
  38.     } else {
  39.       // Запоминает запрос, т.к. он может еще понадобиться
  40.       this.request = request;
  41.       request.send();
  42.     }
  43.   },
  44.  
  45.   execute: function(code, options) {
  46.     this.call('execute', {code: code}, options);
  47.   },
  48.  
  49.   cancelAllRequests: function() {
  50.     console.log("Cancel all requests");
  51.     this._requestQueue = [];
  52.     this._unlock();
  53.   },
  54.  
  55.   _processRequestQueue: function() {
  56.     var request = this._requestQueue.shift();
  57.     if (request) {
  58.       request.send();
  59.     } else {
  60.       console.log("Requests queue empty");
  61.       this._unlock();
  62.     }
  63.   },
  64.  
  65.   _lock: function() {
  66.     this._locked = true;
  67.   },
  68.  
  69.   _unlock: function() {
  70.     this._locked = false;
  71.   },
  72. };
  73.  
  74. EventEmitter.mixin(Api);
  75.  
  76. function ApiRequest(api, method, params, options) {
  77.   this.api = api;
  78.   this.method = method;
  79.   this.params = extend({}, params);
  80.   this.done = options.done;
  81.   this.fail = options.fail;
  82.   this.context = options.context;
  83.   this.delay = options.delay || this.api.delay;
  84. }
  85.  
  86. ApiRequest.prototype = {
  87.   _processing: true,
  88.  
  89.   send: function() {
  90.     console.log("Send request");
  91.     this.api._lock();
  92.     var params = extend({}, this.api.extraParams, this.params);
  93.     if (this.api.version) {
  94.       params.v = this.api.version;
  95.     }
  96.     var scheme = "https";
  97.     if (this.api.accessToken) {
  98.       params.access_token = this.api.accessToken.accessToken;
  99.       if (this.api.accessToken.secret) {
  100.         // https://vk.com/dev/api_nohttps
  101.         scheme = "http";
  102.         if (params.sig) {
  103.           delete params.sig;
  104.         }
  105.         var str = formatStr(
  106.           "{0}{1}?{2}{3}",
  107.           API_PATH,
  108.           this.method,
  109.           encodeQueryParams(params),
  110.           this.api.accessToken.secret
  111.         );
  112.         params.sig = md5(str);
  113.       }
  114.     }
  115.     var format = "{0}://{1}{2}{3}";
  116.     var endpoint = formatStr(format, scheme, API_HOST, API_PATH, this.method);
  117.     var nextRequestTime = this.api._requestTime + this.delay;
  118.     var now = Date.now();
  119.     var timeout = nextRequestTime > now ? nextRequestTime - now : 0;
  120.     console.log("timeout = %sms", timeout);
  121.     var self = this;
  122.     setTimeout(function() {
  123.       post(endpoint, params, function(data) {
  124.         self.api._requestTime = Date.now();
  125.         var error = data.error;
  126.         var response = data.response;
  127.         if (error) {
  128.           console.error(self._formatError(error));
  129.           self.api.emit('error', error);
  130.         }
  131.         if (self._processing) {
  132.           if (error && typeof self.fail == "function") {
  133.             self.fail.call(self.context, error);
  134.           }
  135.           if (response && typeof self.done == "function") {
  136.             self.done.call(self.context, response);
  137.           }
  138.           self.next();
  139.         }
  140.       });
  141.     }, timeout);
  142.   },
  143.  
  144.   // Переходит к следующему запросу в очереди
  145.   next: function() {
  146.     this.api._processRequestQueue();
  147.   },
  148.  
  149.   // Останавливает обработку всех запросов
  150.   stopProcessing: function() {
  151.     console.log("Stop all requests processing");
  152.     this._processing = false;
  153.   },
  154.  
  155.   // Повторить запрос через delay миллисекунд
  156.   retry: function(delay) {
  157.     this.processing = true;
  158.     if (delay != null) {
  159.       this.delay = delay;
  160.     }
  161.     this.send();
  162.   },
  163.  
  164.   _formatError: function(error) {
  165.     var params = {};
  166.     for (var i = 0; i < error.request_params.length; ++i) {
  167.       params[error.request_params[i].key] = error.request_params[i].value;
  168.     }
  169.     var method = params.method;
  170.     delete params.method;
  171.     return template(
  172.       'An error occurred while calling method "{method}"' +
  173.       ' with parameters: {params}' +
  174.       ' (Error Code: {code}, Message: {msg})',
  175.       {
  176.         code: error.error_code,
  177.         msg: error.error_msg,
  178.         method: method,
  179.         params: this._formatParams(params)
  180.       }
  181.     );
  182.   },
  183.  
  184.   _formatParams: function(params) {
  185.     return JSON.stringify(params, function(k, v) {
  186.       if (k == "access_token") {
  187.         return "**CENSORED**";
  188.       }
  189.       return v;
  190.     });
  191.   },
  192. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement