Instagram Service update

This commit is contained in:
tmshv committed 2016-03-25 13:36:00 +03:00
1 parent 30c8c4f259
commit ec52ee2c2a
14 files changed
+120 -75

No files matched your search

+1
View File
@@ -7,4 +7,5 @@ npm-debug.log
/node_modules /node_modules
/build /build
/out
/coverage /coverage
+9
View File
@@ -8,6 +8,11 @@ server="art.shburg.org"
tarfile="build.tar.gz" tarfile="build.tar.gz"
tarpath=$TMPDIR${tarfile} tarpath=$TMPDIR${tarfile}
destpath="~/www/art.shburg.org/services/instagram" destpath="~/www/art.shburg.org/services/instagram"
private=".private"
echo "Building..."
NODE_ENV=production npm run webpack
npm run build
echo "Packing..." echo "Packing..."
env GZIP=-9 tar -czf ${tarpath} \ env GZIP=-9 tar -czf ${tarpath} \
@@ -16,6 +21,8 @@ env GZIP=-9 tar -czf ${tarpath} \
--exclude=.git \ --exclude=.git \
--exclude=.idea \ --exclude=.idea \
--exclude=.DS_Store \ --exclude=.DS_Store \
--exclude=.private* \
--exclude=src \
--exclude=deploy.sh \ --exclude=deploy.sh \
--exclude=install.sh \ --exclude=install.sh \
. .
@@ -26,6 +33,8 @@ echo "Build file: $tarpath"
scp ${tarpath} hoster@${server}:${destpath} scp ${tarpath} hoster@${server}:${destpath}
echo "Build file successfully uploaded on ${server}" echo "Build file successfully uploaded on ${server}"
scp ${private} hoster@${server}:${destpath}
echo "Installing..." echo "Installing..."
ssh hoster@${server} "bash -s" -- < ./install.sh ${destpath} ${tarfile} ssh hoster@${server} "bash -s" -- < ./install.sh ${destpath} ${tarfile}
+1
View File
@@ -2,6 +2,7 @@
dir_path="$1" dir_path="$1"
build_file="$2" build_file="$2"
script="out/index.js"
cd ${dir_path} cd ${dir_path}
gunzip -c ${build_file} | tar xopf - gunzip -c ${build_file} | tar xopf -
+2 -2
View File
@@ -9,8 +9,8 @@ server {
ssl_prefer_server_ciphers on; ssl_prefer_server_ciphers on;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL; ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
location /api/instagram { location /service/instagram {
proxy_pass http://hudozka_instagram; proxy_pass http://hudozka_instagram/;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection $connection_upgrade;
+9 -4
View File
@@ -3,13 +3,12 @@
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"webpack": "webpack" "webpack": "webpack",
"start": "babel-node src/index.js",
"build": "babel src -d out"
}, },
"dependencies": { "dependencies": {
"babel-core": "^6.7.2",
"babel-polyfill": "^6.7.2", "babel-polyfill": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0",
"co": "^4.6.0", "co": "^4.6.0",
"ejson": "^2.1.2", "ejson": "^2.1.2",
"instagram-node": "^0.5.8", "instagram-node": "^0.5.8",
@@ -19,6 +18,7 @@
"koa-bodyparser": "^3.0.0", "koa-bodyparser": "^3.0.0",
"koa-compose": "^3.1.0", "koa-compose": "^3.1.0",
"koa-convert": "^1.2.0", "koa-convert": "^1.2.0",
"koa-mount": "^2.0.0",
"koa-route": "^3.0.0", "koa-route": "^3.0.0",
"koa-session-mongo": "^1.1.0", "koa-session-mongo": "^1.1.0",
"koa-session-store": "^1.1.1", "koa-session-store": "^1.1.1",
@@ -28,9 +28,14 @@
"react": "^0.14.7", "react": "^0.14.7",
"react-dom": "^0.14.7", "react-dom": "^0.14.7",
"react-router": "^2.0.1", "react-router": "^2.0.1",
"request-promise": "^2.0.1",
"thunkify": "^2.1.2" "thunkify": "^2.1.2"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0",
"babel-cli": "^6.6.5",
"babel-loader": "^6.2.4", "babel-loader": "^6.2.4",
"babel-preset-react": "^6.5.0", "babel-preset-react": "^6.5.0",
"html-webpack-plugin": "^2.10.0", "html-webpack-plugin": "^2.10.0",
+17 -6
View File
@@ -1,16 +1,26 @@
import path from 'path';
import {readFileSync} from 'fs'; import {readFileSync} from 'fs';
const env = process.env['NODE_ENV'] || 'production'; const env = process.env['NODE_ENV'] || 'production';
const isProduction = env === 'production'; const isProduction = env === 'production';
let privatePath = process.env['PRIVATE'] || '../.private'; let privatePath = path.join(
__dirname,
process.env['PRIVATE'] || '../.private'
);
let privateData = JSON.parse( let privateData = JSON.parse(
readFileSync(privatePath, 'utf-8') readFileSync(privatePath, 'utf-8')
); );
export const port = isProduction ? 18000 : 3000; export const port = isProduction ? 18000 : 3000;
export const pathPrefix = isProduction ? '/service/instagram' : ''; // export const pathPrefix = isProduction ? '/service/instagram' : '';
export const indexPath = '../build/main.html'; export const pathPrefix = '';
export const indexPath = path.join(
__dirname,
'./templates/index.html'
// '../build/main.html'
);
// export const indexPath = '../build/main.html';
export const clientId = privateData['clientId']; export const clientId = privateData['clientId'];
export const clientSecret = privateData['clientSecret']; export const clientSecret = privateData['clientSecret'];
@@ -43,8 +53,9 @@ export const database = {
host: 'localhost', host: 'localhost',
port: 27017, port: 27017,
db: 'hudozka', db: 'hudozka',
username: '', username: privateData['databaseUser'],
password: '' password: privateData['databasePassword']
}; };
export const databaseUri = `mongodb://${database.host}:${database.port}/${database.db}`; const databaseAuth = database.username && database.password ? `${database.username}:${database.password}@` : '';
export const databaseUri = `mongodb://${databaseAuth}${database.host}:${database.port}/${database.db}`;
-6
View File
@@ -34,8 +34,6 @@ export async function updateSubscription(callbackUrl) {
let data = await instagram.subscriptions.list(); let data = await instagram.subscriptions.list();
let activeSubscriptions = data.result; let activeSubscriptions = data.result;
console.log(data);
let objectSubscriptions = activeSubscriptions.filter(i => i.object === 'user' && !i.object_id && i.callback_url === callbackUrl) let objectSubscriptions = activeSubscriptions.filter(i => i.object === 'user' && !i.object_id && i.callback_url === callbackUrl)
if (!objectSubscriptions.length) { if (!objectSubscriptions.length) {
@@ -65,10 +63,6 @@ export async function updateSubscription(callbackUrl) {
// limit: 5000 // limit: 5000
// } // }
await instagram.subscriptions.subscribeUser(callbackUrl); await instagram.subscriptions.subscribeUser(callbackUrl);
// let data = await instagram.subscriptions.subscribeTag('love', callbackUrl);
console.log(data);
return data;
} }
} }
+57 -3
View File
@@ -1,3 +1,57 @@
require('babel-core/register'); import 'babel-polyfill';
require('babel-polyfill');
require('./main'); import {databaseUri, port, updateSubscriptionDelay, updateSubscriptionCallbackUrl} from './config'
import {connect} from './core/db';
import server from './server';
// import update from './core/update';
import {updateSubscription, clearSubscriptions} from './core/update';
let repeat = time => fn => setInterval(fn, time);
function main(){
async function loop(){
try{
await connect(databaseUri);
}catch(error){
console.log(error);
}
try{
let app = server();
app.listen(port);
console.log(`Start listening ${port}`);
}catch(error){
console.log(error);
}
await clearSubscriptions();
await updateSubscription(updateSubscriptionCallbackUrl);
repeat(updateSubscriptionDelay)(() => {
updateSubscription(updateSubscriptionCallbackUrl);
});
}
loop();
}
main();
// .then(() => {
// return update('hudozka', {
// "changed_aspect": "media",
// "object": "tag",
// "object_id": "love",
// // "time": 1458675620,
// "time": 1458594000,
// "subscription_id": 22487204,
// "data": {}
// })
// .catch(e => {
// console.log(e);
// })
// .then(result => {
// console.log('update done');
// console.log(result);
// });
// })
-44
View File
@@ -1,44 +0,0 @@
import {databaseUri, port, updateSubscriptionDelay, updateSubscriptionCallbackUrl} from './config'
import {connect} from './core/db';
import server from './server';
import update from './core/update';
import {updateSubscription, clearSubscriptions} from './core/update';
connect(databaseUri)
.catch(error => {
console.log(error);
})
.then(() => {
let app = server();
app.listen(port);
})
.then(() => {
console.log(`Start listening ${port}`);
})
.then(clearSubscriptions)
.then(() => {
let repeat = time => fn => setInterval(fn, time);
updateSubscription(updateSubscriptionCallbackUrl);
repeat(updateSubscriptionDelay)(() => {
updateSubscription(updateSubscriptionCallbackUrl);
});
});
// .then(() => {
// return update('hudozka', {
// "changed_aspect": "media",
// "object": "tag",
// "object_id": "love",
// // "time": 1458675620,
// "time": 1458594000,
// "subscription_id": 22487204,
// "data": {}
// })
// .catch(e => {
// console.log(e);
// })
// .then(result => {
// console.log('update done');
// console.log(result);
// });
// })
+2 -2
View File
@@ -3,7 +3,7 @@ import update from '../core/update';
export function callbackGet() { export function callbackGet() {
return get('/callback/:id', async (ctx, id) => { return get('/callback/:id', async (ctx, id) => {
console.log('challenge', id); // console.log('challenge', id);
if(ctx.query['hub.mode'] == 'subscribe'){ if(ctx.query['hub.mode'] == 'subscribe'){
ctx.body = ctx.query['hub.challenge']; ctx.body = ctx.query['hub.challenge'];
@@ -16,7 +16,7 @@ export function callbackGet() {
export function callbackPost() { export function callbackPost() {
return post('/callback/:id', async (ctx, id) => { return post('/callback/:id', async (ctx, id) => {
let body = ctx.request.body; let body = ctx.request.body;
console.log(id, body); // console.log(id, body);
update(id, body); update(id, body);
ctx.status = 200; ctx.status = 200;
+8 -3
View File
@@ -30,13 +30,18 @@ export function routers() {
} }
export const url = path => `${root}${path}`; export const url = path => `${root}${path}`;
// export const url = path => path;
export const get = (path, fn) => route['get'](url(path), fn); export const get = (path, fn) => route['get'](url(path), fn);
export const post = (path, fn) => route['post'](url(path), fn); export const post = (path, fn) => route['post'](url(path), fn);
export function index(fn){ export function index(fn){
return async function (ctx){ return async (ctx) => {
ctx.type = 'text/html'; try{
ctx.body = await fs.createReadStream(indexPath); ctx.type = 'text/html';
ctx.body = await fs.createReadStream(indexPath);
}catch(e){
console.log(e);
}
} }
} }
+7 -1
View File
@@ -1,4 +1,5 @@
import Koa from 'koa'; import Koa from 'koa';
import mount from 'koa-mount';
import serve from 'koa-static'; import serve from 'koa-static';
import convert from 'koa-convert'; import convert from 'koa-convert';
import sessionStore from 'koa-session-store'; import sessionStore from 'koa-session-store';
@@ -8,7 +9,7 @@ import bodyParser from 'koa-bodyparser';
import {db} from './core/db'; import {db} from './core/db';
import instagram from './middlewares/instagram'; import instagram from './middlewares/instagram';
import {routers, statelessRouters} from './routers'; import {routers, statelessRouters} from './routers';
import {sessionSecret, sessionExpirationTime} from './config'; import {pathPrefix, sessionSecret, sessionExpirationTime} from './config';
export default function(){ export default function(){
const app = new Koa(); const app = new Koa();
@@ -24,6 +25,11 @@ export default function(){
app.use(error()); app.use(error());
return app; return app;
// const root = new Koa();
// root.use(mount(pathPrefix, app));
// return root;
} }
function session(name, exp){ function session(name, exp){
+1 -1
View File
@@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>Instagram Control</title>
</head> </head>
<body> <body>
<div class="app-container"></div> <div class="app-container"></div>
+6 -3
View File
@@ -1,11 +1,14 @@
import webpack from'webpack'; import webpack from'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin'; import HTML from 'html-webpack-plugin';
const env = process.env['NODE_ENV'] || 'production';
const isProduction = env === 'production';
// import autoprefixer from 'autoprefixer'; // import autoprefixer from 'autoprefixer';
module.exports = { module.exports = {
entry: './src/app.js', entry: './src/app.js',
watch: true, watch: !isProduction,
//resolve: { //resolve: {
// modulesDirectories: [ // modulesDirectories: [
// './node_modules' // './node_modules'
@@ -25,7 +28,7 @@ module.exports = {
// }, // },
plugins: [ plugins: [
// new webpack.HotModuleReplacementPlugin(), // new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({ new HTML({
filename: 'main.html', filename: 'main.html',
title: 'Instagram', title: 'Instagram',
template: 'src/templates/index.html', template: 'src/templates/index.html',