Parcourir la source

fixed update all bug

Marvin Zhang il y a 5 ans
Parent
commit
cc5707ac1d
12 fichiers modifiés avec 146 ajouts et 108 suppressions
  1. 0 3
      config.js
  2. 5 0
      config/config.ts
  3. 2 1
      constants.js
  4. 6 1
      data.js
  5. 1 1
      exec.js
  6. 1 1
      init.js
  7. 4 1
      spiders/base.js
  8. 96 97
      spiders/import/base.js
  9. 2 1
      src/constants.ts
  10. 1 0
      src/locales/zh-CN/menu.ts
  11. 13 2
      src/pages/Environment/EnvironmentList.tsx
  12. 15 0
      src/pages/Helper/Helper.tsx

+ 0 - 3
config.js

@@ -6,7 +6,4 @@ module.exports = {
     MONGO_DB: 'artipub',
     MONGO_USERNAME: '',
     MONGO_PASSWORD: '',
-
-    // HEADLESS: true
-    HEADLESS: false
 }

+ 5 - 0
config/config.ts

@@ -146,6 +146,11 @@ export default {
           component: './ArticleList/ArticleList',
         },
         {
+          path: '/helper',
+          name: 'helper',
+          icon: 'key',
+        },
+        {
           path: '/environments',
           name: 'environments',
           icon: 'setting',

+ 2 - 1
constants.js

@@ -22,7 +22,8 @@ module.exports = {
     RICH_TEXT: 'rich-text'
   },
   environment: {
-    updateStatsCron: 'update_stats_cron',
+    UPDATE_STATS_CRON: 'update_stats_cron',
+    ENABLE_CHROME_DEBUG: 'enable_chrome_debug',
   },
   cookieStatus: {
     NO_COOKIE: 'no_cookie',

+ 6 - 1
data.js

@@ -61,9 +61,14 @@ module.exports = {
   // 环境变量
   environments: [
     {
-      _id: constants.environment.updateStatsCron,
+      _id: constants.environment.UPDATE_STATS_CRON,
       label: '更新文章统计数据频率',
       value: '0 0/30 * * * *'
+    },
+    {
+      _id: constants.environment.ENABLE_CHROME_DEBUG,
+      label: 'Chrome浏览器调试模式',
+      value: 'Y'
     }
   ]
 }

+ 1 - 1
exec.js

@@ -43,7 +43,7 @@ class Runner {
     taskCronJob.start()
 
     // 获取环境变量
-    const updateStatsCron = await models.Environment.findOne({ _id: constants.environment.updateStatsCron })
+    const updateStatsCron = await models.Environment.findOne({ _id: constants.environment.UPDATE_STATS_CRON })
 
     // 数据统计执行器
     const statsLock = new AsyncLock()

+ 1 - 1
init.js

@@ -27,7 +27,7 @@ const init = async () => {
     const environment = data.environments[i]
     let environmentDb = await models.Environment.findOne({ _id: environment._id })
     if (!environmentDb) {
-      environmentDb = new models.Platform(platform)
+      environmentDb = new models.Environment(environment)
       await environmentDb.save()
     } else {
       // do nothing

+ 4 - 1
spiders/base.js

@@ -45,6 +45,9 @@ class BaseSpider {
       silent: false
     })
 
+    // 是否开启chrome浏览器调试
+    const enableChromeDebug = await models.Environment.findOne({_id: constants.environment.ENABLE_CHROME_DEBUG}).value
+
     // 浏览器
     this.browser = await this.pcr.puppeteer.launch({
       executablePath: this.pcr.executablePath,
@@ -55,7 +58,7 @@ class BaseSpider {
       // 打开开发者工具, 当此值为true时, headless总为false
       devtools: false,
       // 关闭headless模式, 不会打开浏览器
-      headless: globalConfig.HEADLESS
+      headless: enableChromeDebug !== 'Y'
     })
 
     // 页面

+ 96 - 97
spiders/import/base.js

@@ -11,110 +11,109 @@ showdown.setOption('tasklists', true)
 showdown.setFlavor('github')
 
 class BaseImportSpider extends BaseSpider {
-    constructor(platformName) {
-        super(BaseSpider)
-        if (!platformName) {
-            throw new Error('platformId must not be empty')
-        }
-        this.platformName = platformName
+  constructor(platformName) {
+    super(BaseSpider)
+    if (!platformName) {
+      throw new Error('platformId must not be empty')
     }
-
-    async init() {
-        // 平台
-        this.platform = await models.Platform.findOne({ name: this.platformName })
-
-        // PCR
-        this.pcr = await PCR({
-            revision: '',
-            detectionPath: '',
-            folderName: '.chromium-browser-snapshots',
-            hosts: ['https://storage.googleapis.com', 'https://npm.taobao.org/mirrors'],
-            retry: 3,
-            silent: false
-        })
-
-        // 浏览器
-        this.browser = await this.pcr.puppeteer.launch({
-            executablePath: this.pcr.executablePath,
-            timeout: 60000,
-            //如果是访问https页面 此属性会忽略https错误
-            ignoreHTTPSErrors: true,
-            devtools: false,
-            headless: globalConfig.HEADLESS,
-            args: [
-                '--no-sandbox',
-                '--disable-setuid-sandbox'
-            ]
-        })
-
-        // 页面
-        this.page = await this.browser.newPage()
-
-        // 设置 浏览器视窗
-        await this.page.setViewport({
-            width: 1300,
-            height: 938
-        })
-
-        // 配置
-        this.config = config[this.platform.name]
-        if (!config) {
-            throw new Error(`config (platform: ${this.platform.name}) cannot be found`)
-        }
-
-        // 编辑器选择器
-        this.editorSel = this.config.editorSel
-
-        // showdown配置
-        showdown.setOption('tables', true)
-        showdown.setOption('tasklists', true)
-        showdown.setFlavor('github')
-
-        // markdown to html转换器
-        this.converter = new showdown.Converter()
-    }
-
-    async fetchArticles() {
-        // to be overridden
+    this.platformName = platformName
+  }
+
+  async init() {
+    // 平台
+    this.platform = await models.Platform.findOne({ name: this.platformName })
+
+    // PCR
+    this.pcr = await PCR({
+      revision: '',
+      detectionPath: '',
+      folderName: '.chromium-browser-snapshots',
+      hosts: ['https://storage.googleapis.com', 'https://npm.taobao.org/mirrors'],
+      retry: 3,
+      silent: false
+    })
+
+    // 是否开启chrome浏览器调试
+    const enableChromeDebug = await models.Environment.findOne({ _id: constants.environment.ENABLE_CHROME_DEBUG }).value
+
+    // 浏览器
+    this.browser = await this.pcr.puppeteer.launch({
+      executablePath: this.pcr.executablePath,
+      timeout: 60000,
+      //如果是访问https页面 此属性会忽略https错误
+      ignoreHTTPSErrors: true,
+      devtools: false,
+      headless: enableChromeDebug !== 'Y',
+    })
+
+    // 页面
+    this.page = await this.browser.newPage()
+
+    // 设置 浏览器视窗
+    await this.page.setViewport({
+      width: 1300,
+      height: 938
+    })
+
+    // 配置
+    this.config = config[this.platform.name]
+    if (!config) {
+      throw new Error(`config (platform: ${this.platform.name}) cannot be found`)
     }
 
-    async fetch() {
-        logger.info('fetching articles')
-
-        await this.init()
-        await this.setCookies()
-        try {
-            await this.page.goto(this.platform.url, { timeout: 60000 })
-        } catch (e) {
-            console.error(e)
-            await this.browser.close()
-            return []
-        }
-        await this.page.waitFor(5000)
-        const articles = await this.fetchArticles()
-        await this.browser.close()
-        return articles
+    // 编辑器选择器
+    this.editorSel = this.config.editorSel
+
+    // showdown配置
+    showdown.setOption('tables', true)
+    showdown.setOption('tasklists', true)
+    showdown.setFlavor('github')
+
+    // markdown to html转换器
+    this.converter = new showdown.Converter()
+  }
+
+  async fetchArticles() {
+    // to be overridden
+  }
+
+  async fetch() {
+    logger.info('fetching articles')
+
+    await this.init()
+    await this.setCookies()
+    try {
+      await this.page.goto(this.platform.url, { timeout: 60000 })
+    } catch (e) {
+      console.error(e)
+      await this.browser.close()
+      return []
     }
-
-    async importArticle(siteArticle) {
-        // to be overridden
+    await this.page.waitFor(5000)
+    const articles = await this.fetchArticles()
+    await this.browser.close()
+    return articles
+  }
+
+  async importArticle(siteArticle) {
+    // to be overridden
+  }
+
+  async import(siteArticles) {
+    logger.info('importing articles')
+
+    await this.init()
+    await this.setCookies()
+    for (let i = 0; i < siteArticles.length; i++) {
+      const siteArticle = siteArticles[i]
+      if (siteArticle.exists && siteArticle.associated) continue
+      await this.importArticle(siteArticle)
     }
 
-    async import(siteArticles) {
-        logger.info('importing articles')
+    await this.browser.close()
 
-        await this.init()
-        await this.setCookies()
-        for (let i = 0; i < siteArticles.length; i++) {
-            const siteArticle = siteArticles[i]
-            if (siteArticle.exists && siteArticle.associated) continue
-            await this.importArticle(siteArticle)
-        }
-
-        await this.browser.close()
-
-        logger.info('imported articles')
-    }
+    logger.info('imported articles')
+  }
 }
 
 module.exports = BaseImportSpider

+ 2 - 1
src/constants.ts

@@ -25,7 +25,8 @@ export default {
     RICH_TEXT: 'rich-text',
   },
   environment: {
-    updateStatsCron: 'update_stats_cron',
+    UPDATE_STATS_CRON: 'update_stats_cron',
+    ENABLE_CHROME_DEBUG: 'enable_chrome_debug',
   },
   cookieStatus: {
     NO_COOKIE: 'no_cookie',

+ 1 - 0
src/locales/zh-CN/menu.ts

@@ -5,6 +5,7 @@ export default {
   'menu.article-edit': '文章编辑',
   'menu.platforms': '平台管理',
   'menu.environments': '系统设置',
+  'menu.helper': '登陆助手',
   'menu.more-blocks': '更多区块',
   'menu.home': '首页',
   'menu.login': '登录',

+ 13 - 2
src/pages/Environment/EnvironmentList.tsx

@@ -16,7 +16,7 @@ const EnvironmentList: React.FC<EnvironmentListProps> = props => {
   const {dispatch, environment} = props;
 
   const getValue = (d: Environment) => {
-    if (d._id === constants.environment.updateStatsCron) {
+    if (d._id === constants.environment.UPDATE_STATS_CRON) {
       return (
         <Select
           value={d.value}
@@ -32,6 +32,17 @@ const EnvironmentList: React.FC<EnvironmentListProps> = props => {
           <Select.Option value="0 0 0 * * *">每天</Select.Option>
         </Select>
       )
+    } else if (d._id === constants.environment.ENABLE_CHROME_DEBUG) {
+      return (
+        <Select
+          value={d.value}
+          style={{width: '200px'}}
+          onChange={onFieldChange(d)}
+        >
+          <Select.Option value="Y">开启</Select.Option>
+          <Select.Option value="N">关闭</Select.Option>
+        </Select>
+      );
     } else {
       return d.value
     }
@@ -64,7 +75,7 @@ const EnvironmentList: React.FC<EnvironmentListProps> = props => {
       });
       const environments = environment.environments ? environment.environments.map((_d: Environment) => {
         if (_d._id === d._id) _d.value = d.value;
-        return d;
+        return _d;
       }) : [];
       dispatch({
         type: 'environment/saveEnvironmentList',

+ 15 - 0
src/pages/Helper/Helper.tsx

@@ -0,0 +1,15 @@
+import React from 'react';
+import {PageHeaderWrapper} from '@ant-design/pro-layout';
+import {Table} from "antd";
+
+const Helper: React.FC<any> = () => {
+
+  return (
+    <PageHeaderWrapper>
+      <Table>
+      </Table>
+    </PageHeaderWrapper>
+  );
+};
+
+export default Helper;