- instagram module
This commit is contained in:
1 parent
73d628c94c
commit
12b39c8b0b
5 files changed
-485
No files matched your search
@@ -1,40 +0,0 @@
|
|||||||
var db = require("../core/db");
|
|
||||||
var co = require("co");
|
|
||||||
var instagram = require("./instagram");
|
|
||||||
var config = require("../config");
|
|
||||||
|
|
||||||
export default function () {
|
|
||||||
return co(function*() {
|
|
||||||
yield loadUsers();
|
|
||||||
|
|
||||||
let tags = config.instagram['tags'];
|
|
||||||
console.log(tags);
|
|
||||||
|
|
||||||
yield updateSubscription(['love']);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function* loadUsers() {
|
|
||||||
let users = yield db.c('users').find({provider: 'instagram'})
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
users.forEach(u => {
|
|
||||||
instagram.initUser(u);
|
|
||||||
});
|
|
||||||
|
|
||||||
return users;
|
|
||||||
}
|
|
||||||
|
|
||||||
function* updateSubscription(tags) {
|
|
||||||
console.log(tags);
|
|
||||||
|
|
||||||
//return tags.map(tag => {
|
|
||||||
// let callbackUrl = config.instagram['tag_callback'];
|
|
||||||
//
|
|
||||||
// return co(function *() {
|
|
||||||
// return yield instagram.api.subscribe(tag, callbackUrl);
|
|
||||||
// });
|
|
||||||
//}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
var EventEmitter = require("events").EventEmitter;
|
|
||||||
var thunkify = require("thunkify");
|
|
||||||
import config from "../config";
|
|
||||||
var db = require("../core/db");
|
|
||||||
var instagram = require("instagram-node");
|
|
||||||
|
|
||||||
function Instagram(options) {
|
|
||||||
this.client_id = options.client_id;
|
|
||||||
this.client_secret = options.client_secret;
|
|
||||||
this.redirect_uri = options.redirect_uri;
|
|
||||||
this.default_user = options.default_user;
|
|
||||||
this.clients = {};
|
|
||||||
this.users = {};
|
|
||||||
|
|
||||||
EventEmitter.call(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
Instagram.prototype = Object.create(EventEmitter.prototype);
|
|
||||||
|
|
||||||
Instagram.prototype.authorizationURL = function (scope, state) {
|
|
||||||
var params = {};
|
|
||||||
params.scope = scope || ["basic"];
|
|
||||||
if (state) {
|
|
||||||
params.state = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
var client = createClient();
|
|
||||||
return client["get_authorization_url"](this.redirect_uri, params);
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.addClient = function *(code, params) {
|
|
||||||
var token = params.access_token;
|
|
||||||
var item = params.user;
|
|
||||||
item.code = code;
|
|
||||||
item.access_token = token;
|
|
||||||
item.provider = "instagram";
|
|
||||||
yield db.c("users").update({provider: "instagram", id: params.user.id}, item, {upsert: true});
|
|
||||||
|
|
||||||
return this.initUser(item);
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.initUser = function (user) {
|
|
||||||
var token = user.access_token;
|
|
||||||
this.clients[user.code] = createClient(token);
|
|
||||||
this.users[user.code] = user;
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.getUsers = function () {
|
|
||||||
var users = this.users;
|
|
||||||
return Object.keys(users)
|
|
||||||
.map(function (code) {
|
|
||||||
return users[code];
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.update = function (body) {
|
|
||||||
this.emit('update', body);
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.client = function (code) {
|
|
||||||
if (code && code in this.clients) {
|
|
||||||
return this.clients[code];
|
|
||||||
} else {
|
|
||||||
return createClient();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.user = function (code) {
|
|
||||||
if (code && code in this.users) {
|
|
||||||
return this.users[code];
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.authorize = function *(code) {
|
|
||||||
var api = createClient();
|
|
||||||
var authorize = thunkify(api["authorize_user"]);
|
|
||||||
var result = yield authorize(code, this.redirect_uri);
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
yield this.addClient(code, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.getUser = function *() {
|
|
||||||
if (this.default_user) {
|
|
||||||
var du = yield db.c("users").findOne({username: this.default_user});
|
|
||||||
if (du) {
|
|
||||||
return du;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return yield db.c("users").findOne({provider: "instagram"});
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.getUsers = function *() {
|
|
||||||
return yield db.c("users").find({provider: "instagram"}).toArray();
|
|
||||||
};
|
|
||||||
|
|
||||||
Instagram.prototype.api = {
|
|
||||||
subscriptions: function *() {
|
|
||||||
var ig = createClient();
|
|
||||||
var subs = thunkify(ig["subscriptions"]);
|
|
||||||
try {
|
|
||||||
var s = yield subs();
|
|
||||||
return s[0];
|
|
||||||
} catch (error) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
subscribe: function *(tag, callbackURL) {
|
|
||||||
var ig = createClient();
|
|
||||||
var sub = thunkify(ig["add_tag_subscription"]);
|
|
||||||
try {
|
|
||||||
var s = yield sub(tag, callbackURL);
|
|
||||||
return s[0];
|
|
||||||
} catch (error) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
tagMediaRecent: function *(client, tag, count) {
|
|
||||||
var tag_media_recent = thunkify(client["tag_media_recent"]);
|
|
||||||
|
|
||||||
try{
|
|
||||||
var result = yield tag_media_recent(tag, {count: count});
|
|
||||||
return {
|
|
||||||
medias: result[0],
|
|
||||||
pagination: result[1],
|
|
||||||
remaining: result[2],
|
|
||||||
limit: result[3]
|
|
||||||
}
|
|
||||||
}catch(error){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = new Instagram(config.instagram);
|
|
||||||
|
|
||||||
function createClient(token) {
|
|
||||||
var client = instagram.instagram({}, {});
|
|
||||||
if (token) {
|
|
||||||
client.use({access_token: token});
|
|
||||||
} else {
|
|
||||||
client.use({
|
|
||||||
client_id: config.instagram.client_id,
|
|
||||||
client_secret: config.instagram.client_secret
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
/**
|
|
||||||
* Created by tmshv on 14/03/15.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var publisher = require("./publish");
|
|
||||||
|
|
||||||
module.exports = function (app) {
|
|
||||||
app.io.use(function *(next) {
|
|
||||||
publisher.on("post", function (post) {
|
|
||||||
console.log("IO POST", post);
|
|
||||||
app.io.emit("post", post);
|
|
||||||
});
|
|
||||||
|
|
||||||
yield* next;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
/**
|
|
||||||
* Created by tmshv on 10/03/15.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var co = require("co");
|
|
||||||
var util = require("util");
|
|
||||||
var EventEmitter = require("events").EventEmitter;
|
|
||||||
var instagram = require("./instagram");
|
|
||||||
var config = require("../config");
|
|
||||||
var db = require("../core/db");
|
|
||||||
|
|
||||||
function Publisher() {
|
|
||||||
EventEmitter.call(this);
|
|
||||||
|
|
||||||
var self = this;
|
|
||||||
instagram.on("update", function (data) {
|
|
||||||
var tag = config.instagram["tags"][0];
|
|
||||||
|
|
||||||
findInstagrams(tag)
|
|
||||||
.catch(function (error) {
|
|
||||||
console.log(error);
|
|
||||||
})
|
|
||||||
.then(function (posts) {
|
|
||||||
var ids = posts.map(function (gram) {
|
|
||||||
return gram.data.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
return uploadInstagrams(posts)
|
|
||||||
.then(function () {
|
|
||||||
return db.c("timeline").find({"data.id": {$in: ids}}).toArray();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then(function (posts) {
|
|
||||||
self.emit("post", posts);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
util.inherits(Publisher, EventEmitter);
|
|
||||||
|
|
||||||
module.exports = new Publisher();
|
|
||||||
|
|
||||||
function *fbt(client, tag) {
|
|
||||||
var param = yield instagram.api.tagMediaRecent(client, tag, 10);
|
|
||||||
return param.medias.map(toInstagram);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toInstagram(gram) {
|
|
||||||
var post = {};
|
|
||||||
|
|
||||||
try {
|
|
||||||
post.body = gram.caption.text;
|
|
||||||
} catch (error) {
|
|
||||||
post.body = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
post.type = "instagram";
|
|
||||||
post.tags = gram.tags;
|
|
||||||
post.date = new Date(parseInt(gram["created_time"]) * 1000);
|
|
||||||
|
|
||||||
post.data = {
|
|
||||||
id: gram.id,
|
|
||||||
author: gram.user["username"],
|
|
||||||
image: gram.images,
|
|
||||||
url: gram.link
|
|
||||||
};
|
|
||||||
|
|
||||||
return post;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findInstagrams(tag){
|
|
||||||
return co(function *() {
|
|
||||||
var user = yield instagram.getUser();
|
|
||||||
if (user) {
|
|
||||||
var client = instagram.client(user.code);
|
|
||||||
var permitted_users = yield instagram.getUsers();
|
|
||||||
permitted_users = permitted_users.map(function (user) {
|
|
||||||
return user["username"];
|
|
||||||
});
|
|
||||||
|
|
||||||
var medias = yield fbt(client, tag);
|
|
||||||
//medias = medias.filter(function (gram) {
|
|
||||||
// return permitted_users.indexOf(gram.data.author) > -1;
|
|
||||||
//});
|
|
||||||
|
|
||||||
return medias;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function uploadInstagrams(list){
|
|
||||||
return Promise.all(
|
|
||||||
list.map(function (g) {
|
|
||||||
return db.c("timeline").update({"data.id": g.data.id}, g, {upsert: true});
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
var path = require("path");
|
|
||||||
var route = require("koa-route");
|
|
||||||
var router = require("../routes");
|
|
||||||
var instagram = require("../instagram/instagram");
|
|
||||||
|
|
||||||
function expire(time) {
|
|
||||||
return time || new Date(Date.now() + 1000 * 60 * 60 * 24 * 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function (app) {
|
|
||||||
app.use(function *(next) {
|
|
||||||
let code = this.cookies.get('instagram_code');
|
|
||||||
if (code) {
|
|
||||||
let client = instagram.client(code);
|
|
||||||
let user = instagram.user(code);
|
|
||||||
if (client && user) {
|
|
||||||
this.instagram = {
|
|
||||||
client: client,
|
|
||||||
user: user
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
yield* next;
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
route.get('/instagram', function *() {
|
|
||||||
let invite = this.cookies.get('invite_code') || this.query.code;
|
|
||||||
|
|
||||||
if(!this.instagram) {
|
|
||||||
if(invite == undefined){
|
|
||||||
this.body = 'invite required';
|
|
||||||
this.status = 401;
|
|
||||||
}else if(invite == 'LOL'){
|
|
||||||
this.cookies.set('invite_code', invite, {expires: expire()});
|
|
||||||
|
|
||||||
//todo: invalidate invite
|
|
||||||
|
|
||||||
yield router.index(
|
|
||||||
path.join(__dirname, '../templates/instagram.html')
|
|
||||||
);
|
|
||||||
}else{
|
|
||||||
this.body = 'invite ist correct';
|
|
||||||
this.status = 401;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
yield router.index(
|
|
||||||
path.join(__dirname, '../templates/instagram.html')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
route.get("/instagram/auth", function *() {
|
|
||||||
let invite = this.cookies.get('invite_code');
|
|
||||||
if(invite) {
|
|
||||||
let url = instagram.authorizationURL(null, '/instagram');
|
|
||||||
this.redirect(url);
|
|
||||||
}else{
|
|
||||||
this.status = 401;
|
|
||||||
this.body = 'invite required';
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
route.get('/instagram/user', function *() {
|
|
||||||
let code = this.cookies.get('instagram_code');
|
|
||||||
let user = instagram.user(code);
|
|
||||||
|
|
||||||
if(user) {
|
|
||||||
this.body = user;
|
|
||||||
}else{
|
|
||||||
this.status = 404;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
route.get('/instagram/auth/callback', function *() {
|
|
||||||
let state = this.query.state || '/';
|
|
||||||
let code = this.query.code;
|
|
||||||
|
|
||||||
let client;
|
|
||||||
try {
|
|
||||||
client = yield instagram.authorize(code);
|
|
||||||
} catch (e) {
|
|
||||||
client = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client) {
|
|
||||||
this.cookies.set('instagram_code', code, {expires: expire()});
|
|
||||||
this.redirect(state);
|
|
||||||
} else {
|
|
||||||
this.status = 401;
|
|
||||||
this.redirect(state);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(route.get('/instagram/subscription/list', function *() {
|
|
||||||
let result = yield instagram.api.subscriptions();
|
|
||||||
if(result) {
|
|
||||||
this.body = result;
|
|
||||||
}else{
|
|
||||||
this.status = 500;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
app.use(route.get('/instagram/subscription/add', function *() {
|
|
||||||
let tag = this.query.tag;
|
|
||||||
let cu = this.query.url;
|
|
||||||
|
|
||||||
var result = yield instagram.api.subscribe(tag, cu);
|
|
||||||
if(result) {
|
|
||||||
this.body = result;
|
|
||||||
}else{
|
|
||||||
this.status = 500;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
app.use(route.get('/instagram/subscription/delete', function *() {
|
|
||||||
let id = this.query.id;
|
|
||||||
|
|
||||||
var result = yield instagram.api.unsubscribe(id);
|
|
||||||
if(result) {
|
|
||||||
this.body = result;
|
|
||||||
}else{
|
|
||||||
this.status = 500;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
app.use(route.get('/instagram/callback/:rnd', function *() {
|
|
||||||
if(this.query['hub.mode'] == 'subscribe'){
|
|
||||||
this.body = this.query['hub.challenge'];
|
|
||||||
}else{
|
|
||||||
this.status = 400;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
app.use(route.post('/instagram/callback/:rnd', function *(rnd) {
|
|
||||||
let sign = this.header['x-hub-signature'];
|
|
||||||
//console.log(this.headers);
|
|
||||||
|
|
||||||
let body = this.request.body;
|
|
||||||
|
|
||||||
//var hmac = crypto.createHmac("sha1", instagram.client_secret);
|
|
||||||
//hmac.update(body);
|
|
||||||
//var shasum = hmac.digest("hex");
|
|
||||||
|
|
||||||
|
|
||||||
//console.log("X-Hub-Signature", sign);
|
|
||||||
//console.log("Hmac", shasum);
|
|
||||||
//console.log();
|
|
||||||
|
|
||||||
//if(shasum.digest("hex") == sign) {
|
|
||||||
instagram.update(body);
|
|
||||||
//}
|
|
||||||
|
|
||||||
this.body = '';
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
Reference in new issue
Block a user