Skip to content

Commit

Permalink
tweak title from URL code
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 13, 2018
1 parent 0644acc commit 9eec0bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
4 changes: 1 addition & 3 deletions modules/user/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ final class Env(

val jsonView = new JsonView(isOnline)

private lazy val titleUrl = new TitleUrl

lazy val noteApi = new NoteApi(db(CollectionNote), timeline, system.lilaBus, titleUrl)
lazy val noteApi = new NoteApi(db(CollectionNote), timeline, system.lilaBus)

lazy val trophyApi = new TrophyApi(db(CollectionTrophy))

Expand Down
5 changes: 2 additions & 3 deletions modules/user/src/main/NoteApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ case class UserNotes(user: User, notes: List[Note])
final class NoteApi(
coll: Coll,
timeline: akka.actor.ActorSelection,
bus: lila.common.Bus,
titleUrl: TitleUrl
bus: lila.common.Bus
) {

import reactivemongo.bson._
Expand Down Expand Up @@ -80,7 +79,7 @@ final class NoteApi(
), 'userNote)
}
} >> {
modOnly ?? titleUrl(text) flatMap {
modOnly ?? Title.fromUrl(text) flatMap {
_ ?? { UserRepo.addTitle(to.id, _) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ object Title {
lazy val fromNames = all.map(_.swap).toMap

def titleName(title: String) = names get title getOrElse title
}

final class TitleUrl {
object fromUrl {

// https://ratings.fide.com/card.phtml?event=740411
private val FideProfileUrlRegex = """(?:https?:https://)ratings\.fide\.com/card\.phtml\?event=(\d+)""".r
// >&nbsp;FIDE title</td><td colspan=3 bgcolor=#efefef>&nbsp;Grandmaster</td>
private val FideProfileTitleRegex = """>&nbsp;FIDE title</td><td colspan=3 bgcolor=#efefef>&nbsp;([^<]+)</td>""".r.unanchored
// https://ratings.fide.com/card.phtml?event=740411
private val FideProfileUrlRegex = """(?:https?:https://)ratings\.fide\.com/card\.phtml\?event=(\d+)""".r
// >&nbsp;FIDE title</td><td colspan=3 bgcolor=#efefef>&nbsp;Grandmaster</td>
private val FideProfileTitleRegex = """>&nbsp;FIDE title</td><td colspan=3 bgcolor=#efefef>&nbsp;([^<]+)</td>""".r.unanchored

def apply(url: String): Fu[Option[String]] = url.trim match {
case FideProfileUrlRegex(id) => parseIntOption(id) ?? fromFideProfile
case _ => fuccess(none)
}
def apply(url: String): Fu[Option[String]] = url.trim match {
case FideProfileUrlRegex(id) => parseIntOption(id) ?? fromFideProfile
case _ => fuccess(none)
}

private def fromFideProfile(id: Int): Fu[Option[String]] = {
WS.url(s"""http:https://ratings.fide.com/card.phtml?event=$id""").get().map(_.body) map {
case FideProfileTitleRegex(name) => Title.fromNames get name
private def fromFideProfile(id: Int): Fu[Option[String]] = {
WS.url(s"""http:https://ratings.fide.com/card.phtml?event=$id""").get().map(_.body) map {
case FideProfileTitleRegex(name) => Title.fromNames get name
}
}
}
}

0 comments on commit 9eec0bb

Please sign in to comment.