instagram update;
This commit is contained in:
1 parent
0372a81304
commit
38e0a4dadd
3 files changed
+130
-98
No files matched your search
+128
-96
@@ -2,57 +2,106 @@
|
|||||||
* Created by tmshv on 22/11/14.
|
* 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 config = require("../config");
|
||||||
var db = require("../core/db");
|
var db = require("../core/db");
|
||||||
var instagram = require("../core/instagram");
|
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.uri)
|
||||||
|
|
||||||
db.init(config.db)
|
|
||||||
.then(function () {
|
.then(function () {
|
||||||
server(config.port);
|
server(config.port);
|
||||||
});
|
});
|
||||||
|
|
||||||
function server(port) {
|
function server(port) {
|
||||||
var app = express();
|
var app = koa();
|
||||||
app.use(logger("dev"));
|
app.proxy = true;
|
||||||
app.use(cookieParser());
|
app.use(logger());
|
||||||
app.use(bodyParser.json());
|
|
||||||
app.use(bodyParser.urlencoded({extended: true}));
|
|
||||||
app.use(methodOverride());
|
|
||||||
|
|
||||||
require("../routers/instagram")(app);
|
|
||||||
|
|
||||||
var Router = express.Router;
|
|
||||||
var instagram = require("../core/instagram");
|
var instagram = require("../core/instagram");
|
||||||
var router = new Router();
|
|
||||||
router.route("/").get(function (req, res) {
|
app.use(
|
||||||
if ("instagram_code" in req.cookies) {
|
route.get("/", function *() {
|
||||||
var client = instagram.client(req.cookies["instagram_code"]);
|
if ("instagram_code" in this.cookies) {
|
||||||
instagramSandbox(client, res);
|
var client = instagram.client(this.cookies["instagram_code"]);
|
||||||
} else {
|
|
||||||
var ig = instagram.client();
|
yield instagramSandbox(client);
|
||||||
var url = ig.get_authorization_url(config.instagram.redirect_uri, {scope: ["basic"], state: "hello"});
|
} else {
|
||||||
res.redirect(url);
|
var ig = instagram.client();
|
||||||
}
|
var url = ig["get_authorization_url"](config.instagram.redirect_uri, {
|
||||||
});
|
scope: ["basic"],
|
||||||
app.use(router);
|
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);
|
app.listen(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
function instagramSandbox(ig, res) {
|
function instagramSandbox(ig) {
|
||||||
// //{"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) {
|
function *user_search() {
|
||||||
// // console.log(err, users, remaining, limit);
|
var user_search = thunkify(ig["user_search"]);
|
||||||
// // res.json(users);
|
var s = yield user_search("hudozka");
|
||||||
// //});
|
this.body = s[0];
|
||||||
//
|
}
|
||||||
|
|
||||||
// // 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);
|
||||||
@@ -74,67 +123,50 @@ function instagramSandbox(ig, res) {
|
|||||||
// // res.json(result);
|
// // res.json(result);
|
||||||
// //});
|
// //});
|
||||||
|
|
||||||
findByTag(ig, "shlb_hudozka", 50)
|
function *fbt() {
|
||||||
.catch(function (error) {
|
var param = yield findByTag(ig, "shlb_hudozka", 50);
|
||||||
console.log(error);
|
|
||||||
})
|
|
||||||
.then(function (params) {
|
|
||||||
var list = params.medias;
|
|
||||||
var pagination = params.pagination;
|
|
||||||
var remaining = params.remaining;
|
|
||||||
var limit = params.limit;
|
|
||||||
|
|
||||||
var posts = list.map(function (gram) {
|
var grams = param.medias.map(toInstagram);
|
||||||
//var post = new Post();
|
var ids = grams.map(function (g) {
|
||||||
var post = {};
|
return g.data.id;
|
||||||
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
|
|
||||||
//}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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) {
|
||||||
return new Promise(function (resolve, reject) {
|
var tag_media_recent = thunkify(ig["tag_media_recent"]);
|
||||||
ig.tag_media_recent(tag, {count: count}, function (err, medias, pagination, remaining, limit) {
|
|
||||||
if (err) {
|
var result = yield tag_media_recent(tag, {count: count});
|
||||||
reject(err);
|
return {
|
||||||
} else {
|
medias: result[0],
|
||||||
resolve({
|
pagination: result[1],
|
||||||
medias: medias,
|
remaining: result[2],
|
||||||
pagination: pagination,
|
limit: result[3]
|
||||||
remaining: remaining,
|
};
|
||||||
limit: limit
|
}
|
||||||
});
|
|
||||||
}
|
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 config = require("../config");
|
||||||
var Promise = require("promise");
|
|
||||||
|
|
||||||
var auth_clients = {};
|
var auth_clients = {};
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -25,7 +25,8 @@
|
|||||||
"koa-static": "^1.4.9",
|
"koa-static": "^1.4.9",
|
||||||
"koa-useragent": "0.0.5",
|
"koa-useragent": "0.0.5",
|
||||||
"mz": "^1.3.0",
|
"mz": "^1.3.0",
|
||||||
"promised-mongo": "^0.11.1"
|
"promised-mongo": "^0.11.1",
|
||||||
|
"thunkify": "^2.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bower": "^1.3.12",
|
"bower": "^1.3.12",
|
||||||
|
|||||||
Reference in new issue
Block a user