Skip to content

Commit

Permalink
add electron
Browse files Browse the repository at this point in the history
  • Loading branch information
junyiz committed Jun 3, 2022
1 parent cafd84c commit da48c6f
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.DS_store
blog-*
node_modules

out
dist
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# dature
基于 Node.js 的新浪博客抓取程序,抓取数据包含标题、正文、时间、分类、标签、图片,并生成 HTML 文件
基于 Node.js 的新浪博客备份程序,备份数据包含标题、正文、时间、分类、标签、图片,并生成 HTML 文件

## 前置
需要先安装 Node.js,参见:[https://nodejs.org/zh-cn/download/](https://nodejs.org/zh-cn/download/)
Expand All @@ -13,25 +13,25 @@ npm install -g dature
## 使用

```bash
# 抓取牛根生的博客
# 备份牛根生的博客
dature -u 1263917762 -c "xxxxxx"

# xxxxxx 为 cookie
```
## 抓取后生成的新博客
![example](https://raw.githubusercontent.com/junyiz/dature/master/images/example.jpg)
## 备份后生成的新博客
![example](https://gitee.com/junyiz/dature/raw/master/images/example.jpg)

## 如何查看新浪博客的 UID

![example](https://raw.githubusercontent.com/junyiz/dature/master/images/sina.jpg)
![example](https://gitee.com/junyiz/dature/raw/master/images/sina.jpg)

## 如何查看新浪博客的 cookie

![cookie](https://raw.githubusercontent.com/junyiz/dature/master/images/cookie.png)
登录新浪博客,打开浏览器的开发者工具,然后刷新页面,如下大红框,即是 cookie
![cookie](https://gitee.com/junyiz/dature/raw/master/images/cookie.png)

注意只需复制大红框中冒号后面的部分

## buy-me-a-tea
如果这个仓库对你有帮助,欢迎 star 或 buy me a tea:

<img src="https://raw.githubusercontent.com/junyiz/dature/master/images/wechat.jpg" width="300" />
<img src="https://gitee.com/junyiz/dature/raw/master/images/wechat.jpg" width="300" />
Binary file added assets/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (argv.uid) {
const cookie = (argv.cookie || '')
.replace(/(NowDate|BLOG_TITLE|mblog_userinfo)[^;]*;/g, '')
fetch(dir, argv.uid, cookie).then(function() {
console.info(`\n抓取完毕, 博客存储目录:${dir}\n`)
console.info(`\n备份完毕, 博客存储目录:${dir}\n`)
})
} else {
yargs.showHelp()
Expand Down
76 changes: 76 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dature:新浪博客备份工具</title>
<style>
body {
background-color: #484d5d;
background-image: url(assets/bg.png);
color: azure;
}
#app {
padding: 20px 0;
margin-left: auto;
margin-right: auto;
width: 420px;
}
label {
width: 70px;
display: inline-block;
}
.userid {
display: flex;
margin-bottom: 20px;
}
input[name=uid] {
flex: 1;
height: 30px;
line-height: 30px;
border-radius: 3px;
}
.cookie {
display: flex;
margin-bottom: 20px;
}
textarea[name=cookie] {
flex: 1;
height: 120px;
border-radius: 3px;
}
.button {
text-align: center;
margin-bottom: 20px;
}
.button a {
cursor: pointer;
display: inline-block;
width: 80px;
line-height: 30px;
border-radius: 3px;
border: 1px dashed #e2e2e2;
}
.help {
font-size: 14px;
padding-left: 70px;
margin-bottom: 20px;
}
.output {
font-size: 12px;
overflow-y: auto;
}
a {
color: darkseagreen;
}
</style>
</head>
<body>
<div id="app">
<div class="userid"><label for="uid">UID:</label> <input name="uid" id="uid" /></div>
<div class="cookie"><label for="cookie">Cookie:</label><textarea name="cookie" id="cookie"></textarea></div>
<div class="help"><a href="https://gitee.com/junyiz/dature#%E5%A6%82%E4%BD%95%E6%9F%A5%E7%9C%8B%E6%96%B0%E6%B5%AA%E5%8D%9A%E5%AE%A2%E7%9A%84-uid">点击查看如何获取是 UID 和 Cookie?</a></div>
<div class="button"><a id="btn">确定</a></div>
<div class="output" id="out"></div>
</div>
</body>
</html>
6 changes: 3 additions & 3 deletions lib/sina.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ module.exports = async function extract(uid, dir, cookie) {
let imgs = []
let data = { uid, post: [] }

console.log('抓取博文目录:\n')
console.log('备份博客文章目录:\n')

// 遍历博文目录,抽取博客标题、博文地址
let i = 1, page = 1
do {
let list = await htmlDownloader(`http://blog.sina.com.cn/s/articlelist_${uid}_0_${i}.html`, { cookie }).catch(console.error)
let list = await htmlDownloader(`http://blog.sina.com.cn/s/articlelist_${uid}_0_${i}.html`, { cookie }).catch(console.log)
let $ = cheerio.load(list)
console.log(`第 ${i} 页:`)
$('.articleList .atc_main a[title]').each((i, el) => {
Expand All @@ -34,7 +34,7 @@ module.exports = async function extract(uid, dir, cookie) {
}
} while (++i <= page)

console.log(`\n博客${data.title})共有博文 ${urls.length} 篇, 以下开始按篇抓取:\n`)
console.log(`\n博客:${data.title || ''}, 共有文章 ${urls.length} 篇, 以下开始按篇备份:\n`)

let n = 1
// 遍历博文地址,抽取博文内容(标题、正文、时间、分类、图片、原文链接)
Expand Down
18 changes: 18 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { app, BrowserWindow } = require('electron')
const path = require('path')

const createWindow = () => {
const win = new BrowserWindow({
width: 528,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})

win.loadFile('index.html')
}

app.whenReady().then(() => {
createWindow()
})
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "dature",
"version": "0.3.6",
"description": "新浪博客抓取程序",
"main": "cli.js",
"description": "新浪博客备份工具",
"main": "main.js",
"bin": {
"dature": "cli.js"
},
"dependencies": {
"cheerio": "^0.22.0",
"iconv-lite": "^0.4.21",
"is-url": "^1.2.2",
"mkdirp": "^0.5.1",
"yargs": "^11.0.0",
"iconv-lite": "^0.4.21"
"yargs": "^11.0.0"
},
"devDependencies": {
"electron": "^19.0.2",
"electron-builder": "^23.0.3",
"pkg": "^5.6.0"
},
"repository": {
Expand All @@ -25,9 +27,11 @@
},
"homepage": "https://github.com/junyiz/dature#readme",
"scripts": {
"start": "electron .",
"build": "pkg .",
"build:macos": "pkg . -t node12-macos-x64 -o ./dist/dature-macos -d",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"dist": "electron-builder --win --x64"
},
"keywords": [
"data",
Expand All @@ -50,6 +54,15 @@
],
"outputPath": "dist"
},
"build": {
"appId": "cn.dature.app",
"mac": {
"target": ["dmg", "zip"]
},
"win": {
"target": ["nsis", "zip"]
}
},
"author": "Junyi Zhang <junyime@qq.com>",
"license": "ISC"
}
31 changes: 31 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fetch = require('./lib/fetch')
const join = require('path').join
const { shell } = require('electron')

const $ = (selector) => document.getElementById(selector)

window.addEventListener('DOMContentLoaded', () => {
const btn = $('btn')
const out = $('out')

btn.addEventListener('click', () => {
const uid = $('uid').value
const cookie = ($('cookie').value || '')
.replace(/(NowDate|BLOG_TITLE|mblog_userinfo)[^;]*;/g, '')

const dir = join(process.cwd(), `./blog-${uid}`)

console.log = (log) => out.innerHTML += log.replace('\n', '<br>')

fetch(dir, uid, cookie).then(function() {
console.log(`\n备份完毕, 博客存储目录:<a href="${dir}/index.html">${dir}</a>\n`)
})
})

out.addEventListener('click', (event) => {
if (event.target.tagName === 'A') {
event.preventDefault()
shell.openExternal(event.target.href)
}
})
})

0 comments on commit da48c6f

Please sign in to comment.