-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathFaciaContentFrontendHelpers.scala
81 lines (68 loc) · 3.17 KB
/
FaciaContentFrontendHelpers.scala
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
package implicits
import common.Edition.defaultEdition
import implicits.Dates._
import model._
import model.content.{MediaAssetPlatform, MediaAtom}
import model.pressed._
import org.joda.time.DateTime
import org.jsoup.Jsoup
import com.github.nscala_time.time.Implicits._
import scala.util.Try
object FaciaContentFrontendHelpers {
implicit class FaciaContentFrontendHelper(faciaContent: PressedContent) {
def frontendTags: Seq[model.Tag] = faciaContent.properties.maybeContent.map(_.tags.tags).getOrElse(Nil)
val trailPicture: Option[ImageMedia] = {
val imageOverride = faciaContent.properties.image.flatMap(ImageOverride.createImageMedia)
val defaultTrailPicture = faciaContent.properties.maybeContent.flatMap(_.trail.trailPicture)
imageOverride.orElse(defaultTrailPicture)
}
//TODO: Use the blocks field of CAPI to derive this in a more structured way
def mainYouTubeMediaAtom: Option[MediaAtom] =
for {
main <- faciaContent.properties.maybeContent.map(_.fields.main)
mediaAtoms <- faciaContent.properties.maybeContent.map(_.elements.mediaAtoms)
document <- Some(Jsoup.parse(main))
atomContainer <- Option(document.getElementsByClass("element-atom").first())
bodyElement <- Some(atomContainer.getElementsByTag("gu-atom"))
atomId <- Some(bodyElement.attr("data-atom-id"))
mainMediaAtom <- mediaAtoms.find(ma =>
(ma.id == atomId && !ma.expired.getOrElse(false)) && ma.assets.exists(
_.platform == MediaAssetPlatform.Youtube,
),
)
} yield mainMediaAtom
def mainVideo: Option[VideoElement] = faciaContent.properties.maybeContent.flatMap(_.elements.mainVideo)
lazy val shouldHidePublicationDate: Boolean = {
faciaContent.branding(defaultEdition).exists(_.isPaid) &&
faciaContent.card.webPublicationDateOption.exists(_.isOlderThan(2.weeks))
}
def slideshow: Option[List[FaciaImageElement]] =
faciaContent.properties.image match {
case Some(ImageSlideshow(assets)) =>
Option {
assets.flatMap(asset =>
Try(
FaciaImageElement(
asset.imageSrc,
asset.imageSrcWidth.toInt,
asset.imageSrcHeight.toInt,
asset.imageCaption,
),
).toOption,
)
}
case _ => None
}
def trailSlideshow(aspectWidth: Int, aspectHeight: Int): Option[List[FaciaImageElement]] =
slideshow.map(_.filter(image => IsRatio(aspectWidth, aspectHeight, image.width, image.height)))
def contributors: Seq[Tag] = faciaContent.properties.maybeContent.map(_.tags.contributors).getOrElse(Nil)
def series: Seq[Tag] = faciaContent.properties.maybeContent.map(_.tags.series).getOrElse(Nil)
def keywords: Seq[Tag] = faciaContent.properties.maybeContent.map(_.tags.keywords).getOrElse(Nil)
def supporting: List[PressedContent] =
faciaContent match {
case content: CuratedContent => content.supportingContent
case _ => Nil
}
def webPublicationDate: DateTime = faciaContent.card.webPublicationDateOption.getOrElse(DateTime.now)
}
}