-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add custom parser - channelnewsasia.com (#44)
- Loading branch information
Showing
5 changed files
with
6,466 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
6,338 changes: 6,338 additions & 0 deletions
6,338
fixtures/www.channelnewsasia.com/1737324313613.html
Large diffs are not rendered by default.
Oops, something went wrong.
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,36 @@ | ||
export const WwwChannelnewsasiaComExtractor = { | ||
domain: 'www.channelnewsasia.com', | ||
|
||
title: { | ||
selectors: [['meta[name="og:title"]', 'value']], | ||
}, | ||
|
||
author: { | ||
selectors: [ | ||
'.link--author-profile', | ||
['meta[name="cXenseParse:author"]', 'value'], | ||
], | ||
}, | ||
|
||
date_published: { | ||
selectors: ['.article-publish:not(span)'], | ||
format: 'DD MMM YYYY HH:mma', | ||
timezone: 'Asia/Singapore', | ||
}, | ||
|
||
dek: { | ||
selectors: ['.content-detail__description'], | ||
}, | ||
|
||
lead_image_url: { | ||
selectors: [['meta[name="og:image"]', 'value']], | ||
}, | ||
|
||
content: { | ||
selectors: ['section[data-title="Content"]'], | ||
|
||
transforms: {}, | ||
|
||
clean: [], | ||
}, | ||
}; |
86 changes: 86 additions & 0 deletions
86
src/extractors/custom/www.channelnewsasia.com/index.test.js
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,86 @@ | ||
import assert from 'assert'; | ||
import URL from 'url'; | ||
import cheerio from 'cheerio'; | ||
|
||
import Parser from 'mercury'; | ||
import getExtractor from 'extractors/get-extractor'; | ||
import { excerptContent } from 'utils/text'; | ||
|
||
const fs = require('fs'); | ||
|
||
describe('WwwChannelnewsasiaComExtractor', () => { | ||
describe('initial test case', () => { | ||
let result; | ||
let url; | ||
beforeAll(() => { | ||
url = | ||
'https://www.channelnewsasia.com/singapore/police-arrest-suspects-ica-change-addresses-unauthorised-attempts-4869916'; | ||
const html = fs.readFileSync( | ||
'./fixtures/www.channelnewsasia.com/1737324313613.html' | ||
); | ||
result = Parser.parse(url, { html, fallback: false }); | ||
}); | ||
|
||
it('is selected properly', () => { | ||
const extractor = getExtractor(url); | ||
assert.equal(extractor.domain, URL.parse(url).hostname); | ||
}); | ||
|
||
it('returns the title', async () => { | ||
const { title } = await result; | ||
|
||
assert.equal( | ||
title, | ||
`Another six people arrested for unauthorised address changes using ICA online service` | ||
); | ||
}); | ||
|
||
it('returns the author', async () => { | ||
const { author } = await result; | ||
|
||
assert.equal(author, 'Daphne Yow'); | ||
}); | ||
|
||
it('returns the date_published', async () => { | ||
const { date_published } = await result; | ||
|
||
assert.equal(date_published, '2025-01-17T13:06:00.000Z'); | ||
}); | ||
|
||
it('returns the dek', async () => { | ||
const { dek } = await result; | ||
|
||
assert.equal( | ||
dek, | ||
'This brings the total number of people arrested so far to 13.' | ||
); | ||
}); | ||
|
||
it('returns the lead_image_url', async () => { | ||
const { lead_image_url } = await result; | ||
|
||
assert.equal( | ||
lead_image_url, | ||
`https://dam.mediacorp.sg/image/upload/s--WYyV_F7F--/c_crop,h_574,w_1021,x_4,y_1/f_auto,q_auto/c_fill,g_auto,h_676,w_1200/v1/mediacorp/cna/image/2022/11/17/ica3.jpg?itok=smLA3pam` | ||
); | ||
}); | ||
|
||
it('returns the content', async () => { | ||
const { content } = await result; | ||
|
||
const $ = cheerio.load(content || ''); | ||
|
||
const first13 = excerptContent( | ||
$('*') | ||
.first() | ||
.text(), | ||
13 | ||
); | ||
|
||
assert.equal( | ||
first13, | ||
'SINGAPORE: The police have arrested another six people in relation to a series' | ||
); | ||
}); | ||
}); | ||
}); |