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 route = require("koa-route");
|
||||
var logger = require("koa-logger");
|
||||
var bodyParser = require("koa-bodyparser");
|
||||
|
||||
var config = require("../config");
|
||||
var db = require("../core/db");
|
||||
@@ -21,23 +22,51 @@ function server(port) {
|
||||
var app = koa();
|
||||
app.proxy = true;
|
||||
app.use(logger());
|
||||
app.use(bodyParser());
|
||||
|
||||
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(
|
||||
route.get("/", function *() {
|
||||
if ("instagram_code" in this.cookies) {
|
||||
var client = instagram.client(this.cookies["instagram_code"]);
|
||||
|
||||
yield instagramSandbox(client);
|
||||
if (this.instagram) {
|
||||
yield instagramSandbox(this.instagram, this.query);
|
||||
} else {
|
||||
var ig = instagram.client();
|
||||
var url = ig["get_authorization_url"](config.instagram.redirect_uri, {
|
||||
var client = instagram.client();
|
||||
var url = client["get_authorization_url"](config.instagram.redirect_uri, {
|
||||
scope: ["basic"],
|
||||
state: "hello"
|
||||
});
|
||||
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(
|
||||
route.get("/auth/instagram/callback", function *() {
|
||||
var code = this.query.code;
|
||||
instagram.authorize(code)
|
||||
.catch(function (error) {
|
||||
console.log(error.body);
|
||||
this.status = 500;
|
||||
})
|
||||
.then(function (client) {
|
||||
this.cookie("instagram_code", code);
|
||||
this.redirect("/");
|
||||
});
|
||||
var client = yield instagram.authorize(code);
|
||||
|
||||
//.catch(function (error) {
|
||||
// console.log(error.body);
|
||||
// this.status = 500;
|
||||
//})
|
||||
//.then(function (client) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
function instagramSandbox(ig) {
|
||||
function instagramSandbox(gram, params) {
|
||||
var ig = gram.client;
|
||||
var current_user = gram.user;
|
||||
|
||||
function *user_search() {
|
||||
var user_search = thunkify(ig["user_search"]);
|
||||
var s = yield user_search("hudozka");
|
||||
this.body = s[0];
|
||||
}
|
||||
var methods = {
|
||||
user_search: function (user) {
|
||||
return function *() {
|
||||
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) {
|
||||
// // console.log(err, medias, pagination, remaining, limit);
|
||||
// // 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) {
|
||||
// // console.log(err, subscriptions, limit);
|
||||
// // 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 param = yield findByTag(ig, "shlb_hudozka", 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();
|
||||
}
|
||||
|
||||
return fbt;
|
||||
var method = params.method || "user";
|
||||
var value = params.value || current_user.id;
|
||||
return methods[method](value);
|
||||
}
|
||||
|
||||
function *findByTag(ig, tag, count) {
|
||||
|
||||
+25
-11
@@ -3,28 +3,42 @@
|
||||
*/
|
||||
|
||||
var config = require("../config");
|
||||
|
||||
var instagram = require("instagram-node");
|
||||
var auth_clients = {};
|
||||
|
||||
module.exports = {
|
||||
client: function(param){
|
||||
if(param in auth_clients) {
|
||||
return auth_clients[param];
|
||||
client: function(code){
|
||||
if(code && code in auth_clients) {
|
||||
return auth_clients[code].client;
|
||||
}else{
|
||||
return createClient();
|
||||
}
|
||||
},
|
||||
|
||||
user: function(code){
|
||||
if(code in auth_clients) {
|
||||
return auth_clients[code].user;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
authorize: function(code){
|
||||
return new Promise(function(resolve, reject){
|
||||
var api = createClient();
|
||||
api.authorize_user(code, config.instagram.redirect_uri, function (err, result) {
|
||||
if (err) return reject(err);
|
||||
else {
|
||||
var user = result.user;
|
||||
var token = result.access_token;
|
||||
var c = createClient(token);
|
||||
auth_clients[code] = c;
|
||||
resolve(c);
|
||||
var client = createClient(token);
|
||||
|
||||
auth_clients[code] = {
|
||||
client: client,
|
||||
user: user
|
||||
};
|
||||
|
||||
resolve(client);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -32,14 +46,14 @@ module.exports = {
|
||||
};
|
||||
|
||||
function createClient(token) {
|
||||
var api = require('instagram-node').instagram();
|
||||
var client = instagram.instagram({}, {});
|
||||
if (token) {
|
||||
api.use({access_token: token});
|
||||
client.use({access_token: token});
|
||||
} else {
|
||||
api.use({
|
||||
client.use({
|
||||
client_id: config.instagram.client_id,
|
||||
client_secret: config.instagram.client_secret
|
||||
});
|
||||
}
|
||||
return api;
|
||||
return client;
|
||||
}
|
||||
Reference in new issue
Block a user