Last active
August 29, 2015 14:21
-
-
Save janlisse/f2672bf8bbee009ef009 to your computer and use it in GitHub Desktop.
Paginated scraper flow
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
def scrape(implicit charset: Charset): Flow[URL, Document, Unit] = Flow[URL].mapAsyncUnordered(10)(httpClient.getDocument(charset)) | |
.buffer(10, OverflowStrategy.backpressure) | |
.map(toDocument) | |
def scrapePaginated[T](f: Document => (T, Option[URL]))(implicit charset: Charset) = Flow() { implicit b => | |
import akka.stream.scaladsl.FlowGraph.Implicits._ | |
val merge = b.add(MergePreferred[URL](1)) | |
val unzip = b.add(Unzip[T, Option[URL]]()) | |
merge ~> scrape(charset).map(f) ~> unzip.in | |
unzip.out1.filter(_.isDefined).map(_.get) ~> Flow[URL].buffer(100, OverflowStrategy.fail) ~> merge.preferred | |
(merge.in(0), unzip.out0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment