From 1f7b979c787cf5e484317d1465617bfb1bdefa28 Mon Sep 17 00:00:00 2001 From: Alex Collin Date: Sat, 24 Jan 2015 18:28:29 +0300 Subject: [PATCH 1/2] Add param "cursor" for use pagination --- README.md | 2 ++ lib/class.instagram.js | 3 +++ 2 files changed, 5 insertions(+) diff --git a/README.md b/README.md index 3956e4d..e4f3507 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ In addition, the parameters object may include two functions, one of which will complete: function(data, pagination){ // data is a javascript object/array/null matching that shipped Instagram // when available (mostly /recent), pagination is a javascript object with the pagination information + // for example use cursor pagination in the follows or other methods + Instagram.users.follows({ cursor: pagination.next_cursor }); }, error: function(errorMessage, errorObject, caller){ // errorMessage is the raised error message diff --git a/lib/class.instagram.js b/lib/class.instagram.js index 4f07118..fe48871 100644 --- a/lib/class.instagram.js +++ b/lib/class.instagram.js @@ -175,6 +175,9 @@ } if (params['path'] != null) { options['path'] = params['path']; + if(params['cursor'] != null) { + options['cursor'] = params['cursor']; + } } options['path'] = process.env['TEST_PATH_PREFIX'] != null ? "" + process.env['TEST_PATH_PREFIX'] + options['path'] : options['path']; complete = (_ref = params['complete']) != null ? _ref : params['complete'] = this._complete; From 44d5197141abc7f6d821a31ad892b736751aca8a Mon Sep 17 00:00:00 2001 From: Alex Collin Date: Wed, 28 Jan 2015 12:45:38 +0300 Subject: [PATCH 2/2] Add param "remaining" for get remaining 1 hour queries --- README.md | 4 +- lib/class.instagram.js | 475 +++++++++++++++++++++-------------------- 2 files changed, 241 insertions(+), 238 deletions(-) diff --git a/README.md b/README.md index e4f3507..69fd495 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,9 @@ In addition, the parameters object may include two functions, one of which will { name: 'blue', - complete: function(data, pagination){ + complete: function(data, pagination, remaining){ + console.log(remaining); //±5000 + // remaining(int) parameter used for get data of the remaining 1 hour queries to API Instagram // data is a javascript object/array/null matching that shipped Instagram // when available (mostly /recent), pagination is a javascript object with the pagination information // for example use cursor pagination in the follows or other methods diff --git a/lib/class.instagram.js b/lib/class.instagram.js index fe48871..56f7062 100644 --- a/lib/class.instagram.js +++ b/lib/class.instagram.js @@ -1,248 +1,249 @@ // Generated by CoffeeScript 1.3.3 -(function() { - var APIClient, InstagramAPI, querystring; - - querystring = require('querystring'); - - InstagramAPI = (function() { - - function InstagramAPI() { - var module, moduleClass, _i, _len, _ref; - this._api_version = 'v1'; - this._http_client = require('http'); - this._https_client = require('https'); - this._config = { - client_id: process.env['CLIENT_ID'] != null ? process.env['CLIENT_ID'] : 'CLIENT-ID', - client_secret: process.env['CLIENT_SECRET'] != null ? process.env['CLIENT_SECRET'] : 'CLIENT-SECRET', - callback_url: process.env['CALLBACK_URL'] != null ? process.env['CALLBACK_URL'] : 'CALLBACK-URL', - redirect_uri: process.env['REDIRECT_URI'] != null ? process.env['REDIRECT_URI'] : 'REDIRECT_URI', - access_token: process.env['ACCESS_TOKEN'] != null ? process.env['ACCESS_TOKEN'] : null, - maxSockets: 5 - }; - this._options = { - host: process.env['TEST_HOST'] != null ? process.env['TEST_HOST'] : 'api.instagram.com', - port: process.env['TEST_PORT'] != null ? process.env['TEST_PORT'] : null, - method: "GET", - path: '', - headers: { - 'User-Agent': 'Instagram Node Lib 0.0.9', - 'Accept': 'application/json', - 'Content-Length': 0 - } - }; - _ref = ['media', 'tags', 'users', 'locations', 'geographies', 'subscriptions', 'oauth']; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - module = _ref[_i]; - moduleClass = require("./class.instagram." + module); - this[module] = new moduleClass(this); - } - } - - /* - Generic Response Methods - */ - - - InstagramAPI.prototype._error = function(e, value, caller) { - var message; - if (value === void 0) { - value = '[undefined]'; - } - if (value === null) { - value = '[null]'; - } - message = "" + e + " occurred: " + value + " in " + caller; - console.log(message); - return message; - }; - - InstagramAPI.prototype._complete = function(data, pagination) { - var i; - for (i in data) { - console.log(data[i]); - } - if (pagination != null) { - return console.log(pagination); - } - }; - - /* - Shared Data Manipulation Methods - */ - - - InstagramAPI.prototype._to_querystring = function(params) { - var i, obj; - obj = {}; - for (i in params) { - if (i !== 'complete' && i !== 'error') { - obj[i] = params[i]; - } - } - return querystring.stringify(obj); - }; - - InstagramAPI.prototype._merge = function(obj1, obj2) { - var i; - for (i in obj2) { - obj1[i] = obj2[i]; - } - return obj1; - }; - - InstagramAPI.prototype._to_value = function(key, value) { - if (typeof value !== 'object') { - return value; - } - if (value[key] != null) { - return value[key]; - } - return null; - }; - - InstagramAPI.prototype._to_object = function(key, value) { - var obj; - if (typeof value === 'object' && (value[key] != null)) { - return value; - } - obj = {}; - return obj[key] = value; - }; - - InstagramAPI.prototype._clone = function(from_object) { - var property, to_object; - to_object = {}; - for (property in from_object) { - to_object[property] = from_object[property]; - } - return to_object; - }; - - /* - Set Methods - */ - - - InstagramAPI.prototype._set_maxSockets = function(value) { - if (parseInt(value) === value && value > 0) { - this._http_client.Agent.defaultMaxSockets = value; - return this._https_client.Agent.defaultMaxSockets = value; - } - }; - - InstagramAPI.prototype.set = function(property, value) { - if (this._config[property] !== void 0) { - this._config[property] = value; - } - if (this._options[property] !== void 0) { - this._options[property] = value; - } - if (typeof this["_set_" + property] === 'function') { - return this["_set_" + property](value); - } - }; - - /* - Shared Request Methods - */ - - - InstagramAPI.prototype._credentials = function(params, require) { - if (params == null) { - params = {}; - } - if (require == null) { - require = null; - } - if ((require != null) && (params[require] != null) || (params['access_token'] != null) || (params['client_id'] != null)) { - return params; - } - if (require !== null && (this._config[require] != null)) { - params[require] = this._config[require]; - } else if (this._config['access_token'] != null) { - params['access_token'] = this._config['access_token']; - } else if (this._config['client_id'] != null) { - params['client_id'] = this._config['client_id']; - } - return params; - }; - - InstagramAPI.prototype._request = function(params) { - var appResponse, complete, error, http_client, options, post_data, request, _ref, _ref1; - options = this._clone(this._options); - if (params['method'] != null) { - options['method'] = params['method']; - } - if (params['path'] != null) { - options['path'] = params['path']; - if(params['cursor'] != null) { - options['cursor'] = params['cursor']; +(function () { + var APIClient, InstagramAPI, querystring; + + querystring = require('querystring'); + + InstagramAPI = (function () { + + function InstagramAPI() { + var module, moduleClass, _i, _len, _ref; + this._api_version = 'v1'; + this._http_client = require('http'); + this._https_client = require('https'); + this._config = { + client_id: process.env['CLIENT_ID'] != null ? process.env['CLIENT_ID'] : 'CLIENT-ID', + client_secret: process.env['CLIENT_SECRET'] != null ? process.env['CLIENT_SECRET'] : 'CLIENT-SECRET', + callback_url: process.env['CALLBACK_URL'] != null ? process.env['CALLBACK_URL'] : 'CALLBACK-URL', + redirect_uri: process.env['REDIRECT_URI'] != null ? process.env['REDIRECT_URI'] : 'REDIRECT_URI', + access_token: process.env['ACCESS_TOKEN'] != null ? process.env['ACCESS_TOKEN'] : null, + maxSockets: 5 + }; + this._options = { + host: process.env['TEST_HOST'] != null ? process.env['TEST_HOST'] : 'api.instagram.com', + port: process.env['TEST_PORT'] != null ? process.env['TEST_PORT'] : null, + method: "GET", + path: '', + headers: { + 'User-Agent': 'Instagram Node Lib 0.0.9', + 'Accept': 'application/json', + 'Content-Length': 0 + } + }; + _ref = ['media', 'tags', 'users', 'locations', 'geographies', 'subscriptions', 'oauth']; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + module = _ref[_i]; + moduleClass = require("./class.instagram." + module); + this[module] = new moduleClass(this); + } } - } - options['path'] = process.env['TEST_PATH_PREFIX'] != null ? "" + process.env['TEST_PATH_PREFIX'] + options['path'] : options['path']; - complete = (_ref = params['complete']) != null ? _ref : params['complete'] = this._complete; - appResponse = params['response']; - error = (_ref1 = params['error']) != null ? _ref1 : params['error'] = this._error; - if (options['method'] !== "GET" && (params['post_data'] != null)) { - post_data = this._to_querystring(params['post_data']); - } else { - post_data = ''; - } - options['headers']['Content-Length'] = post_data.length; - if (process.env['TEST_PROTOCOL'] === 'http') { - http_client = this._http_client; - } else { - http_client = this._https_client; - } - request = http_client.request(options, function(response) { - var data; - data = ''; - response.setEncoding('utf8'); - response.on('data', function(chunk) { - return data += chunk; - }); - return response.on('end', function() { - var pagination, parsedResponse; - try { - parsedResponse = JSON.parse(data); - if ((parsedResponse != null) && (parsedResponse['meta'] != null) && parsedResponse['meta']['code'] !== 200) { - return error(parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request"); - } else if (parsedResponse['access_token'] != null) { - return complete(parsedResponse, appResponse); + + /* + Generic Response Methods + */ + + + InstagramAPI.prototype._error = function (e, value, caller) { + var message; + if (value === void 0) { + value = '[undefined]'; + } + if (value === null) { + value = '[null]'; + } + message = "" + e + " occurred: " + value + " in " + caller; + console.log(message); + return message; + }; + + InstagramAPI.prototype._complete = function (data, pagination) { + var i; + for (i in data) { + console.log(data[i]); + } + if (pagination != null) { + return console.log(pagination); + } + }; + + /* + Shared Data Manipulation Methods + */ + + + InstagramAPI.prototype._to_querystring = function (params) { + var i, obj; + obj = {}; + for (i in params) { + if (i !== 'complete' && i !== 'error') { + obj[i] = params[i]; + } + } + return querystring.stringify(obj); + }; + + InstagramAPI.prototype._merge = function (obj1, obj2) { + var i; + for (i in obj2) { + obj1[i] = obj2[i]; + } + return obj1; + }; + + InstagramAPI.prototype._to_value = function (key, value) { + if (typeof value !== 'object') { + return value; + } + if (value[key] != null) { + return value[key]; + } + return null; + }; + + InstagramAPI.prototype._to_object = function (key, value) { + var obj; + if (typeof value === 'object' && (value[key] != null)) { + return value; + } + obj = {}; + return obj[key] = value; + }; + + InstagramAPI.prototype._clone = function (from_object) { + var property, to_object; + to_object = {}; + for (property in from_object) { + to_object[property] = from_object[property]; + } + return to_object; + }; + + /* + Set Methods + */ + + + InstagramAPI.prototype._set_maxSockets = function (value) { + if (parseInt(value) === value && value > 0) { + this._http_client.Agent.defaultMaxSockets = value; + return this._https_client.Agent.defaultMaxSockets = value; + } + }; + + InstagramAPI.prototype.set = function (property, value) { + if (this._config[property] !== void 0) { + this._config[property] = value; + } + if (this._options[property] !== void 0) { + this._options[property] = value; + } + if (typeof this["_set_" + property] === 'function') { + return this["_set_" + property](value); + } + }; + + /* + Shared Request Methods + */ + + + InstagramAPI.prototype._credentials = function (params, require) { + if (params == null) { + params = {}; + } + if (require == null) { + require = null; + } + if ((require != null) && (params[require] != null) || (params['access_token'] != null) || (params['client_id'] != null)) { + return params; + } + if (require !== null && (this._config[require] != null)) { + params[require] = this._config[require]; + } else if (this._config['access_token'] != null) { + params['access_token'] = this._config['access_token']; + } else if (this._config['client_id'] != null) { + params['client_id'] = this._config['client_id']; + } + return params; + }; + + InstagramAPI.prototype._request = function (params) { + var appResponse, complete, error, http_client, options, post_data, request, _ref, _ref1; + options = this._clone(this._options); + if (params['method'] != null) { + options['method'] = params['method']; + } + if (params['path'] != null) { + options['path'] = params['path']; + if (params['cursor'] != null) { + options['cursor'] = params['cursor']; + } + } + options['path'] = process.env['TEST_PATH_PREFIX'] != null ? "" + process.env['TEST_PATH_PREFIX'] + options['path'] : options['path']; + complete = (_ref = params['complete']) != null ? _ref : params['complete'] = this._complete; + appResponse = params['response']; + error = (_ref1 = params['error']) != null ? _ref1 : params['error'] = this._error; + if (options['method'] !== "GET" && (params['post_data'] != null)) { + post_data = this._to_querystring(params['post_data']); } else { - pagination = typeof parsedResponse['pagination'] === 'undefined' ? {} : parsedResponse['pagination']; - return complete(parsedResponse['data'], pagination); + post_data = ''; } - } catch (e) { - if (appResponse != null) { - return error(e, data, '_request', appResponse); + options['headers']['Content-Length'] = post_data.length; + if (process.env['TEST_PROTOCOL'] === 'http') { + http_client = this._http_client; } else { - return error(e, data, '_request'); - } - } - }); - }); - if (post_data != null) { - request.write(post_data); - } - request.addListener('error', function(connectionException) { - if (connectionException.code !== 'ENOTCONN') { - if (appResponse != null) { - return error(connectionException, options, "_request", appResponse); - } else { - return error(connectionException, options, "_request"); - } - } - }); - return request.end(); - }; + http_client = this._https_client; + } + request = http_client.request(options, function (response) { + var data; + data = ''; + response.setEncoding('utf8'); + response.on('data', function (chunk) { + return data += chunk; + }); + return response.on('end', function () { + var pagination, parsedResponse; + try { + parsedResponse = JSON.parse(data); + var remaining = parseInt(response.headers['x-ratelimit-remaining'], 10) || 0; + if ((parsedResponse != null) && (parsedResponse['meta'] != null) && parsedResponse['meta']['code'] !== 200) { + return error(parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request"); + } else if (parsedResponse['access_token'] != null) { + return complete(parsedResponse, appResponse); + } else { + pagination = typeof parsedResponse['pagination'] === 'undefined' ? {} : parsedResponse['pagination']; + return complete(parsedResponse['data'], pagination, remaining); + } + } catch (e) { + if (appResponse != null) { + return error(e, data, '_request', appResponse); + } else { + return error(e, data, '_request'); + } + } + }); + }); + if (post_data != null) { + request.write(post_data); + } + request.addListener('error', function (connectionException) { + if (connectionException.code !== 'ENOTCONN') { + if (appResponse != null) { + return error(connectionException, options, "_request", appResponse); + } else { + return error(connectionException, options, "_request"); + } + } + }); + return request.end(); + }; - return InstagramAPI; + return InstagramAPI; - })(); + })(); - APIClient = new InstagramAPI; + APIClient = new InstagramAPI; - module.exports = APIClient; + module.exports = APIClient; }).call(this);