Skip to content

Commit

Permalink
TRELLO-2203: integrate social blade client
Browse files Browse the repository at this point in the history
  • Loading branch information
ssedoudbgouv committed Feb 19, 2024
1 parent eda9ee7 commit 05ef8cd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
10 changes: 6 additions & 4 deletions app/orchestrators/socialmedia/InfluencerOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ class InfluencerOrchestrator(
val executionContext: ExecutionContext
) {

def exist(name: String, socialNetwork: SocialNetworkSlug): Future[Boolean] =
influencerRepository.get(name, socialNetwork).flatMap { signalConsoCertifiedInfluencers =>
def exist(name: String, socialNetwork: SocialNetworkSlug): Future[Boolean] = {
val curated = name.toLowerCase.replaceAll("\\s", "")
influencerRepository.get(curated, socialNetwork).flatMap { signalConsoCertifiedInfluencers =>
if (signalConsoCertifiedInfluencers.nonEmpty) {
Future.successful(true)
} else {
socialBladeClient.checkSocialNetworkUsername(socialNetwork, name).flatMap { existsOnSocialBlade =>
socialBladeClient.checkSocialNetworkUsername(socialNetwork, curated).flatMap { existsOnSocialBlade =>
if (existsOnSocialBlade) {
influencerRepository.create(CertifiedInfluencer(UUID.randomUUID(), socialNetwork, name)).map(_ => true)
influencerRepository.create(CertifiedInfluencer(UUID.randomUUID(), socialNetwork, curated)).map(_ => true)
} else {
Future.successful(false)
}
}
}
}
}

}
23 changes: 17 additions & 6 deletions app/orchestrators/socialmedia/SocialBladeClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ class SocialBladeClient(config: SocialBladeClientConfiguration)(implicit ec: Exe
): Future[Boolean] = {

val request: RequestT[Identity, Either[String, String], Any] = basicRequest
.get(uri"${config.url}/b/${platform.entryName.toLowerCase}/statistics")
.header("query", username.toLowerCase)
.get(uri"${config.url}/b/${platform.entryName.toLowerCase}/statistics?query=${username}")
.header("clientid", config.clientId)
.header("token", config.token)
.header("history", "default")
.response(asString)

logger.infoWithTitle("socialblade_client", request.toCurl(Set("clientid", "token")))

request.send(backend).map {
case Response(Right(body), statusCode, _, _, _, _) if statusCode.isSuccess =>
handleSuccessResponse(body, username)
handleSuccessResponse(body, username, platform)

case Response(Right(body), statusCode, _, _, _, _) =>
logger.errorWithTitle(
Expand All @@ -50,19 +51,29 @@ class SocialBladeClient(config: SocialBladeClientConfiguration)(implicit ec: Exe
false

case Response(Left(error), statusCode, _, _, _, _) =>
logger.errorWithTitle("socialblade_client_error", s"Error $statusCode calling Social blade : $error")
if (statusCode.code == 404) {
logger.infoWithTitle("socialblade_client_notfound", s"${username} not found for ${platform.entryName}")
} else {
logger.errorWithTitle("socialblade_client_error", s"Error $statusCode calling Social blade : $error")
}
// Act as the username does not exist in social blade
false
}

}

private def handleSuccessResponse(body: String, username: String): Boolean =
private def handleSuccessResponse(body: String, username: String, platform: SocialNetworkSlug): Boolean =
Try(Json.parse(body))
.map { jsonBody =>
jsonBody.validate[SocialBladeResponse] match {
case JsSuccess(response, _) =>
response.data.id.username.equalsIgnoreCase(username)
val found = response.data.id.username.equalsIgnoreCase(username)
if (found) {
logger.infoWithTitle("socialblade_client_found", s"${username} found for ${platform.entryName}")
} else {
logger.infoWithTitle("socialblade_client_notfound", s"${username} not found for ${platform.entryName}")
}
found
case JsError(errors) =>
logger.errorWithTitle(
"socialblade_client_error",
Expand Down
4 changes: 2 additions & 2 deletions conf/common/socialblade.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ social-blade {
url: "https://matrix.sbapis.com"
url: ${?SOCIALBLADE_URL}
client-id: ""
client-id: ${?SOCIALBLADE-CLIENT_ID}
client-id: ${?SOCIALBLADE_CLIENT_ID}
token: ""
token: ${?SOCIALBLADE-TOKEN}
token: ${?SOCIALBLADE_TOKEN}
}
2 changes: 1 addition & 1 deletion zip.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ GET localhost:9000/api/reports/files?reportId=d894fe80-935b-45f8-bfab-ea1c8933f5
###


GET localhost:9000/api/certified-influencer?name=orange&socialNetwork=Facebook
GET "localhost:9000/api/certified-influencer?name=xxxx&socialNetwork=YouTube"

0 comments on commit 05ef8cd

Please sign in to comment.