+ io; other changes;

This commit is contained in:
tmshv committed 2015-03-16 17:21:01 +03:00
1 parent 754c8bf9a4
commit dc130ad394
13 files changed
+145 -63

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ var server = require("../server");
var instagram = require("../instagram");
db.init(config.db.uri)
.then(instagram)
//.then(instagram)
.then(function () {
server(config.port);
});
+2 -1
View File
@@ -26,6 +26,7 @@
"angular-animate": "~1.3.3",
"angular": "~1.3.3",
"angulartics": "~0.17.2",
"angular-spinner": "~0.6.1"
"angular-spinner": "~0.6.1",
"socket.io-client": "~1.3.5"
}
}
+1
View File
@@ -69,6 +69,7 @@ app.run(function($location, $rootScope, $http, config) {
});
require("./api/api")(app);
require("./services/io")(app);
require("./ui/menu")(app);
require("./ui/timeline")(app);
require("./filters/uppercase-first")(app);
+11
View File
@@ -0,0 +1,11 @@
/**
* Created by tmshv on 15/03/15.
*/
module.exports = function (app) {
var socket = io();
app.service("io", function(){
return socket;
})
};
+33 -18
View File
@@ -28,6 +28,22 @@ function postText(post){
module.exports = function(app) {
app.directive("timeline", function (team) {
function toInstagram(post){
var author_id = personByInstagram(team.team, post.data.author);
var name = author_id ? team.short(author_id) : post.data.author;
return {
date: post.date,
image: post.data.image.standard_resolution.url,
url: post.data.url,
username: name,
//userpic: post.data.user.profile_picture,
text: post.body,
//text: postText(post),
type: post.type
};
}
return {
templateUrl: "/views/timeline.html",
scope: {
@@ -38,7 +54,7 @@ module.exports = function(app) {
return index % 2 === 1;
}
},
controller: function($scope, $timeout, usSpinnerService, api){
controller: function($scope, $timeout, usSpinnerService, io, api){
var portion = 0;
var count = 10;
$scope.feed = [];
@@ -67,23 +83,8 @@ module.exports = function(app) {
//}, 5000);
var feed = list.map(function (post) {
if(post.type == "instagram"){
var author_id = personByInstagram(team.team, post.data.author);
var name = author_id ? team.short(author_id) : post.data.author;
return {
date: post.date,
image: post.data.image.standard_resolution.url,
url: post.data.url,
username: name,
//userpic: post.data.user.profile_picture,
text: post.body,
//text: postText(post),
type: post.type
};
}else{
return post;
}
if(post.type == "instagram") return toInstagram(post);
else return post;
});
portion++;
@@ -92,6 +93,20 @@ module.exports = function(app) {
};
$scope.loadNext();
io.on("post", function (params) {
var posts = params instanceof Array ? params : [params];
console.log("post", posts);
var feed = posts.map(function (post) {
if(post.type == "instagram") return toInstagram(post);
else return post;
});
$scope.$apply(function () {
$scope.feed = feed.concat($scope.feed);
});
});
}
};
});
+1 -3
View File
@@ -21,9 +21,7 @@ var config = {
db: {
uri: "mongodb://localhost:27017/hudozka"
},
tag: "shlb_hudozka"
}
};
module.exports = Object.keys(privateData)
+1 -3
View File
@@ -5,12 +5,10 @@
var db = require("../core/db");
var co = require("co");
var instagram = require("./instagram");
var publish = require("./publish");
var config = require("../config");
module.exports = function () {
return loadUsers()
.then(publish)
.then(updateSubscription)
};
@@ -29,7 +27,7 @@ function loadUsers() {
}
function updateSubscription() {
var tags = [config["tag"]];
var tags = config.instagram["tags"];
return tags.map(function (tag) {
var cb = config.instagram["tag_callback"];
return co(function *() {
+16
View File
@@ -0,0 +1,16 @@
/**
* 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;
});
};
+61 -34
View File
@@ -3,48 +3,42 @@
*/
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);
module.exports = function () {
return co(function *() {
instagram.on("update", function (data) {
var tag = config["tag"];
var self = this;
instagram.on("update", function (data) {
var tag = config.instagram["tags"][0];
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;
}
findInstagrams(tag)
.catch(function (error) {
console.log(error);
})
.then(function (posts) {
return posts.map(function (g) {
console.log(g);
return db.c("posts").update({"data.id": g.data.id}, g, {upsert: true})
.catch(function (error) {
console.log(error);
});
});
})
.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("posts").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);
@@ -54,7 +48,12 @@ function *fbt(client, tag) {
function toInstagram(gram) {
var post = {};
post.body = gram.caption.text;
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);
@@ -68,3 +67,31 @@ function toInstagram(gram) {
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("posts").update({"data.id": g.data.id}, g, {upsert: true});
})
);
}
+14 -1
View File
@@ -5,7 +5,11 @@ server {
server_name art.shlisselburg.org;
location / {
proxy_pass http://localhost:18010;
proxy_pass http://hudozka;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
@@ -19,3 +23,12 @@ server {
server_name art.shburg.org;
return 301 $scheme://art.shlisselburg.org$request_uri;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream hudozka {
server localhost:18010;
}
+1 -1
View File
@@ -16,7 +16,6 @@
"dependencies": {
"co": "^4.4.0",
"instagram-node": "^0.5.1",
"koa": "^0.18.0",
"koa-bodyparser": "^1.4.1",
"koa-conditional-get": "^1.0.2",
"koa-etag": "^2.0.0",
@@ -26,6 +25,7 @@
"koa-route": "^2.4.0",
"koa-static": "^1.4.9",
"koa-useragent": "0.0.5",
"koa.io": "0.0.3",
"mz": "^1.3.0",
"promised-mongo": "^0.11.1",
"thunkify": "^2.1.2"
+2 -1
View File
@@ -1,6 +1,6 @@
var path = require("path");
var koa = require("koa");
var koa = require("koa.io");
var route = require("koa-route");
var serve = require("koa-static");
var logger = require("koa-logger");
@@ -58,6 +58,7 @@ require("./routes/news")(app);
require("./routes/404")(app);
require("./instagram/router")(app);
require("./instagram/io")(app);
if (process.env["NODE_ENV"] === "development") {
app.use(route.get("/debug", function *() {
+1
View File
@@ -20,6 +20,7 @@
<script src="/bower_components/angulartics/dist/angulartics-ga.min.js"></script>
<script src="/bower_components/spin.js/spin.js"></script>
<script src="/bower_components/angular-spinner/angular-spinner.min.js"></script>
<script src="/bower_components/socket.io-client/socket.io.js"></script>
<link rel="stylesheet" href="/bower_components/fotorama/fotorama.css">
<script src="/bower_components/fotorama/fotorama.js"></script>