ソースを参照

Add README, Fix public paths

Tatiana Inama 6 年 前
コミット
9c635b3769

+ 3 - 1
.gitignore

@@ -13,6 +13,7 @@ node_modules
 
 # misc
 .DS_Store
+.env
 .env.local
 .env.development.local
 .env.test.local
@@ -22,4 +23,5 @@ npm-debug.log*
 yarn-debug.log*
 yarn-error.log*
 
-/scraper/public
+/scraper/public
+/scraper/*/public

+ 24 - 0
README.md

@@ -0,0 +1,24 @@
+# Critter Crossing
+
+Guide to catch all the fishes/insects from Animal Crossing.
+
+## Install
+
+`npm install scraper/ & npm install app/`
+
+You also need config files with the following values:
+
+### Scraper
+```
+PORT
+AC_FISH='https://animalcrossing.fandom.com/wiki/Fish_(New_Horizons)' **WIKI PATH
+AC_INSECT='https://animalcrossing.fandom.com/wiki/Bugs_(New_Horizons)' **WIKI PATH
+PUBLIC_PATH
+```
+
+### App
+
+```
+REACT_APP_API **PATH to scraper
+```
+

+ 2 - 1
scraper/.env

@@ -1,3 +1,4 @@
 PORT=3001
 AC_FISH='https://animalcrossing.fandom.com/wiki/Fish_(New_Horizons)'
-AC_INSECT='https://animalcrossing.fandom.com/wiki/Bugs_(New_Horizons)'
+AC_INSECT='https://animalcrossing.fandom.com/wiki/Bugs_(New_Horizons)'
+PUBLIC_PATH='/mnt/c/Users/tati/git/ac-guide/scraper/public'

BIN
scraper/public/images/fishes/Great-white shark.png


BIN
scraper/public/images/insects/Blue-weevil beetle.png


BIN
scraper/public/images/insects/Citrus-long-horned beetle.png


BIN
scraper/public/images/insects/Earth-boring-dung beetle.png


BIN
scraper/public/images/insects/Giant-water bug.png


BIN
scraper/public/images/insects/Great-purple emperor.png


BIN
scraper/public/images/insects/Madagascan-sunset moth.png


BIN
scraper/public/images/insects/Man-faced-stink bug.png


BIN
scraper/public/images/insects/Paper-kite butterfly.png


BIN
scraper/public/images/insects/Queen-Alexandra's birdwing.png


BIN
scraper/public/images/insects/Rajah-Brooke's birdwing.png


BIN
scraper/public/images/insects/Rosalia-batesi beetle.png


+ 1 - 1
scraper/src/scraper.ts

@@ -93,7 +93,7 @@ export const saveImage = (downloadPath: string, critterType: string) => (critter
     const _path = `${critterType}/${critter.name.replace(/ /g, '-')}.png`;
     return downloadImg(critter.imgUrl, `${downloadPath}/${_path}`).then(result => ({
       ...critter,
-      img: `images/${_path}`
+      imgUrl: `${_path}`
     }))
   })).catch(error => {
     console.error(error)

+ 31 - 13
scraper/src/server.ts

@@ -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}`);
 });