|
| 1 | +const puppeteer = require('puppeteer'); |
| 2 | +const axios = require('axios'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +async function exportJSON(projectName, sid) { |
| 6 | + const cookie = "connect.sid=" + sid; |
| 7 | + |
| 8 | + const meRes = await axios({ |
| 9 | + method: "get", |
| 10 | + url: "https://scrapbox.io/api/users/me", |
| 11 | + headers: { |
| 12 | + "Cookie" : cookie |
| 13 | + } |
| 14 | + }) |
| 15 | + const csrfToken = meRes.data.csrfToken; |
| 16 | + |
| 17 | + const exportRes = await axios({ |
| 18 | + method: "post", |
| 19 | + url: `https://scrapbox.io/api/page-data/export/${projectName}.json`, |
| 20 | + headers: { |
| 21 | + "Cookie" : cookie, |
| 22 | + "X-CSRF-TOKEN": csrfToken, |
| 23 | + } |
| 24 | + }) |
| 25 | + return exportRes.data |
| 26 | +} |
| 27 | + |
| 28 | +async function importJSON(projectName, fileName, sid) { |
| 29 | + const url = new URL(`https://scrapbox.io/projects/${projectName}/settings/page-data`); |
| 30 | + const browser = await puppeteer.launch({ |
| 31 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 32 | + }); |
| 33 | + const page = await browser.newPage(); |
| 34 | + |
| 35 | + await page.setCookie({name: 'connect.sid', value: sid, domain: 'scrapbox.io'}); |
| 36 | + await page.goto(url.toString()); |
| 37 | + await page.waitFor(2000); |
| 38 | + |
| 39 | + const inputUploadHandle = await page.$('input[name="import-file"]'); |
| 40 | + inputUploadHandle.uploadFile(fileName); |
| 41 | + await page.waitFor(4000); |
| 42 | + |
| 43 | + const overwriteCheckBox = await page.$('input[name="overwrite"]'); |
| 44 | + |
| 45 | + const importSubmitButton = await page.$x("//button[contains(., 'Import Pages')]"); |
| 46 | + await importSubmitButton[0].click(); |
| 47 | + |
| 48 | + await page.screenshot("./shot.png") |
| 49 | + await page.waitFor(60000); //転送量に応じて調整 |
| 50 | + |
| 51 | + await browser.close(); |
| 52 | +} |
| 53 | + |
| 54 | +(async() => { |
| 55 | + const sid = "YOUR_SID" |
| 56 | + const exportingProjectName = "SOURCE" //インポート元(本来はprivateプロジェクト) |
| 57 | + const importingProjectName = "DESTINATION" //インポート先(本来はpublicプロジェクト) |
| 58 | + |
| 59 | + const exportedJSON = await exportJSON(exportingProjectName, sid); |
| 60 | + exportedJSON.pages = exportedJSON.pages.filter((page) => { |
| 61 | + return (page.lines.filter(line => { |
| 62 | + return line.includes("[public.icon]") |
| 63 | + }).length != 0) |
| 64 | + }) |
| 65 | + fs.writeFileSync('/tmp/tmp.json', JSON.stringify(exportedJSON)); |
| 66 | + await importJSON(importingProjectName, "/tmp/tmp.json", sid); |
| 67 | +})(); |
0 commit comments