Skip to content

Commit ada4b5f

Browse files
committed
Make download images logic
1 parent 3b3f587 commit ada4b5f

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

src/getData.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from 'axios'
2+
import path from 'path'
3+
4+
const getData = (url) => {
5+
const imgExtenstions = ['.png', '.jpg', '.jpeg', '.svg']
6+
if (imgExtenstions.includes(path.extname(url).toLowerCase())) {
7+
return axios.get(url, { responseType: 'arraybuffer' }).then(response => response.data)
8+
}
9+
return axios.get(url).then(response => response.data)
10+
}
11+
12+
export default getData

src/getLinks.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as cheerio from 'cheerio'
2+
3+
const getLinks = (html) => {
4+
const $ = cheerio.load(html)
5+
const imgTags = $('img')
6+
const links = imgTags.map((i, tag) => $(tag).attr('src')).get()
7+
return links
8+
}
9+
10+
export default getLinks

src/getPage.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
11
import path from 'path'
22
import fsp from 'fs/promises'
3-
import getPage from './getPage.js'
3+
import * as cheerio from 'cheerio'
4+
import getData from './getData.js'
5+
import getLinks from './getLinks.js'
6+
import * as prettier from 'prettier'
47

58
const getAbsolutePath = dirpath => path.resolve(process.cwd(), dirpath)
69

7-
const generateFileName = (url) => {
10+
const generateName = (url) => {
811
const href = new URL(url)
9-
return (href.hostname + href.pathname).replace(/[^a-zA-Z0-9]/g, '-') + '.html'
12+
return (href.hostname + href.pathname).replace(/[^a-zA-Z0-9]/g, '-')
1013
}
1114

1215
const downloadPage = (url, outputDir) => {
13-
const fileName = generateFileName(url)
16+
const fileName = generateName(url) + '.html'
1417
const filePath = path.join(getAbsolutePath(outputDir), fileName)
18+
const filesDirName = generateName(url) + '_files'
19+
const filesDirPath = path.join(getAbsolutePath(outputDir), filesDirName)
20+
let links
21+
let $
1522

16-
return getPage(url)
17-
.then(pageData => fsp.writeFile(filePath, pageData)
18-
.then(() => filePath))
23+
return getData(url)
24+
.then((html) => {
25+
$ = cheerio.load(html)
26+
links = getLinks(html)
27+
28+
if (links.length === 0) {
29+
return null
30+
}
31+
return fsp.mkdir(filesDirPath)
32+
})
33+
.then(() => {
34+
if (links.length === 0) {
35+
return null
36+
}
37+
return Promise.all(links.map((link) => {
38+
const absoluteLink = new URL(link, url).href
39+
const imageName = generateName(absoluteLink).replace(/-(?=[a-zA-Z0-9]+$)/, '.')
40+
const imagePath = path.join(filesDirPath, imageName)
41+
42+
return getData(absoluteLink)
43+
.then(imgData => fsp.writeFile(imagePath, imgData))
44+
.then(() => {
45+
$('img').each((i, tag) => {
46+
if ($(tag).attr('src') === link) {
47+
$(tag).attr('src', path.join(filesDirName, imageName))
48+
}
49+
})
50+
})
51+
}),
52+
)
53+
})
54+
.then(() => prettier.format($.html(), { parser: 'html' }))
55+
.then(formattedHtml => fsp.writeFile(filePath, formattedHtml))
56+
.then(() => filePath)
1957
.catch((err) => {
58+
// console.error('Full error:', err)
2059
throw new Error(`Error: ${err.message}`)
2160
})
2261
}

0 commit comments

Comments
 (0)