首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >puppeteer实现批量下载coding仓库代码

puppeteer实现批量下载coding仓库代码

作者头像
薛定喵君
发布2022-05-18 09:19:25
6070
发布2022-05-18 09:19:25
举报
文章被收录于专栏:薛定喵君薛定喵君薛定喵君

记录下批量下载仓库代码的方法

工具来由

需要把指定帐号下的所有仓库批量下载下来,一个个复制仓库地址进行克隆太忙,所以做了这个自动化工具,如果你有类似需求,可供参考。

操作准备

本地需要安装 git,生成私钥后添加到指定帐号的 SSH 公钥中。

代码

核心代码参考:

const puppeteer = require('puppeteer');
const account = '帐号';
const password = '密码';
const localPath = '本地存放代码的目录';
const { exec, execSync } = require('child_process');

(async () => {
  const browser = await puppeteer.launch({
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-blink-features=AutomationControlled',
    ],
    dumpio: false,
  });
  const page = await browser.newPage();

  // webdriver
  await page.evaluateOnNewDocument(() => {
    const newProto = navigator.__proto__;
    delete newProto.webdriver; //删除 navigator.webdriver字段
    navigator.__proto__ = newProto;
  });

  // 添加 window.chrome字段,向内部填充一些值
  await page.evaluateOnNewDocument(() => {
    window.chrome = {};
    window.chrome.app = {
      InstallState: 'hehe',
      RunningState: 'haha',
      getDetails: 'xixi',
      getIsInstalled: 'ohno',
    };
    window.chrome.csi = function() {};
    window.chrome.loadTimes = function() {};
    window.chrome.runtime = function() {};
  });

  // userAgent设置
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'userAgent', {
      //userAgent在无头模式下有headless字样,所以需覆盖
      get: () =>
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
    });
  });

  // plugins设置
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'plugins', {
      //伪装真实的插件信息
      get: () => [
        {
          0: {
            type: 'application/x-google-chrome-pdf',
            suffixes: 'pdf',
            description: 'Portable Document Format',
            enabledPlugin: Plugin,
          },
          description: 'Portable Document Format',
          filename: 'internal-pdf-viewer',
          length: 1,
          name: 'Chrome PDF Plugin',
        },
        {
          0: {
            type: 'application/pdf',
            suffixes: 'pdf',
            description: '',
            enabledPlugin: Plugin,
          },
          description: '',
          filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai',
          length: 1,
          name: 'Chrome PDF Viewer',
        },
        {
          0: {
            type: 'application/x-nacl',
            suffixes: '',
            description: 'Native Client Executable',
            enabledPlugin: Plugin,
          },
          1: {
            type: 'application/x-pnacl',
            suffixes: '',
            description: 'Portable Native Client Executable',
            enabledPlugin: Plugin,
          },
          description: '',
          filename: 'internal-nacl-plugin',
          length: 2,
          name: 'Native Client',
        },
      ],
    });
  });

  // languages设置
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'languages', {
      //添加语言
      get: () => ['zh-CN', 'zh', 'en'],
    });
  });

  // permissions设置
  await page.evaluateOnNewDocument(() => {
    const originalQuery = window.navigator.permissions.query; //notification伪装
    window.navigator.permissions.query = (parameters) =>
      parameters.name === 'notifications'
        ? Promise.resolve({ state: Notification.permission })
        : originalQuery(parameters);
  });

  // WebGL设置
  await page.evaluateOnNewDocument(() => {
    const getParameter = WebGLRenderingContext.getParameter;
    WebGLRenderingContext.prototype.getParameter = function(parameter) {
      // UNMASKED_VENDOR_WEBGL
      if (parameter === 37445) {
        return 'Intel Inc.';
      }
      // UNMASKED_RENDERER_WEBGL
      if (parameter === 37446) {
        return 'Intel(R) Iris(TM) Graphics 6100';
      }
      return getParameter(parameter);
    };
  });

  await page.goto('https://xxx.coding.net/login', {
    waitUntil: 'networkidle0',
  });

  await page.type('input[name="account"]', account);
  await page.type('input[name="password"]', password);
  console.log('登录...');
  await page.click('.cuk2-button-type-confirm');
  await page.waitForNavigation({
    waitUntil: 'networkidle0',
  });

  page.on('response', async (response) => {
    const request = response.request();
    if (request.url().includes('xxx/depot-group/depots')) {
      console.log('获取所有仓库地址');
      const json = await response.text();
      let repos = JSON.parse(json).data;
      // 下载到本地
      repos.forEach((r) => {
        console.log('下载:' + r.gitSshUrl);
        execSync(`cd ${localPath} && git clone ${r.gitSshUrl}`);
      });

      // await browser.close();
    }
  });

  console.log('跳转仓库列表页');
  await page.goto('https://jnpfsoft.coding.net/p/xxx/repos', {
    waitUntil: 'networkidle2',
  });
})();

参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 工具来由
  • 操作准备
  • 代码
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档