From d781d2c7313fab16944fe0c6f882f1cdc1e6f6bf Mon Sep 17 00:00:00 2001 From: mtane0412 Date: Tue, 12 Jan 2021 20:37:05 +0900 Subject: [PATCH] =?UTF-8?q?contentful=E3=81=AE=E3=82=A8=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=83=AA=E3=81=8B=E3=82=89utc=20offset=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=99=E3=82=8B=20#5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contentful/remove-timezone.js | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 contentful/remove-timezone.js diff --git a/contentful/remove-timezone.js b/contentful/remove-timezone.js new file mode 100644 index 0000000..8e45506 --- /dev/null +++ b/contentful/remove-timezone.js @@ -0,0 +1,52 @@ +require("dotenv").config({ path: `.env.development` }); +const env = process.env; + +process.on("unhandledRejection", console.dir); + +const contentful = require("contentful-management"); +const client = contentful.createClient({ + accessToken: env.CONTENTFUL_PERSONAL_ACCESS_TOKEN, +}); + +const convertNoUtcOffset = async (entries) => { + for (let item of entries.items) { + try { + if (item.fields.publishDate.ja.split('+')[1]) { + const convertedTime = await item.fields.publishDate.ja.split('+')[0]; + console.log(`converted: ${item.fields.publishDate.ja} --> ${convertedTime}`); + item.fields.publishDate.ja = convertedTime; + const updatedEntry = await item.update(); + await updatedEntry.publish(); + } + } catch (err) { + console.log('skipped for some reason'); + } + }; +} + +const checkPublishDate = (entries) => { + for (let item of entries.items) { + try { + if (item.fields.publishDate.ja.split('+')[1]) { + console.log(`warning: ${item.fields.title.ja}`); + console.log(item.fields.publishDate.ja); + } else { + console.log(item.fields.publishDate.ja) + } + } catch (err) { + throw err; + } + } +} + +(async () => { + const space = await client.getSpace(env.CONTENTFUL_SPACE_ID); + const environment = await space.getEnvironment("master"); + const entries = await environment.getEntries({ + content_type: "blogPost", + limit: 200, + order: "sys.createdAt", + }); + convertNoUtcOffset(entries); + checkPublishDate(entries); +})(); \ No newline at end of file