| 2 | import fetch from 'node-fetch' |
| 3 | |
| 4 | function scrapeProperties(html) { |
| 5 | const $ = cheerio.load(html) |
| 6 | |
| 7 | const columnMap = { |
| 8 | 'Style Element': 'subject', |
| 9 | 'Outlook 2007/10/13 +': 'outlook', |
| 10 | 'Outlook 03/Express/Mail': 'outlook-legacy', |
| 11 | 'iPhone iOS 7/iPad': 'apple-ios', |
| 12 | 'Outlook.com': 'outlook-web', |
| 13 | 'Apple Mail 6.5': 'apple-mail', |
| 14 | 'Yahoo! Mail': 'yahoo-mail', |
| 15 | 'Google Gmail': 'gmail', |
| 16 | 'Android 4 (Gmail) +': 'gmail-android', |
| 17 | } |
| 18 | const columnTitles = $('#clients td').map((idx, el) => $(el).text()).get() |
| 19 | const columns = columnTitles.map((title) => { |
| 20 | const col = columnMap[title] |
| 21 | if (!col) { |
| 22 | throw new Error('Unexpected table column encountered. Scraper out of date?') |
| 23 | } |
| 24 | return col |
| 25 | }) |
| 26 | |
| 27 | const props = {} |
| 28 | const propRe = /^[a-z-]+/ |
| 29 | $('#csstable tbody tr:not(.short)').each((idx, el) => { |
| 30 | let subject |
| 31 | const data = {} |
| 32 | $(el).find('td').each((tdIdx, tdEl) => { |
| 33 | const columnName = columns[tdIdx] |
| 34 | if (columnName === 'subject') { |
| 35 | subject = $(tdEl).text() |
| 36 | } else { |
| 37 | const $successEl = $(tdEl).find('.success') |
| 38 | if ($successEl.is('.info')) { |
| 39 | data[columnName] = $successEl.attr('data-original-title') |
| 40 | } else { |
| 41 | data[columnName] = $successEl.is('.true') |
| 42 | } |
| 43 | } |
| 44 | }) |
| 45 | |
| 46 | if (!subject) { |
| 47 | throw new Error('Could not determine table row subject. Scraper out of date?') |
| 48 | } |
| 49 | |
| 50 | const match = subject.match(propRe) |
| 51 | const propName = match && match[0] |
| 52 | if (!propName) { |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | if (Object.prototype.hasOwnProperty.call(props, propName)) { |
| 57 | if (propName === 'background') { |
| 58 | // skip "CSS3" background property |
| 59 | return |
| 60 | } |
| 61 | throw new Error('Unexpected duplicate property row.') |