Instagram Service update
This commit is contained in:
1 parent
30c8c4f259
commit
ec52ee2c2a
14 files changed
+120
-75
No files matched your search
@@ -1,16 +1,26 @@
|
||||
import path from 'path';
|
||||
import {readFileSync} from 'fs';
|
||||
|
||||
const env = process.env['NODE_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(
|
||||
readFileSync(privatePath, 'utf-8')
|
||||
);
|
||||
|
||||
export const port = isProduction ? 18000 : 3000;
|
||||
export const pathPrefix = isProduction ? '/service/instagram' : '';
|
||||
export const indexPath = '../build/main.html';
|
||||
// export const pathPrefix = isProduction ? '/service/instagram' : '';
|
||||
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 clientSecret = privateData['clientSecret'];
|
||||
@@ -43,8 +53,9 @@ export const database = {
|
||||
host: 'localhost',
|
||||
port: 27017,
|
||||
db: 'hudozka',
|
||||
username: '',
|
||||
password: ''
|
||||
username: privateData['databaseUser'],
|
||||
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}`;
|
||||
@@ -34,8 +34,6 @@ export async function updateSubscription(callbackUrl) {
|
||||
let data = await instagram.subscriptions.list();
|
||||
let activeSubscriptions = data.result;
|
||||
|
||||
console.log(data);
|
||||
|
||||
let objectSubscriptions = activeSubscriptions.filter(i => i.object === 'user' && !i.object_id && i.callback_url === callbackUrl)
|
||||
|
||||
if (!objectSubscriptions.length) {
|
||||
@@ -65,10 +63,6 @@ export async function updateSubscription(callbackUrl) {
|
||||
// limit: 5000
|
||||
// }
|
||||
await instagram.subscriptions.subscribeUser(callbackUrl);
|
||||
// let data = await instagram.subscriptions.subscribeTag('love', callbackUrl);
|
||||
console.log(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,57 @@
|
||||
require('babel-core/register');
|
||||
require('babel-polyfill');
|
||||
require('./main');
|
||||
import 'babel-polyfill';
|
||||
|
||||
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);
|
||||
// });
|
||||
// })
|
||||
@@ -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);
|
||||
// });
|
||||
// })
|
||||
@@ -3,7 +3,7 @@ import update from '../core/update';
|
||||
|
||||
export function callbackGet() {
|
||||
return get('/callback/:id', async (ctx, id) => {
|
||||
console.log('challenge', id);
|
||||
// console.log('challenge', id);
|
||||
|
||||
if(ctx.query['hub.mode'] == 'subscribe'){
|
||||
ctx.body = ctx.query['hub.challenge'];
|
||||
@@ -16,7 +16,7 @@ export function callbackGet() {
|
||||
export function callbackPost() {
|
||||
return post('/callback/:id', async (ctx, id) => {
|
||||
let body = ctx.request.body;
|
||||
console.log(id, body);
|
||||
// console.log(id, body);
|
||||
update(id, body);
|
||||
|
||||
ctx.status = 200;
|
||||
|
||||
@@ -30,13 +30,18 @@ export function routers() {
|
||||
}
|
||||
|
||||
export const url = path => `${root}${path}`;
|
||||
// export const url = path => path;
|
||||
export const get = (path, fn) => route['get'](url(path), fn);
|
||||
export const post = (path, fn) => route['post'](url(path), fn);
|
||||
|
||||
export function index(fn){
|
||||
return async function (ctx){
|
||||
ctx.type = 'text/html';
|
||||
ctx.body = await fs.createReadStream(indexPath);
|
||||
return async (ctx) => {
|
||||
try{
|
||||
ctx.type = 'text/html';
|
||||
ctx.body = await fs.createReadStream(indexPath);
|
||||
}catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import Koa from 'koa';
|
||||
import mount from 'koa-mount';
|
||||
import serve from 'koa-static';
|
||||
import convert from 'koa-convert';
|
||||
import sessionStore from 'koa-session-store';
|
||||
@@ -8,7 +9,7 @@ import bodyParser from 'koa-bodyparser';
|
||||
import {db} from './core/db';
|
||||
import instagram from './middlewares/instagram';
|
||||
import {routers, statelessRouters} from './routers';
|
||||
import {sessionSecret, sessionExpirationTime} from './config';
|
||||
import {pathPrefix, sessionSecret, sessionExpirationTime} from './config';
|
||||
|
||||
export default function(){
|
||||
const app = new Koa();
|
||||
@@ -24,6 +25,11 @@ export default function(){
|
||||
app.use(error());
|
||||
|
||||
return app;
|
||||
|
||||
// const root = new Koa();
|
||||
// root.use(mount(pathPrefix, app));
|
||||
|
||||
// return root;
|
||||
}
|
||||
|
||||
function session(name, exp){
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<title>Instagram Control</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="app-container"></div>
|
||||
|
||||
Reference in new issue
Block a user