From acffe28a33800c86440d56a6015bbaa63a3a3709 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Wed, 23 Mar 2016 02:09:11 +0300 Subject: [PATCH] + Instagram api module --- services/Instagram/src/instagram/EndPoint.js | 9 +++ services/Instagram/src/instagram/Instagram.js | 16 +++++ .../Instagram/src/instagram/Subscription.js | 67 +++++++++++++++++++ services/Instagram/src/instagram/User.js | 34 ++++++++++ services/Instagram/src/instagram/index.js | 43 ++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 services/Instagram/src/instagram/EndPoint.js create mode 100644 services/Instagram/src/instagram/Instagram.js create mode 100644 services/Instagram/src/instagram/Subscription.js create mode 100644 services/Instagram/src/instagram/User.js create mode 100644 services/Instagram/src/instagram/index.js diff --git a/services/Instagram/src/instagram/EndPoint.js b/services/Instagram/src/instagram/EndPoint.js new file mode 100644 index 00000000..d9a7c9ce --- /dev/null +++ b/services/Instagram/src/instagram/EndPoint.js @@ -0,0 +1,9 @@ +export default class EndPoint{ + constructor(client) { + this.client = client; + } + + method(name){ + return this.client[name]; + } +} \ No newline at end of file diff --git a/services/Instagram/src/instagram/Instagram.js b/services/Instagram/src/instagram/Instagram.js new file mode 100644 index 00000000..fa7e189c --- /dev/null +++ b/services/Instagram/src/instagram/Instagram.js @@ -0,0 +1,16 @@ +import Subscription from './Subscription'; +import User from './User'; + +export default class Instagram { + constructor(clientId, clientSecret, accessToken, createApi) { + this.clientId = clientId; + this.clientSecret = clientSecret; + + const authenticatedApi = createApi(clientId, clientSecret, accessToken); + const api = createApi(clientId, clientSecret); + + //noinspection JSUnresolvedVariable + this.user = new User(authenticatedApi); + this.subscriptions = new Subscription(api); + } +} diff --git a/services/Instagram/src/instagram/Subscription.js b/services/Instagram/src/instagram/Subscription.js new file mode 100644 index 00000000..30ab2bf1 --- /dev/null +++ b/services/Instagram/src/instagram/Subscription.js @@ -0,0 +1,67 @@ +import EndPoint from './EndPoint'; + +export default class Subscription extends EndPoint { + list() { + const m = this.method('subscriptions'); + + return new Promise((resolve, reject) => { + m((error, result, remaining, limit) => { + if(error) return reject(error); + resolve({ + result: result, + remaining: remaining, + limit: limit + }); + }); + }); + } + + subscribeTag(tag, callbackUrl) { + const m = this.method('add_tag_subscription'); + + return new Promise((resolve, reject) => { + m(tag, callbackUrl, (error, result, remaining, limit) => { + if(error) return reject(error); + resolve({ + result: result, + remaining: remaining, + limit: limit + }); + }); + }); + } + + subscribeUser(callbackUrl) { + const m = this.method('add_user_subscription'); + + return new Promise((resolve, reject) => { + m(callbackUrl, (error, result, remaining, limit) => { + if(error) return reject(error); + resolve({ + result: result, + remaining: remaining, + limit: limit + }); + }); + }); + } + + removeAll(){ + // ig.del_subscription({ all: true }, function(err, subscriptions, remaining, limit){}); + } + + remove(id){ + // ig.del_subscription({ id: 1 }, function(err, subscriptions, remaining, limit){}); + } + +// unsubscribe: function *(id) { +// var ig = createClient(); +// var sub = thunkify(ig["del_subscription"]); +// try { +// var s = yield sub({id: id}); +// return s[0]; +// } catch (error) { +// return null; +// } +// }, +} \ No newline at end of file diff --git a/services/Instagram/src/instagram/User.js b/services/Instagram/src/instagram/User.js new file mode 100644 index 00000000..188e6fd7 --- /dev/null +++ b/services/Instagram/src/instagram/User.js @@ -0,0 +1,34 @@ +import EndPoint from './EndPoint'; + +export default class User extends EndPoint { + mediaRecent(id = null, count = null, minTime = null, maxTime = null) { + function callback(resolve, reject) { + return function (error, medias, pagination, remaining, limit) { + if (error) return reject(error); + resolve({ + medias: medias, + pagination: pagination, + remaining: remaining, + limit: limit + }); + } + } + + const options = {}; + if (count) options.count = count; + if (minTime) options.min_timestamp = minTime; + if (maxTime) options.max_timestamp = maxTime; + + if (id) { + let m = this.method('user_media_recent'); + return new Promise((resolve, reject) => { + m(id, options, callback(resolve, reject)); + }); + } else { + let m = this.method('user_self_media_recent'); + return new Promise((resolve, reject) => { + m(options, callback(resolve, reject)); + }); + } + } +} diff --git a/services/Instagram/src/instagram/index.js b/services/Instagram/src/instagram/index.js new file mode 100644 index 00000000..82921bde --- /dev/null +++ b/services/Instagram/src/instagram/index.js @@ -0,0 +1,43 @@ +import {instagram} from 'instagram-node'; + +import {clientId, clientSecret} from '../config'; +import Instagram from './Instagram'; + +export function api(accessToken){ + return new Instagram(clientId, clientSecret, accessToken, createClient); +} + +export function createAuthorizationUrl(redirectUri, scope, state) { + let params = { + scope: scope || ['basic'] + }; + if (state) params.state = state; + + let client = createClient(clientId, clientSecret); + return client['get_authorization_url'](redirectUri, params); +} + +export function authorize(code, redirectUri) { + let api = createClient(clientId, clientSecret); + let authorize = api['authorize_user']; + + return new Promise((resolve, reject) => { + authorize(code, redirectUri, (error, user) => { + if(error){ + reject(error); + }else{ + resolve(user); + } + }); + }); +} + +function createClient(clientId, clientSecret, accessToken = null) { + const client = instagram(); + client.use({ + client_id: clientId, + client_secret: clientSecret + }); + if (accessToken) client.use({access_token: accessToken}); + return client; +}