Skip to content

Commit

Permalink
adding scraping from MDN
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriend committed Jun 15, 2018
1 parent a11b889 commit 79194e5
Show file tree
Hide file tree
Showing 4 changed files with 1,031 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Key.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export enum Key {
Percent = '%',
Six = '6',
Caret = '^',

/** something */
Hat = Caret,
Seven = '7',
Ampersand = '&',
Expand Down Expand Up @@ -143,4 +145,4 @@ export enum Key {
GreaterThan = '>',
ForwardSlash = '/',
Question = '?',
}
}
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "dist/js/Key.enum.js",
"types": "dist/declarations/Key.enum.d.ts",
"scripts": {
"build": "tsc"
"build": "tsc",
"scrape": "node scrapeMDNForKeys.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,6 +34,14 @@
},
"homepage": "https://github.com/nfriend/ts-key-enum#readme",
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/cheerio": "^0.22.7",
"@types/request": "^2.47.0",
"@types/request-promise": "^4.1.41",
"chalk": "^2.4.1",
"cheerio": "^1.0.0-rc.2",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"typescript": "^2.9.1"
}
}
58 changes: 58 additions & 0 deletions scrapeMDNForKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const rp = require('request-promise');
const cheerio = require('cheerio');
const chalk = require('chalk');
const fs = require('fs');
const _ = require('lodash');

(async () => {
const mdnUrl = `https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys`;

console.info(chalk.gray('Making GET request to MDN...'));
const pageHtml = await rp(mdnUrl);
console.info(chalk.gray('Successfully received response from MDN.'));

console.info(chalk.gray('Scraping HTML for key names...'));
const $ = cheerio.load(pageHtml);
const keys = [];
const keyValueRegex = /\"([a-z0-9_]+)\"/i;
$('tr').each((index, element) => {
const value = $(element)
.find('td:nth-child(1) code')
.text();
const description = $(element)
.find('td:nth-child(2)')
.text();

const matches = keyValueRegex.exec(value);
const extractedValue = matches ? matches[1] : undefined;

if (extractedValue && description) {
keys.push({ value: extractedValue, description: description });
}
});

// eliminate duplicate keys


console.info(
chalk.gray('Done scraping. Found the following keys:\n') +
keys
.map(
k =>
` - ${chalk.blue(k.value)}: ${chalk.green(
k.description,
)}`,
)
.join('\n'),
);

let enumFile = `export enum Key {\n\n`;

enumFile += keys.map(k => {
return ` /** ${k.description} */\n ${k.value} = '${k.value}',\n\n`;
}).join('')

enumFile += '}';

fs.writeFileSync('./test.ts', enumFile);
})();
Loading

0 comments on commit 79194e5

Please sign in to comment.