Skip to content

Commit

Permalink
more permissive fiction link format
Browse files Browse the repository at this point in the history
  • Loading branch information
Aivean committed Aug 21, 2023
1 parent 5ba5cbc commit db7f665
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/aivean/royalroad/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ class Args(args: Seq[String]) extends ScallopConf(args) {
}

object Args {
val urlValidationRegex = "https?://(www\\.)?royalroadl?.com/fiction/\\d+(/([^/]+))?/?"
val urlValidationRegex = "(https?://(www\\.)?royalroadl?.com/fiction/\\d+)(/.*)?"
}
9 changes: 8 additions & 1 deletion src/main/scala/com/aivean/royalroad/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ object Main extends App {
def handleFromArg[T](chaps: Seq[T], fromChap: Int): Seq[T] =
if (fromChap > 0) chaps.drop(fromChap - 1) else if (fromChap < 0) chaps.takeRight(-fromChap) else chaps

def extractFictionLink(link: String): String = {
val regex = Args.urlValidationRegex.r
link match {
case regex(url, _, _) => url
case _ => throw new IllegalArgumentException("Invalid fiction link: " + link)
}
}

val cliArgs = new Args(args)

Expand All @@ -33,7 +40,7 @@ object Main extends App {
}
}

val doc = browser.get(cliArgs.fictionLink())
val doc = browser.get(extractFictionLink(cliArgs.fictionLink()))
val title = doc >> text("title")

println("Title: " + title)
Expand Down
35 changes: 32 additions & 3 deletions src/test/scala/com/aivean/royalroad/ArgsTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aivean.royalroad

import com.aivean.royalroad.Main.extractFictionLink
import org.scalatest.FunSuite

/**
Expand All @@ -21,12 +22,40 @@ class ArgsTest extends FunSuite {
assert(s"${http}://${www}royalroad${l}.com/fiction/12345/name".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/12345/na-me".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/12345/name/".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/12345/name/chapter/123/chapter-name".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/123/name//".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/12345//".matches(Args.urlValidationRegex))
assert(s"${http}://${www}royalroad${l}.com/fiction/123//".matches(Args.urlValidationRegex))

assert(!s"${http}://${www}royalroad${l}.com/fiction/12345//".matches(Args.urlValidationRegex))
assert(!s"${http}://${www}royalroad${l}.com/fiction/".matches(Args.urlValidationRegex))
assert(!s"${http}://${www}royalroad${l}.com/fiction//".matches(Args.urlValidationRegex))
assert(!s"${http}://${www}royalroad${l}.com/fiction/123//".matches(Args.urlValidationRegex))
assert(!s"${http}://${www}royalroad${l}.com/fiction/123/name//".matches(Args.urlValidationRegex))
}
}

test("extract fiction link") {
for (
http <- List("http", "https");
www <- List("", "www.");
l <- List("", "l")
) {
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345/") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345/name") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345/na-me") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345/name/") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345/name/chapter/123/chapter-name") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/123/name//") ==
s"${http}://${www}royalroad${l}.com/fiction/123")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/12345//") ==
s"${http}://${www}royalroad${l}.com/fiction/12345")
assert(extractFictionLink(s"${http}://${www}royalroad${l}.com/fiction/123//") ==
s"${http}://${www}royalroad${l}.com/fiction/123")
}
}
}

0 comments on commit db7f665

Please sign in to comment.