diff --git a/bin/www.js b/bin/www.js
index ee6c4c43..2b23ddd4 100644
--- a/bin/www.js
+++ b/bin/www.js
@@ -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);
});
\ No newline at end of file
diff --git a/bower.json b/bower.json
index 3c92adb7..49c7dca8 100644
--- a/bower.json
+++ b/bower.json
@@ -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"
}
}
diff --git a/client/app.js b/client/app.js
index 1e2d3a50..e76ad482 100644
--- a/client/app.js
+++ b/client/app.js
@@ -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);
diff --git a/client/services/io.js b/client/services/io.js
new file mode 100644
index 00000000..f8efa884
--- /dev/null
+++ b/client/services/io.js
@@ -0,0 +1,11 @@
+/**
+ * Created by tmshv on 15/03/15.
+ */
+
+module.exports = function (app) {
+ var socket = io();
+
+ app.service("io", function(){
+ return socket;
+ })
+};
\ No newline at end of file
diff --git a/client/ui/timeline.js b/client/ui/timeline.js
index 87b8baf9..cc8be8d5 100644
--- a/client/ui/timeline.js
+++ b/client/ui/timeline.js
@@ -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);
+ });
+ });
}
};
});
diff --git a/config.js b/config.js
index 23102de5..d69a1d7f 100644
--- a/config.js
+++ b/config.js
@@ -21,9 +21,7 @@ var config = {
db: {
uri: "mongodb://localhost:27017/hudozka"
- },
-
- tag: "shlb_hudozka"
+ }
};
module.exports = Object.keys(privateData)
diff --git a/instagram/index.js b/instagram/index.js
index 9f7a28e2..dad6739f 100644
--- a/instagram/index.js
+++ b/instagram/index.js
@@ -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 *() {
diff --git a/instagram/io.js b/instagram/io.js
new file mode 100644
index 00000000..668b6f35
--- /dev/null
+++ b/instagram/io.js
@@ -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;
+ });
+};
\ No newline at end of file
diff --git a/instagram/publish.js b/instagram/publish.js
index f51faed5..7545e1e0 100644
--- a/instagram/publish.js
+++ b/instagram/publish.js
@@ -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});
+ })
+ );
+}
\ No newline at end of file
diff --git a/nginx.conf b/nginx.conf
index dab9c71b..509b098e 100644
--- a/nginx.conf
+++ b/nginx.conf
@@ -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;
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 395924c6..ddb148dd 100644
--- a/package.json
+++ b/package.json
@@ -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"
diff --git a/server.js b/server.js
index 09b4ece9..0402d8ee 100644
--- a/server.js
+++ b/server.js
@@ -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 *() {
diff --git a/templates/index.html b/templates/index.html
index d2d80187..14e0c154 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -20,6 +20,7 @@
+