+ Instagram api module

This commit is contained in:
tmshv committed 2016-03-23 02:09:11 +03:00
1 parent 5c0ce10da2
commit acffe28a33
5 files changed
+169

No files matched your search

@@ -0,0 +1,9 @@
export default class EndPoint{
constructor(client) {
this.client = client;
}
method(name){
return this.client[name];
}
}
@@ -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);
}
}
@@ -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;
// }
// },
}
+34
View File
@@ -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));
});
}
}
}
+43
View File
@@ -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;
}