Last active
February 11, 2022 07:01
-
-
Save pbernet/dc26109e9e1f562d68ea69fbd70d7347 to your computer and use it in GitHub Desktop.
ContentFetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model.HttpRequest | |
import org.slf4j.{Logger, LoggerFactory} | |
import java.net.URLEncoder | |
import scala.concurrent.Future | |
import scala.concurrent.duration.DurationInt | |
import scala.util.{Failure, Success} | |
object ContentFetcher extends App { | |
val logger: Logger = LoggerFactory.getLogger(this.getClass) | |
implicit val system: ActorSystem = ActorSystem() | |
import system.dispatcher | |
def fetchContent(title: String): Future[String] = { | |
logger.info(s"About to read `extract` from Wikipedia entry with title: $title") | |
val encodedTitle = URLEncoder.encode(title, "UTF-8") | |
val requestURL = s"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=$encodedTitle" | |
Http().singleRequest(HttpRequest(uri = requestURL)) | |
// Consume the streamed response entity | |
// Doc: https://doc.akka.io/docs/akka-http/current/client-side/request-level.html | |
.flatMap(_.entity.toStrict(2.seconds)) | |
.map(_.data.utf8String.split("\"extract\":").reverse.head) | |
} | |
val title = "Adam" | |
val responseFuture = fetchContent(title) | |
responseFuture | |
.onComplete { | |
case Success(res) => logger.info(s"Title: ${title} yields content: ${res}") | |
case Failure(_) => logger.info(s"Done with title err") //sys.error("something wrong") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment