Skip to content

Commit

Permalink
add uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
junyiz authored May 30, 2022
1 parent 63518d3 commit cafd84c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/sina.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const urlParse = require('url').parse
const path = require('path')
const cheerio = require('cheerio')
const isUrl = require('is-url')
const uuid = require('./uuid')
const htmlDownloader = require('./html-downloader')
const pictureDownloader = require('./picture-downloader')

Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = async function extract(uid, dir, cookie) {
// 下载正文中的图片,并替换为本地图片地址
.replace(/<img[^>]*real_src="([^"]*)"([^>]*)>/gi, (m, url, b) => {
url = url.replace(/&amp;/gi, '&'); // 图片地址
let name = path.parse(urlParse(url).pathname).base // 图片名称
let name = path.parse(urlParse(url).pathname).base || uuid().replace(/-/g, '').toUpperCase() // 图片名称
// 只下载地址以http开头的图片
if (url.indexOf('http') == 0 && isUrl(url)) {
if (!imgs.find(it => it.url === url)) {
Expand Down
9 changes: 9 additions & 0 deletions lib/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 生成 uuid(v4)
*/
module.exports = function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dature",
"version": "0.3.5",
"version": "0.3.6",
"description": "新浪博客抓取程序",
"main": "cli.js",
"bin": {
Expand Down

0 comments on commit cafd84c

Please sign in to comment.