instagram now works as prototype;
This commit is contained in:
1 parent
67ea5ea96c
commit
17923e3730
2 files changed
+172
-59
No files matched your search
+147
-48
@@ -7,6 +7,7 @@ var thunkify = require("thunkify");
|
|||||||
var koa = require("koa");
|
var koa = require("koa");
|
||||||
var route = require("koa-route");
|
var route = require("koa-route");
|
||||||
var logger = require("koa-logger");
|
var logger = require("koa-logger");
|
||||||
|
var bodyParser = require("koa-bodyparser");
|
||||||
|
|
||||||
var config = require("../config");
|
var config = require("../config");
|
||||||
var db = require("../core/db");
|
var db = require("../core/db");
|
||||||
@@ -21,23 +22,51 @@ function server(port) {
|
|||||||
var app = koa();
|
var app = koa();
|
||||||
app.proxy = true;
|
app.proxy = true;
|
||||||
app.use(logger());
|
app.use(logger());
|
||||||
|
app.use(bodyParser());
|
||||||
|
|
||||||
var instagram = require("../core/instagram");
|
var instagram = require("../core/instagram");
|
||||||
|
|
||||||
|
app.use(function *(next) {
|
||||||
|
var code = this.cookies.get("instagram_code");
|
||||||
|
if (code) {
|
||||||
|
var client = instagram.client(code);
|
||||||
|
var user = instagram.user(code);
|
||||||
|
if (client && user) {
|
||||||
|
this.instagram = {
|
||||||
|
client: client,
|
||||||
|
user: user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield* next;
|
||||||
|
});
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
route.get("/", function *() {
|
route.get("/", function *() {
|
||||||
if ("instagram_code" in this.cookies) {
|
if (this.instagram) {
|
||||||
var client = instagram.client(this.cookies["instagram_code"]);
|
yield instagramSandbox(this.instagram, this.query);
|
||||||
|
|
||||||
yield instagramSandbox(client);
|
|
||||||
} else {
|
} else {
|
||||||
var ig = instagram.client();
|
var client = instagram.client();
|
||||||
var url = ig["get_authorization_url"](config.instagram.redirect_uri, {
|
var url = client["get_authorization_url"](config.instagram.redirect_uri, {
|
||||||
scope: ["basic"],
|
scope: ["basic"],
|
||||||
state: "hello"
|
state: "hello"
|
||||||
});
|
});
|
||||||
this.redirect(url);
|
this.redirect(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//var code = this.cookies.get("instagram_code");
|
||||||
|
//if (code) {
|
||||||
|
// var client = instagram.client(code);
|
||||||
|
// yield instagramSandbox(client);
|
||||||
|
//} else {
|
||||||
|
// var ig = instagram.client();
|
||||||
|
// var url = ig["get_authorization_url"](config.instagram.redirect_uri, {
|
||||||
|
// scope: ["basic"],
|
||||||
|
// state: "hello"
|
||||||
|
// });
|
||||||
|
// this.redirect(url);
|
||||||
|
//}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -79,66 +108,136 @@ function server(port) {
|
|||||||
app.use(
|
app.use(
|
||||||
route.get("/auth/instagram/callback", function *() {
|
route.get("/auth/instagram/callback", function *() {
|
||||||
var code = this.query.code;
|
var code = this.query.code;
|
||||||
instagram.authorize(code)
|
var client = yield instagram.authorize(code);
|
||||||
.catch(function (error) {
|
|
||||||
console.log(error.body);
|
//.catch(function (error) {
|
||||||
this.status = 500;
|
// console.log(error.body);
|
||||||
})
|
// this.status = 500;
|
||||||
.then(function (client) {
|
//})
|
||||||
this.cookie("instagram_code", code);
|
//.then(function (client) {
|
||||||
this.redirect("/");
|
// this.cookie("instagram_code", code);
|
||||||
});
|
// this.redirect("/");
|
||||||
|
//});
|
||||||
|
if (client) {
|
||||||
|
this.cookies.set("instagram_code", code);
|
||||||
|
this.redirect("/");
|
||||||
|
} else {
|
||||||
|
this.status = 401;
|
||||||
|
this.redirect("/");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.use(route.get("/instagram/subscription", function *() {
|
||||||
|
var hub_challenge = this.query["hub.challenge"];
|
||||||
|
//var hub_verify_token = req.query["hub.verify_token"];
|
||||||
|
console.log("real time get", hub_challenge);
|
||||||
|
this.body = hub_challenge;
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.use(route.post("/instagram/subscription", function *() {
|
||||||
|
console.log("real time:");
|
||||||
|
console.log(this.request.body);
|
||||||
|
this.body = "";
|
||||||
|
|
||||||
|
//[ { changed_aspect: 'media',
|
||||||
|
// object: 'tag',
|
||||||
|
// object_id: 'testing',
|
||||||
|
// time: 1425951815,
|
||||||
|
// subscription_id: 17157947,
|
||||||
|
// data: {} } ]
|
||||||
|
}));
|
||||||
|
|
||||||
app.listen(port);
|
app.listen(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
function instagramSandbox(ig) {
|
function instagramSandbox(gram, params) {
|
||||||
|
var ig = gram.client;
|
||||||
|
var current_user = gram.user;
|
||||||
|
|
||||||
function *user_search() {
|
var methods = {
|
||||||
var user_search = thunkify(ig["user_search"]);
|
user_search: function (user) {
|
||||||
var s = yield user_search("hudozka");
|
return function *() {
|
||||||
this.body = s[0];
|
var user_search = thunkify(ig["user_search"]);
|
||||||
}
|
var s = yield user_search(user);
|
||||||
|
this.body = s[0];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
user: function (id) {
|
||||||
|
return function *() {
|
||||||
|
var user = thunkify(ig["user"]);
|
||||||
|
try {
|
||||||
|
var s = yield user(id);
|
||||||
|
this.body = s[0];
|
||||||
|
} catch (error) {
|
||||||
|
this.status = 500;
|
||||||
|
this.body = error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
findByTag: function(tag) {
|
||||||
|
return function *() {
|
||||||
|
var param = yield findByTag(ig, tag, 50);
|
||||||
|
|
||||||
|
var grams = param.medias.map(toInstagram);
|
||||||
|
var ids = grams.map(function (g) {
|
||||||
|
return g.data.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
yield grams.map(function (g) {
|
||||||
|
return db.c("posts").update({"data.id": g.data.id}, g, {upsert: true});
|
||||||
|
});
|
||||||
|
|
||||||
|
this.body = yield db.c("posts").find({"data.id": {$in: ids}}).toArray();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
subscriptions: function(){
|
||||||
|
return function *(){
|
||||||
|
var ig = instagram.client();
|
||||||
|
var subs = thunkify(ig["subscriptions"]);
|
||||||
|
try {
|
||||||
|
var s = yield subs();
|
||||||
|
this.body = s[0];
|
||||||
|
} catch (error) {
|
||||||
|
this.status = 500;
|
||||||
|
this.body = error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
addTagSubscription: function(tag){
|
||||||
|
return function *(){
|
||||||
|
var ig = instagram.client();
|
||||||
|
var sub = thunkify(ig["add_tag_subscription"]);
|
||||||
|
//ig.add_tag_subscription("hello", "http://home.tmshv.ru:1337/instagram/tag/hello", [], function(err, result, remaining, limit){
|
||||||
|
|
||||||
|
try {
|
||||||
|
var s = yield sub(tag, "https://slwjtsrtlh.localtunnel.me/instagram/subscription");
|
||||||
|
this.body = s[0];
|
||||||
|
} catch (error) {
|
||||||
|
this.status = 500;
|
||||||
|
this.body = error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// // ig.user_media_recent("1549382451", [], function (err, medias, pagination, remaining, limit) {
|
// // ig.user_media_recent("1549382451", [], function (err, medias, pagination, remaining, limit) {
|
||||||
// // console.log(err, medias, pagination, remaining, limit);
|
// // console.log(err, medias, pagination, remaining, limit);
|
||||||
// // res.json(users);
|
// // res.json(users);
|
||||||
// // });
|
// // });
|
||||||
//
|
//
|
||||||
// //ig.subscriptions(function(err, result, remaining, limit){
|
|
||||||
// // console.log(err, result, remaining, limit);
|
|
||||||
// // res.json(result);
|
|
||||||
// //});
|
|
||||||
//
|
|
||||||
// //ig.del_subscription({id: 14180526}, function (err, subscriptions, limit) {
|
// //ig.del_subscription({id: 14180526}, function (err, subscriptions, limit) {
|
||||||
// // console.log(err, subscriptions, limit);
|
// // console.log(err, subscriptions, limit);
|
||||||
// // res.json(subscriptions);
|
// // res.json(subscriptions);
|
||||||
// //});
|
|
||||||
//
|
|
||||||
// ///* OPTIONS: { [verify_token] }
|
|
||||||
// // ig.add_tag_subscription("hello", "http://home.tmshv.ru:1337/instagram/tag/hello", [], function(err, result, remaining, limit){
|
|
||||||
// // console.log(err, result, remaining, limit);
|
|
||||||
// // res.json(result);
|
|
||||||
// //});
|
// //});
|
||||||
|
|
||||||
function *fbt() {
|
var method = params.method || "user";
|
||||||
var param = yield findByTag(ig, "shlb_hudozka", 50);
|
var value = params.value || current_user.id;
|
||||||
|
return methods[method](value);
|
||||||
var grams = param.medias.map(toInstagram);
|
|
||||||
var ids = grams.map(function (g) {
|
|
||||||
return g.data.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
yield grams.map(function (g) {
|
|
||||||
return db.c("posts").update({"data.id": g.data.id}, g, {upsert: true});
|
|
||||||
});
|
|
||||||
|
|
||||||
this.body = yield db.c("posts").find({"data.id": {$in: ids}}).toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
return fbt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function *findByTag(ig, tag, count) {
|
function *findByTag(ig, tag, count) {
|
||||||
|
|||||||
+25
-11
@@ -3,28 +3,42 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var config = require("../config");
|
var config = require("../config");
|
||||||
|
var instagram = require("instagram-node");
|
||||||
var auth_clients = {};
|
var auth_clients = {};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
client: function(param){
|
client: function(code){
|
||||||
if(param in auth_clients) {
|
if(code && code in auth_clients) {
|
||||||
return auth_clients[param];
|
return auth_clients[code].client;
|
||||||
}else{
|
}else{
|
||||||
return createClient();
|
return createClient();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
user: function(code){
|
||||||
|
if(code in auth_clients) {
|
||||||
|
return auth_clients[code].user;
|
||||||
|
}else{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
authorize: function(code){
|
authorize: function(code){
|
||||||
return new Promise(function(resolve, reject){
|
return new Promise(function(resolve, reject){
|
||||||
var api = createClient();
|
var api = createClient();
|
||||||
api.authorize_user(code, config.instagram.redirect_uri, function (err, result) {
|
api.authorize_user(code, config.instagram.redirect_uri, function (err, result) {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
else {
|
else {
|
||||||
|
var user = result.user;
|
||||||
var token = result.access_token;
|
var token = result.access_token;
|
||||||
var c = createClient(token);
|
var client = createClient(token);
|
||||||
auth_clients[code] = c;
|
|
||||||
resolve(c);
|
auth_clients[code] = {
|
||||||
|
client: client,
|
||||||
|
user: user
|
||||||
|
};
|
||||||
|
|
||||||
|
resolve(client);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -32,14 +46,14 @@ module.exports = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function createClient(token) {
|
function createClient(token) {
|
||||||
var api = require('instagram-node').instagram();
|
var client = instagram.instagram({}, {});
|
||||||
if (token) {
|
if (token) {
|
||||||
api.use({access_token: token});
|
client.use({access_token: token});
|
||||||
} else {
|
} else {
|
||||||
api.use({
|
client.use({
|
||||||
client_id: config.instagram.client_id,
|
client_id: config.instagram.client_id,
|
||||||
client_secret: config.instagram.client_secret
|
client_secret: config.instagram.client_secret
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return api;
|
return client;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user