Skip to content

Commit

Permalink
initial implementation of planet feed wiki migration tool
Browse files Browse the repository at this point in the history
Co-authored-by: Josue <[email protected]>
  • Loading branch information
chrispinkney and manekenpix committed Apr 8, 2021
1 parent c389260 commit 76ef916
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/users/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
firebase-debug.log
firestore-debug.log
ui-debug.log
legacy_users.json
1 change: 1 addition & 0 deletions src/api/users/env.local
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
FIRESTORE_EMULATOR_HOST=localhost:8088
USERS_PORT=6666
USERS_URL=http://localhost:6666
FEED_URL=https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List
4 changes: 4 additions & 0 deletions src/api/users/tools/migrate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TODO:

- talk about why this is needed
- talk about env.local
68 changes: 68 additions & 0 deletions src/api/users/tools/migrate/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const fetch = require('node-fetch');
const jsdom = require('jsdom');
const { isWebUri } = require('valid-url');
const fs = require('fs');
const { logger } = require('@senecacdot/satellite');

const { JSDOM } = jsdom;
const { FEED_URL } = process.env;

const FILE = 'legacy_users.json';

const getWikiText = async (url) => {
try {
const response = await fetch(url);
const data = await response.text();

const dom = new JSDOM(data);
return dom.window.document.querySelector('pre').textContent;
} catch {
throw new Error(`Unable to download wiki feed data from url ${url}`);
}
};

(async () => {
const commentRegex = /^\s*#/;
let wikiText;
let firstName;
let lastName;
const users = [];

// Try to fetch the feed list from 'FEED_URL'
try {
wikiText = await getWikiText(
FEED_URL || 'https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List'
);
} catch (error) {
logger.error(error);
process.exit(1);
}

// store every line in an array
const lines = wikiText.split(/\r\n|\r|\n/);

// Iterate through all lines and find url/name pairs, then parse them.
lines.forEach((line, index) => {
const feeds = [];

if (!commentRegex.test(line) && line.startsWith('[')) {
feeds.push(line.replace(/[[\]']/g, ''));

if (feeds.length && isWebUri(feeds[0])) {
[firstName, lastName] = lines[index + 1].replace(/^\s*name\s*=\s*/, '').split(' ');
users.push({ firstName, lastName, feeds });
} else {
logger.error(`Skipping invalid wiki feed url ${feeds} for author ${firstName} ${lastName}`);
}
}
});

try {
fs.writeFileSync(`${FILE}`, JSON.stringify(users));
logger.info(
`Processed ${users.length} records. Legacy users were successfully written to file: ${FILE}.`
);
} catch (err) {
logger.error(err);
}
})();
19 changes: 19 additions & 0 deletions src/api/users/tools/migrate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "migrate",
"version": "1.0.0",
"description": "Migrates users from the planet feed wiki list to Firebase",
"main": "migrate.js",
"scripts": {
"start": "env-cmd -f ../../env.local node migrate.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@senecacdot/satellite": "^1.x.0",
"env-cmd": "^10.1.0",
"jsdom": "^16.5.2",
"node-fetch": "^2.6.1",
"set-interval-async": "^2.0.1",
"valid-url": "^1.0.9"
}
}

0 comments on commit 76ef916

Please sign in to comment.