instagram update;
This commit is contained in:
1 parent
0372a81304
commit
38e0a4dadd
3 files changed
+131
-99
No files matched your search
+129
-97
@@ -2,57 +2,106 @@
|
||||
* Created by tmshv on 22/11/14.
|
||||
*/
|
||||
|
||||
var thunkify = require("thunkify");
|
||||
|
||||
var koa = require("koa");
|
||||
var route = require("koa-route");
|
||||
var logger = require("koa-logger");
|
||||
|
||||
var config = require("../config");
|
||||
var db = require("../core/db");
|
||||
var instagram = require("../core/instagram");
|
||||
var express = require("express");
|
||||
var Promise = require("promise");
|
||||
var methodOverride = require("method-override");
|
||||
var logger = require("morgan");
|
||||
var cookieParser = require("cookie-parser");
|
||||
var bodyParser = require("body-parser");
|
||||
|
||||
var Post = require("../schema/post");
|
||||
|
||||
db.init(config.db)
|
||||
db.init(config.db.uri)
|
||||
.then(function () {
|
||||
server(config.port);
|
||||
});
|
||||
|
||||
function server(port) {
|
||||
var app = express();
|
||||
app.use(logger("dev"));
|
||||
app.use(cookieParser());
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({extended: true}));
|
||||
app.use(methodOverride());
|
||||
var app = koa();
|
||||
app.proxy = true;
|
||||
app.use(logger());
|
||||
|
||||
require("../routers/instagram")(app);
|
||||
|
||||
var Router = express.Router;
|
||||
var instagram = require("../core/instagram");
|
||||
var router = new Router();
|
||||
router.route("/").get(function (req, res) {
|
||||
if ("instagram_code" in req.cookies) {
|
||||
var client = instagram.client(req.cookies["instagram_code"]);
|
||||
instagramSandbox(client, res);
|
||||
} else {
|
||||
var ig = instagram.client();
|
||||
var url = ig.get_authorization_url(config.instagram.redirect_uri, {scope: ["basic"], state: "hello"});
|
||||
res.redirect(url);
|
||||
}
|
||||
});
|
||||
app.use(router);
|
||||
|
||||
app.use(
|
||||
route.get("/", function *() {
|
||||
if ("instagram_code" in this.cookies) {
|
||||
var client = instagram.client(this.cookies["instagram_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);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
app.use(
|
||||
route.get("/i", function *() {
|
||||
var code = "6b95d605c82444c98f77de1f304b2a8e";
|
||||
var client = instagram.client(code);
|
||||
|
||||
yield instagramSandbox(client);
|
||||
})
|
||||
);
|
||||
|
||||
app.use(
|
||||
route.get("/old-to-new", function *() {
|
||||
var grams = yield db.c("posts").find({
|
||||
"author": {$exists: true},
|
||||
"id": {$exists: true},
|
||||
"type": "instagram"
|
||||
}).toArray();
|
||||
|
||||
var ids = grams.map(function (g) {
|
||||
return g.id;
|
||||
});
|
||||
|
||||
grams = grams.map(function (g) {
|
||||
return g.data;
|
||||
});
|
||||
grams = grams.map(toInstagram);
|
||||
|
||||
yield db.c("grams").remove({"id": {$in: ids}});
|
||||
yield grams.map(function (g) {
|
||||
return db.c("posts").update({"data.id": g.data.id}, g, {upsert: true});
|
||||
});
|
||||
|
||||
this.body = yield db.c("grams").find({"data.id": {$in: ids}}).toArray();
|
||||
})
|
||||
);
|
||||
|
||||
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("/");
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
app.listen(port);
|
||||
}
|
||||
|
||||
function instagramSandbox(ig, res) {
|
||||
// //{"username":"mlazareffa","bio":"","website":"","profile_picture":"http://images.ak.instagram.com/profiles/anonymousUser.jpg","full_name":"","id":"1549382451"}
|
||||
// //ig.user_search("mlazareffa", [], function(err, users, remaining, limit) {
|
||||
// // console.log(err, users, remaining, limit);
|
||||
// // res.json(users);
|
||||
// //});
|
||||
//
|
||||
function instagramSandbox(ig) {
|
||||
|
||||
function *user_search() {
|
||||
var user_search = thunkify(ig["user_search"]);
|
||||
var s = yield user_search("hudozka");
|
||||
this.body = s[0];
|
||||
}
|
||||
|
||||
// // ig.user_media_recent("1549382451", [], function (err, medias, pagination, remaining, limit) {
|
||||
// // console.log(err, medias, pagination, remaining, limit);
|
||||
// // res.json(users);
|
||||
@@ -74,67 +123,50 @@ function instagramSandbox(ig, res) {
|
||||
// // res.json(result);
|
||||
// //});
|
||||
|
||||
findByTag(ig, "shlb_hudozka", 50)
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
})
|
||||
.then(function (params) {
|
||||
var list = params.medias;
|
||||
var pagination = params.pagination;
|
||||
var remaining = params.remaining;
|
||||
var limit = params.limit;
|
||||
function *fbt() {
|
||||
var param = yield findByTag(ig, "shlb_hudozka", 50);
|
||||
|
||||
var posts = list.map(function (gram) {
|
||||
//var post = new Post();
|
||||
var post = {};
|
||||
post.id = gram.id;
|
||||
post.author = gram.user["username"];
|
||||
post.publishDate = new Date(parseInt(gram["created_time"]) * 1000);
|
||||
post.data = gram;
|
||||
post.body = "";
|
||||
post.type = "instagram";
|
||||
|
||||
return post;
|
||||
});
|
||||
|
||||
Promise.all(posts.map(function (post) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
console.log('updating', post.id);
|
||||
Post.update({id: post.id}, post, {upsert: true}, function (error, num) {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}))
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
})
|
||||
.then(function () {
|
||||
res.send("LOL");
|
||||
});
|
||||
|
||||
//if (pagination.next) {
|
||||
// pagination.next(mtr); // Will get second page results
|
||||
//}
|
||||
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) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
ig.tag_media_recent(tag, {count: count}, function (err, medias, pagination, remaining, limit) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve({
|
||||
medias: medias,
|
||||
pagination: pagination,
|
||||
remaining: remaining,
|
||||
limit: limit
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
function *findByTag(ig, tag, count) {
|
||||
var tag_media_recent = thunkify(ig["tag_media_recent"]);
|
||||
|
||||
var result = yield tag_media_recent(tag, {count: count});
|
||||
return {
|
||||
medias: result[0],
|
||||
pagination: result[1],
|
||||
remaining: result[2],
|
||||
limit: result[3]
|
||||
};
|
||||
}
|
||||
|
||||
function toInstagram(gram) {
|
||||
var post = {};
|
||||
|
||||
post.body = gram.caption.text;
|
||||
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;
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
*/
|
||||
|
||||
var config = require("../config");
|
||||
var Promise = require("promise");
|
||||
|
||||
var auth_clients = {};
|
||||
|
||||
|
||||
+2
-1
@@ -25,7 +25,8 @@
|
||||
"koa-static": "^1.4.9",
|
||||
"koa-useragent": "0.0.5",
|
||||
"mz": "^1.3.0",
|
||||
"promised-mongo": "^0.11.1"
|
||||
"promised-mongo": "^0.11.1",
|
||||
"thunkify": "^2.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bower": "^1.3.12",
|
||||
|
||||
Reference in new issue
Block a user