Skip to content

Commit e1383e3

Browse files
committed
Merge player with video, use extraction in helpers
1 parent a9b1196 commit e1383e3

File tree

6 files changed

+1062
-423
lines changed

6 files changed

+1062
-423
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"arrify": "^1.0.1",
6161
"concat-stream": "^1.5.2",
6262
"content-type": "^1.0.2",
63-
"exif-date": "^2.0.0",
63+
"exif-date": "^2.0.1",
6464
"exiftool2": "0.0.4",
6565
"getvalue": "^1.1.0",
6666
"htmlmetaparser": "^1.2.0",

src/extract/helpers/icon-selector.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Icon } from 'htmlmetaparser'
33
import { ScrapeResult, scrapeResponse } from '../../scrape'
44
import { Snippet, ImageSnippet } from '../interfaces'
55
import { Response, makeRequest as defaultMakeRequest } from '../../scrape/support'
6+
import { extract } from '../'
67

78
export interface Options {
89
preferredSize?: number
@@ -28,19 +29,8 @@ export default function (options: Options = {}) {
2829
if (icons.length === 0 && options.fallbackOnFavicon !== false) {
2930
const url = resolve(result.url, '/favicon.ico')
3031
const response = await makeRequest(url)
31-
const { exifData } = await scrapeResponse(response)
32-
33-
if (!exifData) {
34-
return snippet
35-
}
36-
37-
const icon: ImageSnippet = {
38-
type: 'image',
39-
url,
40-
width: exifData.ImageWidth,
41-
height: exifData.ImageHeight,
42-
encodingFormat: exifData.MIMEType
43-
}
32+
const iconResult = await scrapeResponse(response)
33+
const icon = await extract(iconResult) as ImageSnippet
4434

4535
return Object.assign(snippet, { icon })
4636
}

src/extract/helpers/image-size.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { scrapeResponse } from '../../scrape'
2-
import { Snippet } from '../interfaces'
2+
import { Snippet, ImageSnippet } from '../interfaces'
33
import { Response, makeRequest as defaultMakeRequest } from '../../scrape/support'
4+
import { extract } from '../'
45

56
export interface Options {
67
makeRequest?: (url: string) => Promise<Response>
@@ -19,22 +20,11 @@ export default function (options: Options = {}) {
1920

2021
snippet.image = await Promise.all(snippet.image.map(async (image) => {
2122
try {
22-
const response = await makeRequest(image.secureUrl || image.url)
23-
const { exifData } = await scrapeResponse(response)
23+
const url = image.secureUrl || image.url
24+
const response = await makeRequest(url)
25+
const result = await scrapeResponse(response)
2426

25-
if (!exifData) {
26-
return image // Skip when we failed to get exif data.
27-
}
28-
29-
return {
30-
type: 'image' as 'image',
31-
url: image.url,
32-
secureUrl: image.secureUrl,
33-
caption: image.caption,
34-
encodingFormat: exifData.MIMEType || image.encodingFormat,
35-
width: exifData.ImageWidth || image.width,
36-
height: exifData.ImageHeight || image.height
37-
}
27+
return await extract(result) as ImageSnippet
3828
} catch (e) {
3929
return image // Ignore bad requests.
4030
}

src/extract/interfaces.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ export interface SnippetLocale {
2121
alternate?: string[]
2222
}
2323

24-
export interface SnippetPlayer {
25-
url: string
26-
width: number
27-
height: number
28-
streamUrl?: string
29-
streamContentType?: string
30-
}
31-
3224
export interface SnippetTwitter {
3325
siteId?: string
3426
siteHandle?: string
@@ -82,7 +74,6 @@ export interface HtmlSnippet extends BaseSnippet {
8274
image?: ImageSnippet[]
8375
video?: VideoSnippet[]
8476
audio?: AudioSnippet[]
85-
player?: SnippetPlayer
8677
headline?: string
8778
description?: string
8879
tags?: string[]

src/extract/snippet/html.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
SnippetAppLink,
1111
SnippetApps,
1212
SnippetLocale,
13-
SnippetPlayer,
1413
SnippetTwitter
1514
} from '../interfaces'
1615

@@ -31,7 +30,6 @@ export default function (result: ScrapeResult): HtmlSnippet {
3130
image: getImage(result),
3231
video: getVideo(result),
3332
audio: getAudio(result),
34-
player: getPlayer(result),
3533
entity: getEntity(result),
3634
url: result.url,
3735
canonicalUrl: getCanonicalUrl(result),
@@ -289,6 +287,22 @@ function getVideo (result: ScrapeResult): VideoSnippet[] {
289287
addVideos(ogpVideos, ogpSecureUrls, ogpTypes, ogpWidths, ogpHeights)
290288
}
291289

290+
if (getValue(result, ['twitter', 'card']) === 'player') {
291+
const embedUrl = getValue(result, ['twitter', 'player'])
292+
const width = toNumber(getValue(result, ['twitter', 'player:width']))
293+
const height = toNumber(getValue(result, ['twitter', 'player:height']))
294+
const url = getValue(result, ['twitter', 'player:stream'])
295+
const encodingFormat = getValue(result, ['twitter', 'player:stream:content_type'])
296+
297+
if (embedUrl && width && height) {
298+
addVideo({ type: 'video', url: embedUrl, encodingFormat: 'text/html', width, height })
299+
}
300+
301+
if (url) {
302+
addVideo({ type: 'video', url, encodingFormat, width, height })
303+
}
304+
}
305+
292306
return videos
293307
}
294308

@@ -531,35 +545,6 @@ function getTtl (result: ScrapeResult): number | undefined {
531545
toNumber(getValue(result, ['oembed', 'cache_age']))
532546
}
533547

534-
/**
535-
* Retrieve a URL for embedding an interactive widget.
536-
*/
537-
function getPlayer (result: ScrapeResult): SnippetPlayer | undefined {
538-
const isPlayer = getValue(result, ['twitter', 'card']) === 'player'
539-
540-
if (!isPlayer) {
541-
return
542-
}
543-
544-
const url = getValue(result, ['twitter', 'player'])
545-
const width = toNumber(getValue(result, ['twitter', 'player:width']))
546-
const height = toNumber(getValue(result, ['twitter', 'player:height']))
547-
const streamUrl = getValue(result, ['twitter', 'player:stream'])
548-
const streamContentType = getValue(result, ['twitter', 'player:stream:content_type'])
549-
550-
if (url && width && height) {
551-
return {
552-
url,
553-
width,
554-
height,
555-
streamUrl,
556-
streamContentType
557-
}
558-
}
559-
560-
return
561-
}
562-
563548
/**
564549
* Extract HTML page content types.
565550
*/

0 commit comments

Comments
 (0)