|
|
@@ -4,23 +4,29 @@ import fs from 'fs';
|
|
|
import { saveImage, scrapeCritters, CONFIG, storeData } from './scraper';
|
|
|
|
|
|
const app: express.Application = express();
|
|
|
-const publicDataPath = process.env.DATA_PATH || `${__dirname}/../public/data`;
|
|
|
-const publicImagesPath = process.env.IMG_PATH || `${__dirname}/../public/images`;
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
-if (process.env.AC_FISH === undefined || process.env.AC_INSECT === undefined) {
|
|
|
- console.error("Could not load config file")
|
|
|
+
|
|
|
+if (process.env.AC_FISH === undefined || process.env.AC_INSECT === undefined || process.env.PUBLIC_PATH === undefined) {
|
|
|
+ console.error("Could not load config file. See needed variables in README")
|
|
|
process.exit();
|
|
|
}
|
|
|
|
|
|
+const {
|
|
|
+ AC_FISH,
|
|
|
+ AC_INSECT,
|
|
|
+ PUBLIC_PATH,
|
|
|
+ PORT
|
|
|
+} = process.env;
|
|
|
+
|
|
|
const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
|
|
|
console.error(err);
|
|
|
res.status(500);
|
|
|
res.send({ error: err });
|
|
|
}
|
|
|
|
|
|
-app.use(express.static('public', {
|
|
|
+app.use(express.static(PUBLIC_PATH, {
|
|
|
setHeaders: function setHeaders(res, path, stat) {
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
|
res.header('Access-Control-Allow-Methods', 'GET');
|
|
|
@@ -43,19 +49,31 @@ app.get('/', (req, res) => { res.send('Hello World!') });
|
|
|
|
|
|
|
|
|
const getFishes = () => {
|
|
|
- scrapeCritters({url: process.env.AC_FISH!, type: CONFIG.Fish}).then(saveImage(publicImagesPath, 'fishes')).then(storeData(`${publicDataPath}/fishes.json`));
|
|
|
+ console.info('scraping fishes...')
|
|
|
+ scrapeCritters({url: AC_FISH, type: CONFIG.Fish})
|
|
|
+ .then(saveImage(`${PUBLIC_PATH}/images`, 'fishes'))
|
|
|
+ .then(storeData(`${PUBLIC_PATH}/data/fishes.json`));
|
|
|
}
|
|
|
|
|
|
const getInsects = () => {
|
|
|
- scrapeCritters({url:process.env.AC_INSECT!, type: CONFIG.Insect}).then(saveImage(publicImagesPath, 'insects')).then(storeData(`${publicDataPath}/insects.json`))
|
|
|
+ console.info('scraping insects...')
|
|
|
+ scrapeCritters({url: AC_INSECT, type: CONFIG.Insect})
|
|
|
+ .then(saveImage(`${PUBLIC_PATH}/images`, 'insects'))
|
|
|
+ .then(storeData(`${PUBLIC_PATH}/data/insects.json`))
|
|
|
}
|
|
|
|
|
|
-app.listen(process.env.PORT, () => {
|
|
|
- [publicDataPath, `${publicImagesPath}/insects`, `${publicImagesPath}/fishes`].map(path => {
|
|
|
- fs.mkdirSync(path, {recursive: true})
|
|
|
- })
|
|
|
-
|
|
|
+app.listen(PORT, () => {
|
|
|
+ try {
|
|
|
+ console.info('Creating required paths:');
|
|
|
+ ['data', `images/insects`, `images/fishes`].map(path => {
|
|
|
+ console.info(`-- ${PUBLIC_PATH}/${path}`)
|
|
|
+ fs.mkdirSync(`${PUBLIC_PATH}/${path}`, {recursive: true})
|
|
|
+ })
|
|
|
+ } catch (e) {
|
|
|
+ console.error("Could not create required path folders: ", e);
|
|
|
+ process.exit();
|
|
|
+ }
|
|
|
getFishes();
|
|
|
getInsects();
|
|
|
- console.log(`Scrapper app listening at http://localhost:${process.env.PORT}`);
|
|
|
+ console.log(`All done, app listening at http://localhost:${PORT}`);
|
|
|
});
|