|
| 1 | +package com.karasiq.webmtv.sosach |
| 2 | + |
| 3 | +import akka.actor.ActorSystem |
| 4 | +import akka.http.scaladsl.Http |
| 5 | +import akka.http.scaladsl.model.headers.Accept |
| 6 | +import akka.http.scaladsl.model.{HttpRequest, MediaRange, MediaTypes} |
| 7 | +import akka.stream.ActorMaterializer |
| 8 | +import akka.stream.scaladsl.Source |
| 9 | +import akka.util.ByteString |
| 10 | +import derive.key |
| 11 | +import upickle.Js |
| 12 | +import upickle.default._ |
| 13 | + |
| 14 | +private object JsonApiObjects { |
| 15 | + case class PostId(value: Long) extends AnyVal |
| 16 | + object PostId { |
| 17 | + implicit def postIdToLong(postId: PostId): Long = postId.value |
| 18 | + } |
| 19 | + |
| 20 | + case class File(path: String) |
| 21 | + case class Post(@key("num") id: PostId, subject: String, comment: String, files: Seq[File]) |
| 22 | + case class Thread(posts: Seq[Post]) |
| 23 | + case class Board(@key("Board") name: String, pages: Seq[Int], threads: Seq[Thread]) |
| 24 | + case class ThreadWrapped(@key("Board") board: String, @key("threads") thread: Option[Thread]) |
| 25 | + |
| 26 | + object Implicits { |
| 27 | + implicit val postIdReader = Reader[PostId] { |
| 28 | + case Js.Str(str) ⇒ |
| 29 | + PostId(str.toLong) |
| 30 | + |
| 31 | + case Js.Num(num) ⇒ |
| 32 | + PostId(num.toLong) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class Json2chBoardApi(host: String = "2ch.hk")(implicit as: ActorSystem, am: ActorMaterializer) extends BoardApi { |
| 38 | + import JsonApiObjects.Implicits._ |
| 39 | + |
| 40 | + private val http = Http() |
| 41 | + |
| 42 | + private def retrieveJson[T: Reader](url: String) = { |
| 43 | + Source |
| 44 | + .fromFuture(http.singleRequest(HttpRequest(uri = url, headers = List(Accept(MediaRange(MediaTypes.`application/json`)))))) |
| 45 | + .flatMapConcat(_.entity.dataBytes) |
| 46 | + .fold(ByteString.empty)(_ ++ _) |
| 47 | + .map(bs ⇒ read[T](bs.utf8String)) |
| 48 | + } |
| 49 | + |
| 50 | + private def jsonToAppPost(board: String, postObj: JsonApiObjects.Post): Board.Post = { |
| 51 | + Board.Post(postObj.id, postObj.subject, postObj.comment, postObj.files.map(file ⇒ s"https://$host/$board/${file.path}")) |
| 52 | + } |
| 53 | + |
| 54 | + def board(name: String) = { |
| 55 | + val url = s"https://$host/$name/index.json" |
| 56 | + retrieveJson[JsonApiObjects.Board](url) |
| 57 | + .flatMapConcat { page ⇒ |
| 58 | + val pages = page.pages.filter(_ > 0).map(page ⇒ s"https://$host/$name/$page.json") |
| 59 | + Source.single(page).concat( |
| 60 | + Source(pages.toVector) |
| 61 | + .flatMapConcat(url ⇒ retrieveJson[JsonApiObjects.Board](url).recoverWithRetries(1, { case _ ⇒ Source.empty })) |
| 62 | + ) |
| 63 | + } |
| 64 | + .mapConcat { board ⇒ |
| 65 | + val threads = for (threadObj ← board.threads) |
| 66 | + yield Board.Thread(board.name, threadObj.posts.map(jsonToAppPost(board.name, _))) |
| 67 | + threads.toVector |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + def thread(board: String, id: Long) = { |
| 72 | + val url = s"https://$host/$board/res/$id.json" |
| 73 | + retrieveJson[JsonApiObjects.ThreadWrapped](url) |
| 74 | + .map(thread ⇒ Board.Thread(thread.board, thread.thread.toSeq.flatMap(_.posts.map(jsonToAppPost(thread.board, _))))) |
| 75 | + } |
| 76 | +} |
0 commit comments