-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial implementation of planet feed wiki migration tool
Co-authored-by: Josue <[email protected]>
- Loading branch information
1 parent
c389260
commit 76ef916
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
firebase-debug.log | ||
firestore-debug.log | ||
ui-debug.log | ||
legacy_users.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |