From cdd0c8be1d281775c31303a9753d8710f1e9ee7c Mon Sep 17 00:00:00 2001 From: matteo Date: Sat, 1 Dec 2018 03:02:13 +0100 Subject: [PATCH 1/3] first step -- tolto palette --- node-entry.js | 61 +++++++++++++++++++++++++++++++++++------- package-lock.json | 68 ++++++++++++++++++++++++++--------------------- package.json | 2 ++ 3 files changed, 91 insertions(+), 40 deletions(-) diff --git a/node-entry.js b/node-entry.js index 5adfe7f..81fcbfd 100644 --- a/node-entry.js +++ b/node-entry.js @@ -1,14 +1,55 @@ const express = require('express'); -const app = express(); +const bodyParser = require('body-parser'); +const low = require('lowdb'); +const FileAsync = require('lowdb/adapters/FileAsync'); +const app = express(); // app is an instance of express -app.use(express.static(__dirname + '/build')); +app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded +app.use(bodyParser.json()) // parse application/json +app.use(express.static(__dirname + '/build')); // serve static build site +app.post('/test',(req,res)=>{ + res.send(req.body); +}) -app.get('/test/:id', (req, res) => { - // req.params.id - // new wiki().page(req.params.id) - // .then(page => page.fullInfo()) - // .then(parsed => res.send(parsed)); - res.send({ok:req.params.id}); -}); +// Create database instance and start server +// const adapter = new FileAsync(__dirname + '/db.json'); +low(new FileAsync(__dirname + '/db.json')) + .then(db => { + // DATABASE ROUTES -app.listen(8000, () => console.log('listen on 8000')); + // GET /videolog + app.get('/videolog', (req, res) => { + const post = db + .get('videos') + // .find({ id: req.params.id }) + .value(); + res.send(post); + }); + + // POST /videolog + app.post('/videolog', (req, res) => { + db.get('videos') + .push(req.body) + .last() + .update('timesWatched', n => n + 1 ) + .write() + .then(post => res.send(post)); // non funziona minimamente lol + }); + + // Set db default values + return db + .defaults({ + videos: [ + // { + // videoId: '', + // timesWatched: 0, + // prevalentReason: '', + // lastSelected: new Date() + // } + ] + }) + .write(); + }) + .then(() => { + app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs + }); diff --git a/package-lock.json b/package-lock.json index ee5ab49..30554be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5488,8 +5488,7 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true, - "optional": true + "bundled": true }, "aproba": { "version": "1.2.0", @@ -5507,13 +5506,11 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true, - "optional": true + "bundled": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5526,18 +5523,15 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "concat-map": { "version": "0.0.1", - "bundled": true, - "optional": true + "bundled": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "core-util-is": { "version": "1.0.2", @@ -5640,8 +5634,7 @@ }, "inherits": { "version": "2.0.3", - "bundled": true, - "optional": true + "bundled": true }, "ini": { "version": "1.3.5", @@ -5651,7 +5644,6 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5664,20 +5656,17 @@ "minimatch": { "version": "3.0.4", "bundled": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true, - "optional": true + "bundled": true }, "minipass": { "version": "2.2.4", "bundled": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -5694,7 +5683,6 @@ "mkdirp": { "version": "0.5.1", "bundled": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -5767,8 +5755,7 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true, - "optional": true + "bundled": true }, "object-assign": { "version": "4.1.1", @@ -5778,7 +5765,6 @@ "once": { "version": "1.4.0", "bundled": true, - "optional": true, "requires": { "wrappy": "1" } @@ -5854,8 +5840,7 @@ }, "safe-buffer": { "version": "5.1.1", - "bundled": true, - "optional": true + "bundled": true }, "safer-buffer": { "version": "2.1.2", @@ -5885,7 +5870,6 @@ "string-width": { "version": "1.0.2", "bundled": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -5903,7 +5887,6 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -5942,13 +5925,11 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true, - "optional": true + "bundled": true }, "yallist": { "version": "3.0.2", - "bundled": true, - "optional": true + "bundled": true } } }, @@ -8312,6 +8293,25 @@ "js-tokens": "^3.0.0 || ^4.0.0" } }, + "lowdb": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lowdb/-/lowdb-1.0.0.tgz", + "integrity": "sha512-2+x8esE/Wb9SQ1F9IHaYWfsC9FIecLOPrK4g17FGEayjUWH172H6nwicRovGvSE2CPZouc2MCIqCI7h9d+GftQ==", + "requires": { + "graceful-fs": "^4.1.3", + "is-promise": "^2.1.0", + "lodash": "4", + "pify": "^3.0.0", + "steno": "^0.4.1" + }, + "dependencies": { + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" + } + } + }, "lower-case": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz", @@ -13329,6 +13329,14 @@ "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" }, + "steno": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/steno/-/steno-0.4.4.tgz", + "integrity": "sha1-BxEFvfwobmYVwEA8J+nXtdy4Vcs=", + "requires": { + "graceful-fs": "^4.1.3" + } + }, "stream-browserify": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz", diff --git a/package.json b/package.json index dedacce..682ab72 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,10 @@ "private": true, "dependencies": { "axios": "^0.18.0", + "body-parser": "^1.18.3", "bootstrap": "^4.1.3", "express": "^4.16.4", + "lowdb": "^1.0.0", "react": "^16.6.1", "react-app-polyfill": "^0.1.3", "react-bootstrap": "^1.0.0-beta.2", From 0f38bae769a704de2cbca8ba38f0f4d50683b88f Mon Sep 17 00:00:00 2001 From: matteo Date: Sun, 2 Dec 2018 21:02:12 +0100 Subject: [PATCH 2/3] visualizzazioni video tracciate --- node-entry.js | 58 +++++++++++++++++++++++++++++++++++++++++----- package-lock.json | 5 ++++ package.json | 1 + src/VideoPlayer.js | 14 ++++++++--- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/node-entry.js b/node-entry.js index 81fcbfd..e572520 100644 --- a/node-entry.js +++ b/node-entry.js @@ -1,22 +1,68 @@ const express = require('express'); const bodyParser = require('body-parser'); const low = require('lowdb'); +const lodashId = require('lodash-id'); const FileAsync = require('lowdb/adapters/FileAsync'); const app = express(); // app is an instance of express +const axios = require('axios'); app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded app.use(bodyParser.json()) // parse application/json app.use(express.static(__dirname + '/build')); // serve static build site -app.post('/test',(req,res)=>{ - res.send(req.body); -}) + + +app.get('/test/:id',(req,res) =>{ + axios.post('http://localhost:8000/test', { + "id": req.params.id.toString(), + "reason": req.query.reason.toString() + }).then(response => res.send(response.data), error => console.log(error)) +}); // Create database instance and start server // const adapter = new FileAsync(__dirname + '/db.json'); -low(new FileAsync(__dirname + '/db.json')) +low(new FileAsync(__dirname + '/db.json')) // production +// low(new FileAsync('/tmp/db.json')) // dev .then(db => { + db._.mixin(lodashId); // DATABASE ROUTES +// ============== + + app.post('/test',(req,res)=>{ + //console.log(req.body); + const table = db.get('videos'); + let row = table.getById(req.body.id); + if(row.value()) { + row + .update('timesWatched',(n) => ++n) + .update('lastWatched',() => new Date().toISOString()) + .write() + .then(final => res.send(final)) + } else { + // "reason": { + // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // } + let post = { + "id": req.body.id.toString(), + "timesWatched": 1, + "lastWatched": new Date().toISOString(), + } + table.push(post).last().write().then(final => res.send(final)) + } + + // try { + // let test = table.updateById(req.body.id, { }) + // } + // catch(e) { + + // } + + }); + +// ============== + // GET /videolog app.get('/videolog', (req, res) => { const post = db @@ -31,9 +77,9 @@ low(new FileAsync(__dirname + '/db.json')) db.get('videos') .push(req.body) .last() - .update('timesWatched', n => n + 1 ) + //.update('timesWatched', n => n + 1 ) .write() - .then(post => res.send(post)); // non funziona minimamente lol + .then(post => res.send(post)); // }); // Set db default values diff --git a/package-lock.json b/package-lock.json index 30554be..56822a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8228,6 +8228,11 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, + "lodash-id": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/lodash-id/-/lodash-id-0.14.0.tgz", + "integrity": "sha1-uvSJNOVDobXWNG+MhGmLGoyAOJY=" + }, "lodash._reinterpolate": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz", diff --git a/package.json b/package.json index 682ab72..0119f16 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "body-parser": "^1.18.3", "bootstrap": "^4.1.3", "express": "^4.16.4", + "lodash-id": "^0.14.0", "lowdb": "^1.0.0", "react": "^16.6.1", "react-app-polyfill": "^0.1.3", diff --git a/src/VideoPlayer.js b/src/VideoPlayer.js index 33ba5ad..2a40472 100644 --- a/src/VideoPlayer.js +++ b/src/VideoPlayer.js @@ -1,5 +1,6 @@ import React, { Component } from 'react'; // eslint-disable-line no-unused-vars import YouTube from 'react-youtube'; // eslint-disable-line no-unused-vars +import axios from 'axios'; /* eslint no-console: ["error", { allow: ["info", "error", "warn"] }] */ class VideoPlayer extends Component { @@ -91,8 +92,7 @@ class VideoPlayer extends Component { break; } }, 500); - this.setState({outsideReject2: reject }); - + this.setState({ outsideReject2: reject }); }) }); this.state.promise2.then(msg => { @@ -111,8 +111,16 @@ class VideoPlayer extends Component { this.state.promise1 .then( risultato => { - console.info(risultato); + //console.info(risultato); // post request to local db + axios + .post('http://localhost:8000/test', { + id: risultato.toString() + }) + .then( + response => console.info(response.data), + error => console.info(error) + ); // local storage logic for recent reccomender clearInterval(timerId1); }, From 5f093c35d653d693104a2c0e44efc6fbe5f40cd4 Mon Sep 17 00:00:00 2001 From: matteo Date: Mon, 3 Dec 2018 01:22:48 +0100 Subject: [PATCH 3/3] minor fixes --- node-entry.js | 92 +++++++++++++++------------------------------- src/VideoList.js | 29 +++++++++------ src/VideoPlayer.js | 16 ++++---- 3 files changed, 55 insertions(+), 82 deletions(-) diff --git a/node-entry.js b/node-entry.js index e572520..42f372f 100644 --- a/node-entry.js +++ b/node-entry.js @@ -4,20 +4,11 @@ const low = require('lowdb'); const lodashId = require('lodash-id'); const FileAsync = require('lowdb/adapters/FileAsync'); const app = express(); // app is an instance of express -const axios = require('axios'); app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded app.use(bodyParser.json()) // parse application/json app.use(express.static(__dirname + '/build')); // serve static build site - -app.get('/test/:id',(req,res) =>{ - axios.post('http://localhost:8000/test', { - "id": req.params.id.toString(), - "reason": req.query.reason.toString() - }).then(response => res.send(response.data), error => console.log(error)) -}); - // Create database instance and start server // const adapter = new FileAsync(__dirname + '/db.json'); low(new FileAsync(__dirname + '/db.json')) // production @@ -29,72 +20,49 @@ low(new FileAsync(__dirname + '/db.json')) // production // ============== app.post('/test',(req,res)=>{ - //console.log(req.body); - const table = db.get('videos'); - let row = table.getById(req.body.id); + // "reason": { + // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // } + // try { + // let test = table.updateById(req.body.id, { }) + // } + // catch(e) { + // } + res.send(req.body); + }); + +// ============== + + // GET /videotrack/:id? + app.get('/videotrack/:id?', (req, res) => { + const videos = db.get('videos'); + req.params.id ? res.send(videos.getById(req.params.id).value()) : res.send(videos.value()) + }); + + // POST /videotrack + app.post('/videotrack', (req, res) => { + const videos = db.get('videos'); + const row = videos.getById(req.body.id); if(row.value()) { row .update('timesWatched',(n) => ++n) .update('lastWatched',() => new Date().toISOString()) .write() - .then(final => res.send(final)) + .then(output => res.send(output)) } else { - // "reason": { - // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, - // "": req.body.reason.toString() === "" ? 1:0, - // "": req.body.reason.toString() === "" ? 1:0, - // } - let post = { + let newRow = { "id": req.body.id.toString(), "timesWatched": 1, "lastWatched": new Date().toISOString(), } - table.push(post).last().write().then(final => res.send(final)) + videos.push(newRow).last().write().then(output => res.send(output)) } + }); - // try { - // let test = table.updateById(req.body.id, { }) - // } - // catch(e) { - - // } - - }); - -// ============== - - // GET /videolog - app.get('/videolog', (req, res) => { - const post = db - .get('videos') - // .find({ id: req.params.id }) - .value(); - res.send(post); - }); - - // POST /videolog - app.post('/videolog', (req, res) => { - db.get('videos') - .push(req.body) - .last() - //.update('timesWatched', n => n + 1 ) - .write() - .then(post => res.send(post)); // - }); - // Set db default values - return db - .defaults({ - videos: [ - // { - // videoId: '', - // timesWatched: 0, - // prevalentReason: '', - // lastSelected: new Date() - // } - ] - }) - .write(); + return db.defaults({ videos: [] }).write(); }) .then(() => { app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs diff --git a/src/VideoList.js b/src/VideoList.js index c098398..d8bc5df 100644 --- a/src/VideoList.js +++ b/src/VideoList.js @@ -36,7 +36,7 @@ class VideoList extends React.Component { ); } -// METODI + // METODI getThumbnails(videoItems) { let idToLookFor = videoItems.map(item => item.videoID); let finalChunk = (videoItems.length % 50) + 100; @@ -125,29 +125,36 @@ class VideoList extends React.Component { {items.map(item => ( - + }} + > {/* {item.artist} - {item.title} {item.videoID} {item.category} */} - + {item.artist} - {item.title} -

{item.category}

+

+ {item.category} +

- ))} + ))} ); } } } -export default VideoList; \ No newline at end of file +export default VideoList; diff --git a/src/VideoPlayer.js b/src/VideoPlayer.js index 2a40472..e27ab78 100644 --- a/src/VideoPlayer.js +++ b/src/VideoPlayer.js @@ -64,16 +64,15 @@ class VideoPlayer extends Component { ); } - + + // access to player in all event handlers via event.target _onError(event) { - console.error('on error', event); + console.error('Error: ', event); } _onPlay(event) {} - _onReady(event) { - // access to player in all event handlers via event.target - } + _onReady(event) {} _onStateChange(event) { let timerId1, timerId2; @@ -110,12 +109,11 @@ class VideoPlayer extends Component { }); this.state.promise1 .then( - risultato => { - //console.info(risultato); + videoIdWatched => { // post request to local db axios - .post('http://localhost:8000/test', { - id: risultato.toString() + .post('http://localhost:8000/videotrack', { + id: videoIdWatched.toString() }) .then( response => console.info(response.data),