Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranomahony98 committed Mar 26, 2021
1 parent 5412a25 commit 60e036e
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

NODE_ENV = "developement",
MONGO_URI = mongodb+srv://kieranomahony98:[email protected]/CineGuestion?w=majority?retryWrites=true,
SECRET_OR_KEY = "bAKVdqczerYAYKdMxsaBzbFUJU6ZvL2LwZuxhtpS",
TMDB3= d61cca13e8ffa8187cbf047d7bca9ffb
15 changes: 14 additions & 1 deletion helpers/genres.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ export const matchedGenres = {
'28': ['18', '12', '53'],
'12': ['10752', '80', '10749'],
'16': ['35', '28', '18'],
'35': ['53', '10751', '18']
'35': ['53', '10751', '18'],
'37': ['28', '12', '10752'],
'80': ['28', '53', '35'],
'99': ['36', '12', '10751'],
'18': ['80', '9648', '10752'],
'10751': ['16', '35', '10749'],
'14': ['12', '16'],
'36': ['99', '10752', '28'],
'27': ['14', '878', '53'],
'9648': ['18', '80', '28'],
'10749': ['35', '10751'],
'878': ['53', '12'],
'53': ['28', '9648'],
'10752': ['37', '28', '53']
}

export async function listMatcher(movieGenres) {
Expand Down
63 changes: 63 additions & 0 deletions mongoModels/Trending.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import mongoose, { Schema } from 'mongoose';


const Trending = new Schema({
movieGenerationDate: {
type: String,
required: true,
},
movieSearchCriteria: {
sort_by: {
type: String,
required: false,
},
with_genres: {
type: String,
required: false,
},
primary_release_year: {
type: String,
required: false,
},
with_keywords: {
type: String,
required: false
}
},
movies:
[
{
movieId: {
type: Number,
required: true,
},
movieTitle: {
type: String,
required: true,
},
movieImagePath: {
type: String,
required: true
},
movieDescription: {
type: String,
required: true,
},
movieReleaseYear: {
type: String,
required: false,
},
movieGenres: {
type: String,
required: true,
},
moviePopularity: {
type: String,
required: false,
},

},
],
});

export default mongoose.model("trending", Trending);
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "node dist/server.js",
"build": "",
"storage": "rm -rf dist && mkdir dist && babel . --ignore node_modules -s -d dist",
"local": "PORT=8000 babel-node server.js"
"local": "PORT=8000 nodemon --exec babel-node server.js"
},
"dependencies": {
"@babel/core": "^7.12.10",
Expand All @@ -18,7 +18,6 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"helmet": "^4.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.11",
"moviedb-promise": "^3.1.6",
"node-cron": "^2.0.3",
Expand All @@ -43,7 +42,7 @@
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"nodemon": "^2.0.6"
"nodemon": "^2.0.7"
},
"keywords": [],
"author": "",
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# How to run this Application:

1. Install NodeJs on your computer. https://nodejs.org/en/ the LTS version will suffice.
2. Navigate to the root level of this project within your terminal and run command `npm i`. This is to install project dependencies.
3. run command `npm run local` This will start the automation side of the application.

This part of the application runs on scheduled functions, if you look in the terminal you will see a sample function getting executed every second.
3 changes: 0 additions & 3 deletions routes/api/playlistApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ router.post('/testing', async (req, res) => {
});
});

router.post('/test', async (req, res) => {
res.send("im working");
});

export default router;

Expand Down
6 changes: 4 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ app.listen(process.env.PORT, () => {
logger.info(`app is listening to port ${process.env.PORT}`);
});
// connect to db
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
export const db = mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((db) => {
logger.info('Mongoose successfully connected');
return db;

}).catch((err) => {
logger.error(err);
});
Expand Down
42 changes: 37 additions & 5 deletions services/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ cron.schedule('0 0 0 1 * *', () => {
getMonthlyPlaylistForUser(date);
});

cron.schedule('0 0 0 * * 1', () => {
const thisMonth = new Date().getMonth();
const lastMonth = (thisMonth == 0) ? new Date().setMonth(11) : new Date().setMonth(thisMonth - 1);
const date = new Date(lastMonth).toISOString();
getMonthlyPlaylistForUser(date);
getTrendingNow();
});

cron.schedule('* * * * * *', () => {
console.log('in job for demo');
});

async function userProcessing(allUsers, date, type) {
try {
const userPlaylists = [];
Expand All @@ -34,7 +46,7 @@ async function playlistCreationController(type, date) {
.then((allUsers) => userProcessing(allUsers, date, type))
.then((playlists) => playlists)
.catch((err) => {
logger.error(`Failed to get weekly playlists: ${err.message}`);
logger.error(`Failer in controller: ${err.message}`);
throw err;
}));
}
Expand All @@ -44,7 +56,7 @@ export async function getWeeklyPlaylist() {
return await playlistCreationController(0, new Date(lastWeek).toISOString())
.then((playlists) => playlists)
.catch((err) => {
logger.error(`Failed to get playlists: ${err.message}`);
logger.error(`Failed to get weeekly playlists: ${err.message}`);
});
}

Expand All @@ -63,12 +75,32 @@ export async function getAllTimePlaylist() {
return await playlistCreationController(2, null)
.then((playlists) => playlists)
.catch((err) => {
logger.error(`Failed to get playlists: ${err.message}`);
logger.error(`Failed to get all time playlists: ${err.message}`);
});
}

async function getTrendingNow() {
let lastWeek = new Date().getTime() - (86400000);
lastWeek = new Date(lastWeek).toISOString();
return await getMoviesFromDatabase()
.then(async (allUserData) => {
const userMovies = [].concat.apply([], allUserData.map((user) => {
return user.userMovies.filter((generation) => generation.movieGenerationDate >= lastWeek);
}));
const playlist = await getPlaylist(userMovies, null, 3)
.then((playlist) => playlist)
.catch((err) => {
logger.error(`failed to get playlists: ${err.message}`);
})
return playlist;
});
}

export async function doAll() {
await getWeeklyPlaylist();
await getMonthlyPlaylistForUser();
await getAllTimePlaylist();
await getWeeklyPlaylist();
await getTrendingNow();
return true
}
}

26 changes: 23 additions & 3 deletions services/movieDbService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { remove } from 'winston';
import { logger } from '../helpers/logger';
import MovieSchema from '../mongoModels/movieModel';

import TrendingSchema from '../mongoModels/Trending';
/**
* get movie curation for a user
* @param {String} userId
*/
export async function getMoviesFromDatabase() {
try {
return await MovieSchema.find({})
return await MovieSchema.find({}).lean()
.then((users) => users);
} catch (err) {
logger.error(`failed to retrieve user movies for automation`);
Expand All @@ -17,12 +17,32 @@ export async function getMoviesFromDatabase() {
}

export async function writeToDB(id, results, type) {
const playlist = (type === 0) ? 'userPlaylists.weeklyPlaylists' : (type === 1) ? 'userPlaylists.monthlyPlaylists' : 'userPlaylists.allTimePlaylists';
if (type === 3) {
return await writeTrending(results);
}

const playlist = (type === 0) ? 'userPlaylists.weeklyPlaylists' : (type === 1) ? 'userPlaylists.monthlyPlaylists' : 'userPlaylists.allTimePlaylists';
return await MovieSchema.updateOne({ _id: id }, { $set: { [playlist]: results } })
.then((user) => true)
.catch((err) => {
logger.error(`Failed to write to database: ${err.message}`);
throw err;
});
}

async function writeTrending(results) {
const isDeleted = await TrendingSchema.deleteMany({})
.then((deleted) => true)
.catch((err) => {
logger.error(`failed to delete trending page: ${err.message}`);
});
if (!isDeleted) {
return false;
}
return await new TrendingSchema(results).save()
.then((res) => true)
.catch((err) => {
logger.error(`Failed to write trending: ${err.message}`);
throw err;
})
}
Loading

0 comments on commit 60e036e

Please sign in to comment.