Skip to content

Commit a8ae1aa

Browse files
committed
Improve author connector
Handling twitter names
1 parent d2d66fb commit a8ae1aa

File tree

6 files changed

+9448
-17
lines changed

6 files changed

+9448
-17
lines changed

__snapshots__/index.js.snap-shot

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ exports['the-verge 1'] = {
648648
}
649649

650650
exports['twitter 1'] = {
651-
"author": "K I K O B E a T S ⚡️",
651+
"author": "Kikobeats",
652652
"date": "2017-06-28T21:01:00.000Z",
653653
"description": "“Experimenting with Clearbit API + Apple TV 3D Parallax https://t.co/Qsm163k4mJ https://t.co/5bcuqoEyAa”",
654654
"image": "https://pbs.twimg.com/profile_images/876748424690925568/14xPNAvw_400x400.jpg",
@@ -658,6 +658,17 @@ exports['twitter 1'] = {
658658
"url": "https://twitter.com/Kikobeats/status/880139124791029763"
659659
}
660660

661+
exports['twitter 2'] = {
662+
"author": "K4rliky",
663+
"date": "2017-11-25T19:04:00.000Z",
664+
"description": "“Lo mejor de @codemotion_es #codemotionMadrid es estar con la gente que quieres 😍@ladyCircus”",
665+
"image": "https://pbs.twimg.com/media/DPfzNMGXUAESoL3.jpg:large",
666+
"logo": "https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
667+
"publisher": "Twitter",
668+
"title": "Asume la realidad: no hay cuchara. on Twitter",
669+
"url": "https://twitter.com/k4rliky/status/934482867480121345"
670+
}
671+
661672
exports['usa-today 1'] = {
662673
"author": "Editors, Usa Today",
663674
"date": "2016-05-24T16:29:19.230Z",

src/get-data/rules/author.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const {isString, flow} = require('lodash')
55
const toTitle = require('to-title-case')
66
const {isUrl} = require('../util')
77

8-
const REGEX_BY = /^[\s\n]*by[\s\n]*/im
8+
const REGEX_BY = /^[\s\n]*by|@[\s\n]*/im
99
const REGEX_STRICT = /^\S+\s+\S+/
1010

1111
const removeBy = value => value.replace(REGEX_BY, '')
@@ -43,11 +43,16 @@ const wrap = rule => $ => {
4343

4444
const strict = rule => $ => {
4545
const value = rule($)
46-
4746
if (!REGEX_STRICT.test(value)) return
4847
return value
4948
}
5049

50+
const getFirst = ($, collection) =>
51+
collection
52+
.filter((i, el) => $(el).text().trim())
53+
.first()
54+
.text()
55+
5156
/**
5257
* Rules.
5358
*/
@@ -63,21 +68,10 @@ module.exports = [
6368
wrap($ => $('meta[property="book:author"]').attr('content')),
6469
strict(wrap($ => $('a[class*="author"]').first().text())),
6570
strict(wrap($ => $('[class*="author"] a').first().text())),
66-
67-
strict(wrap($ => $('a[href*="/author/"]')
68-
.filter((i, el) => $(el).text().trim())
69-
.first()
70-
.text()
71-
)),
72-
71+
strict(wrap($ => getFirst($, $('a[href*="/author/"]')))),
72+
wrap($ => $('a[class*="screenname"]').first().text()),
7373
strict(wrap($ => $('[class*="author"]').first().text())),
7474
strict(wrap($ => $('[class*="byline"]').first().text())),
75-
76-
wrap($ => $('.fullname')
77-
.filter((i, el) => $(el).text().trim())
78-
.first()
79-
.text()
80-
),
81-
75+
wrap($ => getFirst($, $('.fullname'))),
8276
wrap($ => $('[class*="user-info"]').text())
8377
]
File renamed without changes.
File renamed without changes.

test/web/twitter-image/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const snapshot = require('snap-shot')
4+
const {promisify} = require('util')
5+
const {resolve} = require('path')
6+
7+
const fs = require('fs')
8+
9+
const getMetaData = require('../../..')
10+
const readFile = promisify(fs.readFile)
11+
12+
const url = 'https://twitter.com/k4rliky/status/934482867480121345'
13+
14+
it('twitter', async () => {
15+
const html = await readFile(resolve(__dirname, 'input.html'))
16+
const metadata = await getMetaData({html, url})
17+
snapshot(metadata)
18+
})

0 commit comments

Comments
 (0)