Sfoglia il codice sorgente

Host images locally

Tatiana Inama 6 anni fa
parent
commit
89baabaa87
83 ha cambiato i file con 47 aggiunte e 6 eliminazioni
  1. 1 1
      scraper/nodemon.json
  2. BIN
      scraper/public/images/fishes/Anchovy.png
  3. BIN
      scraper/public/images/fishes/Angelfish.png
  4. BIN
      scraper/public/images/fishes/Arapaima.png
  5. BIN
      scraper/public/images/fishes/Arowana.png
  6. BIN
      scraper/public/images/fishes/Barred-knifejaw.png
  7. BIN
      scraper/public/images/fishes/Barreleye.png
  8. BIN
      scraper/public/images/fishes/Betta.png
  9. BIN
      scraper/public/images/fishes/Bitterling.png
  10. BIN
      scraper/public/images/fishes/Black-bass.png
  11. BIN
      scraper/public/images/fishes/Blowfish.png
  12. BIN
      scraper/public/images/fishes/Blue-marlin.png
  13. BIN
      scraper/public/images/fishes/Bluegill.png
  14. BIN
      scraper/public/images/fishes/Butterfly-fish.png
  15. BIN
      scraper/public/images/fishes/Carp.png
  16. BIN
      scraper/public/images/fishes/Catfish.png
  17. BIN
      scraper/public/images/fishes/Char.png
  18. BIN
      scraper/public/images/fishes/Cherry-salmon.png
  19. BIN
      scraper/public/images/fishes/Clown-fish.png
  20. BIN
      scraper/public/images/fishes/Coelacanth.png
  21. BIN
      scraper/public/images/fishes/Crawfish.png
  22. BIN
      scraper/public/images/fishes/Crucian-carp.png
  23. BIN
      scraper/public/images/fishes/Dab.png
  24. BIN
      scraper/public/images/fishes/Dace.png
  25. BIN
      scraper/public/images/fishes/Dorado.png
  26. BIN
      scraper/public/images/fishes/Football-fish.png
  27. BIN
      scraper/public/images/fishes/Freshwater-goby.png
  28. BIN
      scraper/public/images/fishes/Frog.png
  29. BIN
      scraper/public/images/fishes/Gar.png
  30. BIN
      scraper/public/images/fishes/Giant-snakehead.png
  31. BIN
      scraper/public/images/fishes/Giant-trevally.png
  32. BIN
      scraper/public/images/fishes/Golden-trout.png
  33. BIN
      scraper/public/images/fishes/Goldfish.png
  34. BIN
      scraper/public/images/fishes/Great-white shark.png
  35. BIN
      scraper/public/images/fishes/Guppy.png
  36. BIN
      scraper/public/images/fishes/Hammerhead-shark.png
  37. BIN
      scraper/public/images/fishes/Horse-mackerel.png
  38. BIN
      scraper/public/images/fishes/Killifish.png
  39. BIN
      scraper/public/images/fishes/King-salmon.png
  40. BIN
      scraper/public/images/fishes/Koi.png
  41. BIN
      scraper/public/images/fishes/Loach.png
  42. BIN
      scraper/public/images/fishes/Mahi-mahi.png
  43. BIN
      scraper/public/images/fishes/Mitten-crab.png
  44. BIN
      scraper/public/images/fishes/Moray-eel.png
  45. BIN
      scraper/public/images/fishes/Napoleonfish.png
  46. BIN
      scraper/public/images/fishes/Neon-tetra.png
  47. BIN
      scraper/public/images/fishes/Nibble-fish.png
  48. BIN
      scraper/public/images/fishes/Oarfish.png
  49. BIN
      scraper/public/images/fishes/Ocean-sunfish.png
  50. BIN
      scraper/public/images/fishes/Olive-flounder.png
  51. BIN
      scraper/public/images/fishes/Pale-chub.png
  52. BIN
      scraper/public/images/fishes/Pike.png
  53. BIN
      scraper/public/images/fishes/Piranha.png
  54. BIN
      scraper/public/images/fishes/Pond-smelt.png
  55. BIN
      scraper/public/images/fishes/Pop-eyed-goldfish.png
  56. BIN
      scraper/public/images/fishes/Puffer-fish.png
  57. BIN
      scraper/public/images/fishes/Rainbowfish.png
  58. BIN
      scraper/public/images/fishes/Ranchu-goldfish.png
  59. BIN
      scraper/public/images/fishes/Ray.png
  60. BIN
      scraper/public/images/fishes/Red-snapper.png
  61. BIN
      scraper/public/images/fishes/Ribbon-eel.png
  62. BIN
      scraper/public/images/fishes/Saddled-bichir.png
  63. BIN
      scraper/public/images/fishes/Salmon.png
  64. BIN
      scraper/public/images/fishes/Saw-shark.png
  65. BIN
      scraper/public/images/fishes/Sea-bass.png
  66. BIN
      scraper/public/images/fishes/Sea-butterfly.png
  67. BIN
      scraper/public/images/fishes/Sea-horse.png
  68. BIN
      scraper/public/images/fishes/Snapping-Turtle.png
  69. BIN
      scraper/public/images/fishes/Soft-shelled-turtle.png
  70. BIN
      scraper/public/images/fishes/Squid.png
  71. BIN
      scraper/public/images/fishes/Stringfish.png
  72. BIN
      scraper/public/images/fishes/Sturgeon.png
  73. BIN
      scraper/public/images/fishes/Suckerfish.png
  74. BIN
      scraper/public/images/fishes/Surgeonfish.png
  75. BIN
      scraper/public/images/fishes/Sweetfish.png
  76. BIN
      scraper/public/images/fishes/Tadpole.png
  77. BIN
      scraper/public/images/fishes/Tilapia.png
  78. BIN
      scraper/public/images/fishes/Tuna.png
  79. BIN
      scraper/public/images/fishes/Whale-shark.png
  80. BIN
      scraper/public/images/fishes/Yellow-perch.png
  81. BIN
      scraper/public/images/fishes/Zebra-turkeyfish.png
  82. 5 3
      scraper/src/server.ts
  83. 41 2
      scraper/src/scraper.ts

+ 1 - 1
scraper/nodemon.json

@@ -2,5 +2,5 @@
   "watch": ["src"],
   "watch": ["src"],
   "ext": "ts",
   "ext": "ts",
   "ignore": ["src/**/*.spec.ts"],
   "ignore": ["src/**/*.spec.ts"],
-  "exec": "ts-node ./src/server.ts"
+  "exec": "ts-node ./server.ts"
 }
 }

BIN
scraper/public/images/fishes/Anchovy.png


BIN
scraper/public/images/fishes/Angelfish.png


BIN
scraper/public/images/fishes/Arapaima.png


BIN
scraper/public/images/fishes/Arowana.png


BIN
scraper/public/images/fishes/Barred-knifejaw.png


BIN
scraper/public/images/fishes/Barreleye.png


BIN
scraper/public/images/fishes/Betta.png


BIN
scraper/public/images/fishes/Bitterling.png


BIN
scraper/public/images/fishes/Black-bass.png


BIN
scraper/public/images/fishes/Blowfish.png


BIN
scraper/public/images/fishes/Blue-marlin.png


BIN
scraper/public/images/fishes/Bluegill.png


BIN
scraper/public/images/fishes/Butterfly-fish.png


BIN
scraper/public/images/fishes/Carp.png


BIN
scraper/public/images/fishes/Catfish.png


BIN
scraper/public/images/fishes/Char.png


BIN
scraper/public/images/fishes/Cherry-salmon.png


BIN
scraper/public/images/fishes/Clown-fish.png


BIN
scraper/public/images/fishes/Coelacanth.png


BIN
scraper/public/images/fishes/Crawfish.png


BIN
scraper/public/images/fishes/Crucian-carp.png


BIN
scraper/public/images/fishes/Dab.png


BIN
scraper/public/images/fishes/Dace.png


BIN
scraper/public/images/fishes/Dorado.png


BIN
scraper/public/images/fishes/Football-fish.png


BIN
scraper/public/images/fishes/Freshwater-goby.png


BIN
scraper/public/images/fishes/Frog.png


BIN
scraper/public/images/fishes/Gar.png


BIN
scraper/public/images/fishes/Giant-snakehead.png


BIN
scraper/public/images/fishes/Giant-trevally.png


BIN
scraper/public/images/fishes/Golden-trout.png


BIN
scraper/public/images/fishes/Goldfish.png


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


BIN
scraper/public/images/fishes/Guppy.png


BIN
scraper/public/images/fishes/Hammerhead-shark.png


BIN
scraper/public/images/fishes/Horse-mackerel.png


BIN
scraper/public/images/fishes/Killifish.png


BIN
scraper/public/images/fishes/King-salmon.png


BIN
scraper/public/images/fishes/Koi.png


BIN
scraper/public/images/fishes/Loach.png


BIN
scraper/public/images/fishes/Mahi-mahi.png


BIN
scraper/public/images/fishes/Mitten-crab.png


BIN
scraper/public/images/fishes/Moray-eel.png


BIN
scraper/public/images/fishes/Napoleonfish.png


BIN
scraper/public/images/fishes/Neon-tetra.png


BIN
scraper/public/images/fishes/Nibble-fish.png


BIN
scraper/public/images/fishes/Oarfish.png


BIN
scraper/public/images/fishes/Ocean-sunfish.png


BIN
scraper/public/images/fishes/Olive-flounder.png


BIN
scraper/public/images/fishes/Pale-chub.png


BIN
scraper/public/images/fishes/Pike.png


BIN
scraper/public/images/fishes/Piranha.png


BIN
scraper/public/images/fishes/Pond-smelt.png


BIN
scraper/public/images/fishes/Pop-eyed-goldfish.png


BIN
scraper/public/images/fishes/Puffer-fish.png


BIN
scraper/public/images/fishes/Rainbowfish.png


BIN
scraper/public/images/fishes/Ranchu-goldfish.png


BIN
scraper/public/images/fishes/Ray.png


BIN
scraper/public/images/fishes/Red-snapper.png


BIN
scraper/public/images/fishes/Ribbon-eel.png


BIN
scraper/public/images/fishes/Saddled-bichir.png


BIN
scraper/public/images/fishes/Salmon.png


BIN
scraper/public/images/fishes/Saw-shark.png


BIN
scraper/public/images/fishes/Sea-bass.png


BIN
scraper/public/images/fishes/Sea-butterfly.png


BIN
scraper/public/images/fishes/Sea-horse.png


BIN
scraper/public/images/fishes/Snapping-Turtle.png


BIN
scraper/public/images/fishes/Soft-shelled-turtle.png


BIN
scraper/public/images/fishes/Squid.png


BIN
scraper/public/images/fishes/Stringfish.png


BIN
scraper/public/images/fishes/Sturgeon.png


BIN
scraper/public/images/fishes/Suckerfish.png


BIN
scraper/public/images/fishes/Surgeonfish.png


BIN
scraper/public/images/fishes/Sweetfish.png


BIN
scraper/public/images/fishes/Tadpole.png


BIN
scraper/public/images/fishes/Tilapia.png


BIN
scraper/public/images/fishes/Tuna.png


BIN
scraper/public/images/fishes/Whale-shark.png


BIN
scraper/public/images/fishes/Yellow-perch.png


BIN
scraper/public/images/fishes/Zebra-turkeyfish.png


+ 5 - 3
scraper/src/server.ts

@@ -1,19 +1,21 @@
 import express from 'express';
 import express from 'express';
 
 
-import scrape from './scraper';
+import scrape, {saveImage} from './src/scraper';
 
 
 const app: express.Application = express();
 const app: express.Application = express();
 const port: number = 3001;
 const port: number = 3001;
 
 
+app.use(express.static('public'))
+
 app.get('/', (req, res) => {
 app.get('/', (req, res) => {
   scrape();
   scrape();
   res.send('Hello World!')
   res.send('Hello World!')
 });
 });
 
 
 app.get('/fishes', (req, res) => {
 app.get('/fishes', (req, res) => {
-  return scrape().then(fishes => {
+  return scrape().then(saveImage).then((fishes => {
     return res.send(fishes);
     return res.send(fishes);
-  })
+  }))
 })
 })
 
 
 app.listen(port, () => {
 app.listen(port, () => {

+ 41 - 2
scraper/src/scraper.ts

@@ -1,11 +1,23 @@
 import cheerio from 'cheerio';
 import cheerio from 'cheerio';
 import axios from 'axios';
 import axios from 'axios';
 import { splitAt } from 'ramda';
 import { splitAt } from 'ramda';
+import fs from 'fs';
+import express from 'express';
 
 
 type DataParser = (td: Cheerio) => ({
 type DataParser = (td: Cheerio) => ({
   [x: string]: number | string | [number, number][]
   [x: string]: number | string | [number, number][]
 });
 });
 
 
+type Fish = {
+  name: string,
+  img: string,
+  price: number,
+  location: string,
+  shadowSize: number,
+  time: [number, number][],
+  months: number[],
+}
+
 const mkLocation = (location: string): string => {
 const mkLocation = (location: string): string => {
   switch (location) {
   switch (location) {
     case 'River (Clifftop)' || 'River (Clifftop)  Pond':
     case 'River (Clifftop)' || 'River (Clifftop)  Pond':
@@ -47,6 +59,33 @@ const parseMonths = (tr: CheerioElement[]) => {
   }, [])
   }, [])
 }
 }
 
 
+export const saveImage = (fishes: Fish[]) => {
+  return Promise.all(fishes.map(async fish => {
+    const location = `images/fishes/${fish.name.replace(' ', '-')}.png`;
+    
+    const writer = fs.createWriteStream(`${__dirname}/../public/${location}`)
+    
+    const response = await axios.get(fish.img, {
+      responseType: 'stream'
+    });
+  
+    response.data.pipe(writer)
+    
+    return new Promise((resolve, reject) => {
+      writer.on('finish', resolve)
+      writer.on('error', reject)
+    }).then(result => {
+      return {
+        ...fish,
+        img: location
+      }
+    })
+  })).catch(error => {
+    console.error(error)
+    return fishes
+  })
+}
+
 const scrape = async () => {
 const scrape = async () => {
   const html = await axios.get('https://animalcrossing.fandom.com/wiki/Fish_(New_Horizons)');
   const html = await axios.get('https://animalcrossing.fandom.com/wiki/Fish_(New_Horizons)');
   const $ = cheerio.load(html.data, { normalizeWhitespace: true });
   const $ = cheerio.load(html.data, { normalizeWhitespace: true });
@@ -61,7 +100,7 @@ const scrape = async () => {
           ...fish,
           ...fish,
           ...fishPropMap[i]($(td))
           ...fishPropMap[i]($(td))
         };
         };
-      }, {}),
+      }, {} as Fish),
       months: parseMonths(months)
       months: parseMonths(months)
     };
     };
 
 
@@ -69,7 +108,7 @@ const scrape = async () => {
       ...fishes,
       ...fishes,
       fish
       fish
     ]
     ]
-  }, [] as object[]);
+  }, [] as Fish[]);
 
 
   return fishes;
   return fishes;
 }
 }