Skip to content

Commit

Permalink
Merge pull request #138 from tanliyuan/merge/0224
Browse files Browse the repository at this point in the history
Merge/0224
  • Loading branch information
tanliyuan authored Feb 24, 2021
2 parents a06c458 + 0a28581 commit 2af709e
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 156 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
FROM jelastic/nodejs:8.17.0-npm AS frontend
FROM jelastic/nodejs:15.9.0-npm AS frontend

WORKDIR /app
ADD . /app
RUN npm install
RUN npm run build

FROM jelastic/nodejs:8.17.0-npm
FROM jelastic/nodejs:15.9.0-npm
RUN yum install -y nginx && yum clean all
COPY --from=frontend /app/dist /frontend
WORKDIR /app
ADD . /app
RUN cp ./backend/package.json . && \
cp /app/nginx/artipub.conf /etc/nginx/conf.d
cp /app/nginx/artipub.conf /etc/nginx/conf.d/artipub.conf
RUN npm install

EXPOSE 3000 8000
CMD /app/docker_init.sh

28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ArtiPub 目前支持文章编辑、文章发布、数据统计的功能,后期
#### NPM 或源码安装

- MongoDB: 3.6+
- NodeJS: 8.12+
- NodeJS: 10+

## 安装方式

Expand All @@ -71,18 +71,22 @@ ArtiPub 提供 3 种安装方式如下。

### 通过 Docker 安装

通过 Docker,可以免去安装 MongoDB 的步骤,也是我们最推荐的安装方式。使用 Docker 安装 ArtiPub 前,请确保您安装了 Docker 以及 Docker Compose。
通过 Docker,可以免去安装 MongoDB 的步骤,也是我们最推荐的安装方式。使用 Docker 安装 ArtiPub 前,请确保您安装了 Docker 以及 Docker Compose。docker运行 ArtiPub 有两种方式。

在您的项目目录下创建 `docker-compose.yaml` 文件,输入如下内容。
- 通过 docker-compose.yaml 启动

适用于你本地之前没有运行 `mongodb` 容器。 在您的项目目录下创建 `docker-compose.yaml` 文件,输入如下内容。

```yaml
version: '3.3'
version: "3.3"
services:
app:
image: "tikazyq/artipub:latest"
image: "tanliyuan123/artipub:1.0"
environment:
MONGO_HOST: "mongo"
ARTIPUB_API_ADDRESS: "http://localhost:3000" # 后端 API 地址,如果安装地址不在本机,请修改为协议 + 服务器 IP 地址 + 端口号(默认为 3000)
# MONGO_USERNAME: root
# MONGO_PASSWORD: example
ARTIPUB_API_ADDRESS: "http://localhost:3000" # 后端API地址,如果安装地址不在本机,请修改为协议+服务器IP地址+端口号(默认为3000)
ports:
- "8000:8000" # frontend
- "3000:3000" # backend
Expand All @@ -91,11 +95,13 @@ services:
mongo:
image: mongo:latest
restart: always
#volumes:
# - "E:\\mongodb:/data/db"
ports:
- "27017:27017"
```
然后在命令行中输入如下命令。
然后在命令行中输入如下命令。如果你想再次启动容器时上次内容不会被销毁,去掉 `volumes` 两行的注释,改成自己本地路径即可。

```bash
docker-compose up
Expand All @@ -105,6 +111,14 @@ docker-compose up

注意⚠️,如果您的 Docker 宿主机不是本机,例如您用了 Docker Machine 或者 Docker 服务在其他机器上,您需要将环境变量 `ARTIPUB_API_ADDRESS` 改为宿主机 IP + 端口号(默认 3000)。然后,在浏览器输入 `http://< 宿主机 IP>:8000` 即可看到界面。

- 独立启动 artipub 镜像

如果你本地已有启动的mongodb容器,不想用上面方式再起一个。其中 `goofy_ganguly` 为本地已启动的 mongodb 容器名, 替换成你本地的即可。

```bash
docker run --rm -it --link goofy_ganguly -p 3000:3000/tcp -p 8000:8000/tcp tanliyuan123/artipub:1.0
```

### 通过 npm 包安装

如果您对 npm 熟悉,且已经有 MongoDB 的环境,这是最为快捷的方式。
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"log4js": "^5.1.0",
"mongoose": "^5.6.12",
"morgan": "^1.9.1",
"puppeteer-chromium-resolver": "^2.0.1",
"puppeteer-chromium-resolver": "^5.2.0",
"cheerio": "^1.0.0-rc.3",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
Expand Down
11 changes: 9 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
//docker --link 连接独立部署的mongo容器时使用
const mongodbContainerAddr = Object.entries(process.env).find((entry) => entry[0].endsWith('_TCP_ADDR'));
const addr = mongodbContainerAddr ? mongodbContainerAddr[1] : '127.0.0.1';

const mongodbContainerPort = Object.entries(process.env).find((entry) => entry[0].endsWith('_TCP_PORT'));
const port = mongodbContainerPort ? mongodbContainerPort[1] : '27017';

module.exports = {
HOST: '0.0.0.0',
PORT: 3000,
MONGO_HOST: process.env.MONGO_HOST ? process.env.MONGO_HOST : 'localhost',
MONGO_PORT: process.env.MONGO_PORT ? process.env.MONGO_PORT : '27017',
MONGO_HOST: process.env.MONGO_HOST ? process.env.MONGO_HOST : addr,
MONGO_PORT: process.env.MONGO_PORT ? process.env.MONGO_PORT : port,
MONGO_DB: process.env.MONGO_DB ? process.env.MONGO_DB : 'artipub',
MONGO_USERNAME: process.env.MONGO_USERNAME ? process.env.MONGO_USERNAME : '',
MONGO_PASSWORD: process.env.MONGO_PASSWORD ? process.env.MONGO_PASSWORD : '',
Expand Down
7 changes: 3 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.3"
services:
app:
image: "tikazyq/artipub:latest"
image: "tanliyuan123/artipub:1.0"
environment:
MONGO_HOST: "mongo"
# MONGO_USERNAME: root
Expand All @@ -15,8 +15,7 @@ services:
mongo:
image: mongo:latest
restart: always
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: example
#volumes:
# - "E:\\mongodb:/data/db"
ports:
- "27017:27017"
2 changes: 1 addition & 1 deletion docker_init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/bash

# replace original url to new one
if [ "${ARTIPUB_API_ADDRESS}" = "" ];
Expand Down
4 changes: 2 additions & 2 deletions exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const StatsFetcher = require('./lib/StatsFetcher')
// mongodb连接
mongoose.Promise = global.Promise
if (config.MONGO_USERNAME) {
mongoose.connect(`mongodb://${config.MONGO_USERNAME}:${config.MONGO_PASSWORD}@${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}?authSource=${config.MONGO_AUTH_DB}`, { useNewUrlParser: true })
mongoose.connect(`mongodb://${config.MONGO_USERNAME}:${config.MONGO_PASSWORD}@${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}?authSource=${config.MONGO_AUTH_DB}`, { useNewUrlParser: true , useUnifiedTopology: true})
} else {
mongoose.connect(`mongodb://${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}`, { useNewUrlParser: true })
mongoose.connect(`mongodb://${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}`, { useNewUrlParser: true, useUnifiedTopology: true })
}

class Runner {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"eslint": "^5.16.0",
"express": "^4.17.1",
"gh-pages": "^2.0.1",
"husky": "^3.0.0",
"import-sort-cli": "^6.0.0",
"import-sort-parser-babylon": "^6.0.0",
"import-sort-parser-typescript": "^6.0.0",
Expand All @@ -118,6 +117,7 @@
"serverless-http": "^2.0.2",
"slash2": "^2.0.0",
"stylelint": "^10.1.0",
"typescript": "^4.1.5",
"umi-plugin-ga": "^1.1.3",
"umi-plugin-pro": "^1.0.2",
"umi-types": "^0.3.8",
Expand Down
8 changes: 6 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ console.log(process.env)
mongoose.Promise = global.Promise
if (config.MONGO_USERNAME) {
const mongoUrl = `mongodb://${config.MONGO_USERNAME}:${config.MONGO_PASSWORD}@${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}?authSource=${config.MONGO_AUTH_DB}`
mongoose.connect(`mongodb://${config.MONGO_USERNAME}:${config.MONGO_PASSWORD}@${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}?authSource=${config.MONGO_AUTH_DB}`, { useNewUrlParser: true })
mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }
);
} else {
mongoose.connect(`mongodb://${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}`, { useNewUrlParser: true })
mongoose.connect(
`mongodb://${config.MONGO_HOST}:${config.MONGO_PORT}/${config.MONGO_DB}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
);
}

// bodyParser中间件
Expand Down
2 changes: 1 addition & 1 deletion spiders/baijiahao.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BaiJiaHaoSpider extends BaseSpider {
if (codeClose) {
const boundingBox = await codeClose.boundingBox();

//移动鼠标去关闭验证弹框
//移动鼠标去关闭验证弹框, 估计关闭事件绑定在伪元素上,用了坐标判断,元素click方法没法触发
await this.page.mouse.move(boundingBox.x, boundingBox.y, {
steps: 20
});
Expand Down
50 changes: 27 additions & 23 deletions spiders/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ class BaseSpider {
// 打开开发者工具, 当此值为true时, headless总为false
devtools: false,
// 关闭headless模式, 不会打开浏览器
headless: enableChromeDebug !== 'Y',
args: [
'--no-sandbox',
],
headless: enableChromeDebug !== "Y",
args: ["--no-sandbox", '--start-maximized'],
defaultViewport: null
});

// 页面
Expand All @@ -76,20 +75,12 @@ class BaseSpider {
};

// 配置
this.config = config[this.platform.name];
if (!config) {
const platformConfig = config[this.platform.name];
if (!platformConfig) {
throw new Error(`config (platform: ${this.platform.name}) cannot be found`);
}

// URL信息
this.urls = this.config.urls;

// 登陆选择器
this.loginSel = this.config.loginSel;

// 编辑器选择器
this.editorSel = this.config.editorSel;

Object.assign(this, platformConfig);

// 脚注内容
this.footerContent = {
Expand Down Expand Up @@ -212,8 +203,13 @@ class BaseSpider {
*/
async goToEditor() {
logger.info(`navigating to ${this.urls.editor}`);
await this.page.goto(this.urls.editor);
await this.page.waitFor(5000);
await Promise.all([
this.page.goto(this.urls.editor),
this.page.waitForNavigation({
waitUntil: ['load', 'domcontentloaded', 'networkidle2']
})
]);

await this.afterGoToEditor();
}

Expand Down Expand Up @@ -243,7 +239,7 @@ class BaseSpider {
const el = document.querySelector(editorSel.content);
el.focus();
try {
HTMLPreElement.prototype.select = function() {
HTMLPreElement.prototype.select = function () {
let range = document.createRange();
range.selectNodeContents(this);

Expand Down Expand Up @@ -277,7 +273,6 @@ class BaseSpider {
logger.info(`input editor title`);
// 输入标题
await this.page.evaluate(this.inputTitle, this.article, this.editorSel, this.task);
await this.page.waitFor(3000);

// 输入内容
logger.info(`input editor content`);
Expand All @@ -288,8 +283,6 @@ class BaseSpider {
await this.page.evaluate(this.inputFooter, this.article, this.editorSel);
await this.page.waitFor(3000);

await this.page.waitFor(10000);

// 后续处理
await this.afterInputEditor();
}
Expand All @@ -309,7 +302,14 @@ class BaseSpider {
logger.info(`publishing article`);
// 发布文章
const elPub = await this.page.$(this.editorSel.publish);
await elPub.click();

//发布后地址会变更用waitForNavigation,不会变更用固定时间,尽量减少等待时间
await Promise.all([
this.page.$eval(this.editorSel.publish, submit => submit.click()),
this.publishNavigationChange
? this.page.waitForNavigation()
: this.page.waitForTimeout(1500)
]);

// 后续处理
await this.afterPublish();
Expand Down Expand Up @@ -434,7 +434,7 @@ class BaseSpider {
console.log(url);
let text = res.data;
if (this.platform.name === constants.platform.TOUTIAO) {
this.platform.loggedIn = text.includes('userName');
this.platform.loggedIn = !text.includes('login-button');
} else if (this.platform.name === constants.platform.CSDN) {
text = text.message
this.platform.loggedIn = text.includes('成功');
Expand All @@ -459,6 +459,10 @@ class BaseSpider {
}
console.log(this.platform.loggedIn);
await this.platform.save();
})
.catch(error => {
console.error(`${url} 登录态校验异常`);
console.error(error);
});

}
Expand Down
21 changes: 19 additions & 2 deletions spiders/cnblogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@ class CnblogsSpider extends BaseSpider {
// iframeWindow.document.execCommand('insertHTML', false, content)
// }

async inputFooter(article, editorSel) {
// do nothing
async afterGoToEditor() {
const isMarkdownEditor = await this.page.evaluate(() => {
return document.querySelector('#editor-switcher').innerText.includes('markdown');
});

//切换到markdown编辑器
if (!isMarkdownEditor) {
await this.page.click('#editor-switcher');
await this.page.click('#dropdown-menu > button:nth-child(2)');
}

//推荐到首页候选区,需要满足字数
// await this.page.click('#site-publish-candidate');

}

async afterInputEditor() {
//点击预览,触发编辑器事件,不然保存时取不到文本域的值
await this.page.click('.tab-bar li:nth-child(2)');
}

async afterPublish() {
Expand Down
Loading

0 comments on commit 2af709e

Please sign in to comment.