-
Notifications
You must be signed in to change notification settings - Fork 2
/
open-graph.js
64 lines (56 loc) · 1.57 KB
/
open-graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const ogs = require('open-graph-scraper')
const logger = require('@architect/shared/logger')
function setName (result, properties) {
if (result.ogTitle) {
properties.name = [result.ogTitle]
} else if (result.twitterTitle) {
properties.name = [result.twitterTitle]
}
}
function setContent (result, properties) {
if (result.ogDescription) {
properties.content = [result.ogDescription]
} else if (result.twitterDescription) {
properties.content = [result.twitterDescription]
}
}
function setPhoto (result, properties) {
if (result.ogImage) {
properties.photo = [result.ogImage.url]
} else if (result.twitterImage) {
properties.photo = [result.twitterImage.url]
}
}
function setAudio (result, properties) {
if (result.ogAudioSecureURL) {
properties.audio = [result.ogAudioSecureURL]
} else if (result.ogAudioURL) {
properties.audio = [result.ogAudioURL]
}
}
function setAuthor (result, properties) {
if (result.author) {
properties.author = [{
type: ['h-card'],
properties: {
name: [result.author]
}
}]
}
}
async function fetchContext (url) {
const { error, result, response } = await ogs({ url })
if (error) {
logger.warn('Failed to fetch context from source', `${url}\n${response}`)
return
}
logger.info('OGS result', JSON.stringify(result, null, 2))
const properties = {}
setName(result, properties)
setContent(result, properties)
setPhoto(result, properties)
setAudio(result, properties)
setAuthor(result, properties)
return properties
}
module.exports = { fetchContext }