This repository has been archived on 2024-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
anquet-migrator/update.js

126 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-04-21 20:49:05 +03:00
import { Directus } from '@directus/sdk';
import TurndownService from 'turndown';
import fs from 'fs';
import he from 'he';
import * as cheerio from 'cheerio';
import slug from 'slug';
import Redis from 'ioredis';
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config()
const directus = new Directus(process.env.DIRECTUS_HOST);
const turndownService = new TurndownService()
const htmlString = fs.readFileSync('Solutions.xml').toString();
const $ = cheerio.load(htmlString);
const redis = new Redis();
2023-04-21 22:00:00 +03:00
await directus.auth.login({ email: process.env.DIRECTUS_EMAIL, password: process.env.DIRECTUS_PASSWORD })
.catch(() => {
console.error('Invalid credentials');
});
2023-04-21 20:49:05 +03:00
async function start() {
2023-04-21 21:38:10 +03:00
const a = $('solution-article').toArray();
for (let i = 0; i < a.length; i++) {
const article = a[i]
let parentTitle = $("name", $(article).parent().parent()).html();
let title = he.decode($(article).children("title").html());
let description = he.decode($(article).children("description").html());
2023-04-21 22:00:00 +03:00
let images = $(description).children("img").toArray();
for (let j = 0; j < images.length; j++) {
let imageUrl = $(images[j]).attr('src');
const n = await redis.hget("images", imageUrl);
if (!n) {
await redis.hset("images", imageUrl, "");
}
}
2023-04-21 20:49:05 +03:00
const title_slug = slug(parentTitle)+"/"+slug(title)
2023-04-21 21:38:10 +03:00
let original_id = parseInt(he.decode($(article).children("id").html()));
2023-04-21 20:49:05 +03:00
await redis.hset("pageurls", original_id, title_slug);
description = turndownService.turndown(description);
console.log('Saving page '+title);
await redis.hset("pages", original_id, JSON.stringify({
title: title,
content: description,
status: 'published',
original_id: original_id,
url: slug(title),
}))
2023-04-21 21:38:10 +03:00
}
2023-04-21 20:49:05 +03:00
2023-04-21 22:00:00 +03:00
const t = await redis.hgetall("images");
const toImport = Object.keys(t)
2023-04-21 21:25:43 +03:00
for (let i = 0; i < toImport.length; i++) {
try {
2023-04-21 22:00:00 +03:00
const n = await redis.hget("images", toImport[i]);
if (n === "") {
console.log("Saving image "+toImport[i])
let file = await directus.files.import({
url: toImport[i],
});
await redis.hset("images", toImport[i], 'https://directus.anquet.com/assets/' + file.id + '-' + file.width + '-' + file.height);
}
2023-04-21 21:25:43 +03:00
} catch (e) {
2023-04-21 22:00:00 +03:00
console.error(e.getMessage())
2023-04-21 21:25:43 +03:00
// Can't import this image.
}
}
2023-04-21 20:49:05 +03:00
const pages = await redis.hgetall("pages");
2023-04-21 21:38:10 +03:00
const p = Object.keys(pages);
for (let i = 0; i < p.length; i++) {
const original_id = p[i];
2023-04-21 20:49:05 +03:00
const page = JSON.parse(pages[original_id])
const reg = new RegExp(/https:\/\/support.anquet.com\/support\/solutions\/articles\/(\d+)-?/gm);
for (const match of page.content.matchAll(reg)) {
const title_slug = await redis.hget("pageurls", match[1]);
if (title_slug) {
const reg2 = new RegExp("/\(https:\/\/support.anquet.com\/support\/solutions\/articles\/"+match[1]+"-?.*\)/gm");
2023-04-21 21:15:25 +03:00
page.content = page.content.replace(reg2, '('+title_slug+')');
2023-04-21 20:49:05 +03:00
}
}
2023-04-21 22:00:00 +03:00
for (let i = 0; i < toImport.length; i++) {
const image_url = toImport[i]
if (page.content.match(image_url)) {
const newurl = await redis.hget("images", image_url);
if (newurl) {
page.content = page.content.replace(image_url, newurl)
}
2023-04-21 21:15:25 +03:00
}
2023-04-21 22:00:00 +03:00
}
2023-04-21 21:15:25 +03:00
2023-04-21 22:00:00 +03:00
await redis.hset("pages", original_id, JSON.stringify(page))
}
}
async function end() {
const articles = directus.items('omn2omn3');
const translations = directus.items('omn2omn3_translations');
const pages = await redis.hgetall("pages");
const p = Object.keys(pages);
for (let i = 0; i < p.length; i++) {
const original_id = p[i];
const page = JSON.parse(pages[original_id])
2023-04-21 20:49:05 +03:00
const content = await translations.createOne({
languages_code: 'en-GB',
markdown: page.content,
});
await articles.createOne({
title: page.title,
translations: [content.id],
status: 'published',
original_id: original_id,
url: page.url,
}).then(function() {
console.log("Added "+page.title);
});
2023-04-21 21:38:10 +03:00
}
2023-04-21 20:49:05 +03:00
}
2023-04-21 22:00:00 +03:00
await start();
await end();