-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelperFunctions.js
148 lines (125 loc) · 3.57 KB
/
helperFunctions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const moment = require('moment')
const axios = require('axios')
const HTMLParser = require('node-html-parser')
const {
DP,
STREET,
DP_TAGS,
DP_CEO_TAGS,
STREET_TAGS,
UTB_TAGS,
TAG_TO_NAME,
} = require('./constants')
const TIME_AGO = published_at =>
moment(published_at, 'YYYY-MM-DD HH:mm:ss').fromNow()
const DAYS_AGO = published_at =>
moment().diff(moment(published_at, 'YYYY-MM-DD HH:mm:ss'), 'days')
const getRandomIntInclusive = (min, max) =>
Math.floor(Math.random() * (max - min + 1) + min)
const addPhotoCredits = async content => {
// add embedded photo credits
const root = HTMLParser.parse(content)
const imgs = root.querySelectorAll('.media-embed')
const imgsPromise = imgs.map(img => {
const uuid = img.getAttribute('data-uuid')
return axios.get(`https://www.thedp.com/search.json?a=1&s=${uuid}&ty=media`)
})
const imgsAuthors = await Promise.all(imgsPromise)
imgsAuthors.forEach((resp, idx) => {
const { data } = resp
const authors = data.items[0].authors.map(({ name }) => name)
const authorString = authors.join(', ')
const credit = authorString ? `Credit: ${authorString}` : ''
const newNode = `<figure>${HTMLParser.parse(
imgs[idx].outerHTML
)}<figcaption>${credit}</figcaption></figure>`
root.exchangeChild(imgs[idx], newNode)
})
return root.toString()
}
const parseArticleMetaData = (
article,
publication,
section,
isSectionArticle = false
) => {
const { published_at, authors, slug, tags, dominantMedia = {} } = article
// generate the correct slug
const firstIndex = published_at.indexOf('-')
const year = published_at.substring(0, firstIndex)
const month = published_at.substring(
firstIndex + 1,
published_at.indexOf('-', firstIndex + 1)
)
article.slug = `${year}/${month}/${slug}`
// parse authors
article.authors = authors.map(({ name, slug }) => ({ name, slug }))
if (dominantMedia.authors) {
article.dominantMedia.authors = dominantMedia.authors.map(
({ name, slug }) => ({ name, slug })
)
}
// parse tag
if (isSectionArticle) {
// this is a section article from the discover page
article.tag = section
if (article.tag in TAG_TO_NAME) {
article.tag = TAG_TO_NAME[article.tag]
}
} else {
// home article/ search article/ setting article
let TAGS = []
switch (publication) {
case DP:
TAGS = DP_TAGS
break
case STREET:
TAGS = STREET_TAGS
break
default:
TAGS = UTB_TAGS
}
if (TAGS.includes(section)) {
article.tag = section
} else if (DP_CEO_TAGS.includes(section)) {
article.tag = section.split('-')[2]
} else {
const article_tags = tags.map(({ slug }) => slug)
for (let i = 0; i < TAGS.length; i++) {
if (article_tags.includes(TAGS[i])) {
article.tag = TAGS[i]
break
}
}
}
if (article.tag in TAG_TO_NAME) {
article.tag = TAG_TO_NAME[article.tag]
}
if (article.tag) {
article.tag = article.tag.replace('-', ' ')
} else {
// verify if this is ok
article.tag = 'uncategorized'
}
}
delete article.tags
// parse published_at
article.published_at = TIME_AGO(published_at)
return article
}
const parseArticle = async (
article,
publication,
section,
isSectionArticle = false
) => {
article = parseArticleMetaData(article, publication, section, isSectionArticle)
article.content = await addPhotoCredits(article.content)
return article
}
module.exports = {
DAYS_AGO,
getRandomIntInclusive,
parseArticleMetaData,
parseArticle
}