Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scribe #467

Merged
merged 15 commits into from
Jun 2, 2017
Merged
6 changes: 4 additions & 2 deletions app/controllers/Api2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.gu.media.MediaAtomMakerPermissionsProvider
import com.gu.media.logging.Logging
import com.gu.media.upload.model.PlutoSyncMetadata
import com.gu.media.youtube.{YouTube, YouTubeClaims}
import com.gu.media.Capi
import com.gu.pandahmac.HMACAuthActions
import data.DataStores
import model.commands.CommandExceptions._
Expand All @@ -20,7 +21,8 @@ import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class Api2 (override val stores: DataStores, conf: Configuration, override val authActions: HMACAuthActions,
youTube: YouTube, youTubeClaims: YouTubeClaims, awsConfig: AWSConfig, override val permissions: MediaAtomMakerPermissionsProvider)
youTube: YouTube, youTubeClaims: YouTubeClaims, awsConfig: AWSConfig,
override val permissions: MediaAtomMakerPermissionsProvider, capi: Capi)

extends MediaAtomImplicits
with AtomAPIActions
Expand Down Expand Up @@ -65,7 +67,7 @@ class Api2 (override val stores: DataStores, conf: Configuration, override val a
}

def publishMediaAtom(id: String) = APIAuthAction.async { implicit req =>
val command = PublishAtomCommand(id, stores, youTube, youTubeClaims, req.user)
val command = PublishAtomCommand(id, stores, youTube, youTubeClaims, req.user, capi)

val updatedAtom: Future[MediaAtom] = command.process()

Expand Down
6 changes: 3 additions & 3 deletions app/di.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import com.gu.atom.play.ReindexController
import com.gu.media.aws.AwsCredentials
import com.gu.media.youtube.{YouTube, YouTubeClaims}
import com.gu.media.{CapiPreview, MediaAtomMakerPermissionsProvider, Settings}
import com.gu.media.{Capi, MediaAtomMakerPermissionsProvider, Settings}
import controllers._
import data._
import play.api.ApplicationLoader.Context
Expand Down Expand Up @@ -36,7 +36,7 @@ class MediaAtomMaker(context: Context)
private val aws = new AWSConfig(config, credentials)
aws.startKinesisLogging("media-atom-maker")

private val capi = new CapiPreview(config)
private val capi = new Capi(config)

private val stores = new DataStores(aws, capi)
private val permissions = new MediaAtomMakerPermissionsProvider(aws.stage, aws.credentials.instance)
Expand All @@ -52,7 +52,7 @@ class MediaAtomMaker(context: Context)

private val api = new Api(stores, configuration, aws, hmacAuthActions, permissions)

private val api2 = new Api2(stores, configuration, hmacAuthActions, youTube, youTubeClaims, aws, permissions)
private val api2 = new Api2(stores, configuration, hmacAuthActions, youTube, youTubeClaims, aws, permissions, capi)

private val stepFunctions = new StepFunctions(aws)
private val uploads = new UploadController(hmacAuthActions, aws, stepFunctions, stores, permissions)
Expand Down
39 changes: 37 additions & 2 deletions app/model/commands/PublishAtomCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import java.net.URL
import java.time.Instant
import java.util.Date

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes._
import play.api.libs.json.{JsValue, JsLookupResult}

import com.google.api.client.googleapis.json.GoogleJsonResponseException
import com.gu.atom.play.AtomAPIActions
import com.gu.contentatom.thrift.{ContentAtomEvent, EventType}
Expand All @@ -14,14 +19,15 @@ import data.DataStores
import model.Platform.Youtube
import model._
import model.commands.CommandExceptions._
import com.gu.media.Capi

import scala.concurrent.Future
import scala.util.control.NonFatal
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

case class PublishAtomCommand(id: String, override val stores: DataStores, youTube: YouTube, youtubeClaims: YouTubeClaims,
user: PandaUser)
user: PandaUser, val capi: Capi)
extends Command with AtomAPIActions with Logging {

type T = Future[MediaAtom]
Expand Down Expand Up @@ -141,11 +147,40 @@ case class PublishAtomCommand(id: String, override val stores: DataStores, youTu
}
}

private def removeHtmlTagsForYouTube(description: String): String = {
val html = Jsoup.parse(description)
html.select("a").remove()
html.text()
}

private def getComposerLinkText(atomId: String): String = {


val usages: JsValue = (capi.capiQuery("/atom/media/" + atomId + "/usage", true) \ "response" \ "results").get
val usagesList = usages.as[List[String]]

val composerPage = usagesList.find(usage => {
val query = capi.capiQuery(usage, true)
val contentType = (capi.capiQuery(usage, true) \ "response" \ "content" \ "type").get.as[String]
contentType == "video"
})

composerPage match {
case Some(page) => "\n View the video at https://www.theguardian.com/" + page
case None => ""
}
}

private def updateYoutubeMetadata(previewAtom: MediaAtom, asset: Asset) = {

val description = previewAtom.description.map(description => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be moved here so we can use the builder pattern like this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, this becomes difficult because getting the youtube description requires querying capi so I'm just going to leave this here.

removeHtmlTagsForYouTube(description) + getComposerLinkText(previewAtom.id)
})

val metadata = YouTubeMetadataUpdate(
title = Some(previewAtom.title),
categoryId = previewAtom.youtubeCategoryId,
description = previewAtom.description,
description = description,
tags = previewAtom.tags,
license = previewAtom.license,
privacyStatus = previewAtom.privacyStatus.map(_.name))
Expand Down
8 changes: 7 additions & 1 deletion build_config/webpack.dev.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ module.exports = {

plugins: [
new ExtractTextPlugin('main.css')
]
],
node: {
console: 'true',
fs: 'empty',
net: 'empty',
tls: 'empty'
}
};
2 changes: 1 addition & 1 deletion common/src/main/scala/com/gu/media/CapiAccess.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ trait CapiAccess { this: Settings =>
}
}

class CapiPreview(override val config: Config) extends Settings with CapiAccess
class Capi(override val config: Config) extends Settings with CapiAccess
case class CapiException(err: String, cause: Throwable = null) extends RuntimeException(err, cause)
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
"redux": "^3.6.0",
"redux-thunk": "^2.1.0",
"reqwest": "^2.0.5",
"scribe": "guardian/scribe#fcf4e16",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using scribe on this commit rather than a release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because scribe hasn't been released for a while so newer versions are not available on npm. I could look into releasing it later today.

"scribe-plugin-keyboard-shortcuts": "^0.1.1",
"scribe-plugin-link-prompt-command": "^1.0.0",
"scribe-plugin-sanitizer": "^0.1.10",
"scribe-plugin-toolbar": "^1.0.0",
"valid-url": "^1.0.9"
},
"version": "1.0.0",
Expand Down
5 changes: 3 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ object Dependencies {
val apacheHttpClient = "org.apache.httpcomponents" % "httpclient" % "4.0.1"
val apacheHttpCore = "org.apache.httpcomponents" % "httpcore" % "4.0.1"


val jsoup = "org.jsoup" % "jsoup" % "1.8.3"

val panda = Seq(
"com.gu" %% "pan-domain-auth-play_2-5" % pandaVersion,
"com.gu" %% "pan-domain-auth-verification" % pandaVersion,
Expand Down Expand Up @@ -92,7 +93,7 @@ object Dependencies {
val appDependencies = panda ++ atomMaker ++ slf4j ++ Seq(
PlayImport.cache, scalaLogging, jacksonDatabind, okHttp, contentAtomModel, diff,
awsSts, awsEc2, scalaTestPlusPlay, mockito, scalaXml, awsTranscoder,
awsSQS, awsSNS, awsS3
awsSQS, awsSNS, awsS3, jsoup
)

val uploaderDependencies = Seq(
Expand Down
Loading