-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathAsyncHomework.scala
48 lines (41 loc) · 1.41 KB
/
AsyncHomework.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.evolutiongaming.bootcamp.async
import java.net.URL
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, Future}
import scala.io.Source
/** Application:
* - takes a web-page URL from arguments (args array)
* - loads the web-page body, extracts HTTP links from it
* - for all the found links, tries to fetch a server name header if there is one
* - prints all the encountered unique server name values in alphabetical order
*
* Each link processing should be done in parallel.
* Validation of arguments is not needed.
*
* Try to test it on http://google.com!
*/
object AsyncHomework extends App {
private implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
// put your code there
private def fetchPageBody(url: String): Future[String] = {
println(f"Fetching $url")
Future {
val source = Source.fromURL(url)
try {
source.mkString
} finally {
source.close()
}
}
}
private def fetchServerName(url: String): Future[Option[String]] = {
println(s"Fetching server name header for $url")
Future {
Option(new URL(url).openConnection().getHeaderField("Server"))
}
}
private def findLinkUrls(html: String): Future[List[String]] = Future {
val linkPattern = """href="(http[^"]+)"""".r
linkPattern.findAllMatchIn(html).map(m => m.group(1)).toList
}
}