Skip to content

Commit

Permalink
Update Mongo version, add LastFM username
Browse files Browse the repository at this point in the history
  • Loading branch information
schulzetenberg committed Nov 30, 2020
1 parent be8dc94 commit abf7ce7
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 32 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ Deployment on Google Cloud Platform - Compute

Re-deploy on GCP
--------
Run build script from frontend/package.json
Create a build.zip file & upload to a new Github release

SSH into the GCP VM
```console
$ sudo su - bitnami
$ cd /home/bitnami/apps/collector/htdocs/
$ git pull
Download build.zip from https://github.com/schulzetenberg/data-collector/releases & unzip to frontend/
Download build.zip from https://github.com/schulzetenberg/data-collector/releases & unzip to frontend/ (the index.html path should be ~/apps/collector/htdocs/frontend/build/index.html)
$ ./start-data-collector.sh
```

Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ app.use(
saveUninitialized: true,
secret: secrets.sessionSecret,
store: new MongoStore({
url: secrets.db,
url: secrets.MongoUrl + secrets.db,
autoReconnect: true,
}),
cookie: cookieOpts,
Expand Down
16 changes: 10 additions & 6 deletions config/secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ module.exports = {

SSL: process.env.ssl || false,

db: process.env.DB || 'mongodb://localhost:27017/data-collector',
sslKeyPath: process.env.SSL_key_path || './key.pem',

sslCertPath: process.env.SSL_cert_path || './cert.pem',

MongoUrl: process.env.MongoUrl || 'mongodb://localhost:27017/',

db: process.env.DB || 'data-collector',

logLevel: process.env.LOG_LEVEL || 'debug',

Expand All @@ -25,11 +31,9 @@ module.exports = {
apiKey: process.env.SENDGRID_APIKEY || 'apiKey123',
},

cloudinary: {
cloud_name: process.env.CLOUDINARY_CLOUD_NAME || 'Your Cloud Name here',
api_key: process.env.CLOUDINARY_API_KEY || 'apiKey123',
api_secret: process.env.CLOUDINARY_API_SECRET || 'apiSecret123',
},
cloud_name: process.env.CLOUDINARY_CLOUD_NAME || 'Your Cloud Name here',
api_key: process.env.CLOUDINARY_API_KEY || 'apiKey123',
api_secret: process.env.CLOUDINARY_API_SECRET || 'apiSecret123',

defaults: {
emailTo: '[email protected]',
Expand Down
8 changes: 6 additions & 2 deletions controllers/brute-force.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const { MongoClient } = require('mongodb');

const response = require('../nodejs/response');
const secrets = require('../config/secrets');
const logger = require('../nodejs/log');

const store = new MongoStore((ready) => {
MongoClient.connect(secrets.db, (err, db) => {
MongoClient.connect(secrets.MongoUrl, (err, client) => {
if (err) throw err;

const db = client.db(secrets.db);

ready(db.collection('bruteforce-store'));
});
});
Expand All @@ -21,7 +25,7 @@ const failCallback = (req, res, next, nextValidRequestDate) => {
};

const handleStoreError = (error) => {
console.log(error);
logger.error(error);

// cause node to exit, hopefully restarting the process fixes the problem
// eslint-disable-next-line no-throw-literal
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/error-list/error-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const ErrorList: React.FC<{
}> = ({ errors }) => {
const classes = useStyles();

if (!errors || errors.length === 0) {
return <></>;
}

return (
<>
{errors.map((error: string, index: number) => (
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/account/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Account: React.FC = () => {
loadData();
}, []);

const handleSaveData = async (updatedData: any) => {
const handleSaveData = async (updatedData: any): Promise<void> => {
setLoading(true);

try {
Expand Down Expand Up @@ -79,7 +79,7 @@ const Account: React.FC = () => {
setShowProfile(false);
};

const handleSavePassword = async (body: { password: string; confirmPassword: string }) => {
const handleSavePassword = async (body: { password: string; confirmPassword: string }): Promise<void> => {
setLoadingPassword(true);

try {
Expand All @@ -98,7 +98,7 @@ const Account: React.FC = () => {
}
};

const handleRemove = async () => {
const handleRemove = async (): Promise<void> => {
setRemoveErrors([]);
setRemoveLoading(true);

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/pages/app-settings/music-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type FormData = {
spotifySecret: string;
spotifyId: string;
lastFmKey: string;
lastFmUsername: string;
cloudinaryUpload: boolean;
};

Expand All @@ -30,14 +31,15 @@ const MusicSettings: React.FC<{ data: FormData; isLoading: boolean; submit: any

useEffect(() => {
if (data) {
const { active, schedule, spotifySecret, spotifyId, lastFmKey, cloudinaryUpload } = data;
const { active, schedule, spotifySecret, spotifyId, lastFmKey, lastFmUsername, cloudinaryUpload } = data;

reset({
active,
schedule,
spotifySecret,
spotifyId,
lastFmKey,
lastFmUsername,
cloudinaryUpload,
});
}
Expand All @@ -52,6 +54,7 @@ const MusicSettings: React.FC<{ data: FormData; isLoading: boolean; submit: any
<TextField {...formProps} name="spotifySecret" label="Spotify Secret" type="text" />
<TextField {...formProps} name="spotifyId" label="Spotify ID" type="text" />
<TextField {...formProps} name="lastFmKey" label="LastFM Key" type="text" />
<TextField {...formProps} name="lastFmUsername" label="LastFM Username" type="text" />
<div className={classes.textCenter}>
<SwitchForm {...formProps} name="cloudinaryUpload" label="Upload Images to Cloudinary" />
</div>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { ReactElement } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';

// import Home from './pages/home/home';
Expand All @@ -12,18 +12,18 @@ import Account from './pages/account/account';
import PasswordReset from './pages/account/password-reset';
import AppSettings from './pages/app-settings/app-settings';

const AuthenticatedRoute = ({ component: Component, session, ...rest }: any): any => {
const AuthenticatedRoute = ({ component: Component, session, ...rest }: any): ReactElement => {
return (
<Route
{...rest}
render={(props: any): any =>
render={(props: any): ReactElement =>
session ? <Component {...props} /> : <Redirect to={`/sign-in?redirect=${props.location.pathname}`} />
}
/>
);
};

const PublicRoute = ({ component: Component, ...rest }: any): any => {
const PublicRoute = ({ component: Component, ...rest }: any): ReactElement => {
return <Route {...rest} render={(props: any): any => <Component {...props} />} />;
};

Expand Down
4 changes: 3 additions & 1 deletion models/app-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const appConfigSchema = new mongoose.Schema(
music: {
active: { type: Boolean, default: false },
lastFmKey: { type: String, default: '' },
lastFmUsername: { type: String, default: '' },
spotifyId: { type: String, default: '' },
spotifySecret: { type: String, default: '' },
cloudinaryUpload: { type: Boolean, default: false },
Expand Down Expand Up @@ -84,7 +85,8 @@ const appConfigSchema = new mongoose.Schema(
},
{ timestamps: true }, // Save createdAt and updatedAt fields
{ minimize: false }, // Save empty objects
{ strict: true } // Save only fields defined in the schema
{ strict: true }, // Save only fields defined in the schema
{ setDefaultsOnInsert: true }
);

module.exports = mongoose.model('appconfig', appConfigSchema);
2 changes: 1 addition & 1 deletion nodejs/agenda.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Agenda = require('agenda');
const secrets = require('../config/secrets');
const logger = require('./log');

const agenda = new Agenda({ db: { address: secrets.db } });
const agenda = new Agenda({ db: { address: secrets.MongoUrl + secrets.db } });
exports.agenda = agenda;

const appModules = require('./app-modules');
Expand Down
2 changes: 1 addition & 1 deletion nodejs/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require('mongoose');
const secrets = require('../config/secrets');
const logger = require('./log');

const url = secrets.db;
const url = secrets.MongoUrl + secrets.db;
mongoose.connect(url);
const db = mongoose.connection;

Expand Down
2 changes: 1 addition & 1 deletion nodejs/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.save = (userId) => {

function getProfile(config) {
return api
.get({ url: 'https://www.instagram.com/schulzetenberg/?__a=1' })
.get({ url: `https://www.instagram.com/${config.instagram.user}/?__a=1` })
.then((data) => {
const images = data.graphql.user.edge_owner_to_timeline_media.edges
.map(({ node }) => ({
Expand Down
14 changes: 5 additions & 9 deletions nodejs/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ function getTopArtists(config) {

if (!key) return Promise.reject('Missing LastFM key');

const url = `https://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=waterland15&limit=15&page=1&api_key=${key}&format=json&period=12month`;
const url = `https://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=${config.music.lastFmUsername}&limit=15&page=1&api_key=${key}&format=json&period=12month`;

return api.get({ url }).then((data) => {
if (
!data ||
!data.topartists ||
!data.topartists.artist ||
!data.topartists.artist.length ||
!data.topartists['@attr']
) {
if (!data || !data.topartists || !data.topartists.artist || !data.topartists.artist.length || !data.topartists['@attr']) {
return Promise.reject('Could not parse top artist data');
}

Expand Down Expand Up @@ -65,7 +59,9 @@ function recentTracks(promiseData) {
.unix();

const toDate = moment().unix();
const url = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=waterland15&limit=1&page=1&api_key=${promiseData.key}&format=json&from=${fromDate}&to=${toDate}`;

// eslint-disable-next-line max-len
const url = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${promiseData.config.music.lastFmUsername}&limit=1&page=1&api_key=${promiseData.key}&format=json&from=${fromDate}&to=${toDate}`;

return api.get({ url }).then((data) => {
if (!data || !data.recenttracks || !data.recenttracks['@attr']) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"lodash": "4.17.20",
"method-override": "3.0.0",
"moment": "2.27.0",
"mongodb": "2.2.36",
"mongodb": "3.6.3",
"mongoose": "5.10.3",
"morgan": "1.10.0",
"opml-to-json": "0.0.3",
Expand Down

0 comments on commit abf7ce7

Please sign in to comment.