progs/app4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 330 0806e45d873c
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
     1
val http_pattern = """"https?://[^"]*"""".r
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
val email_pattern = 
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
  """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
     5
def print_str(s: String) = 
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
     6
  if (s == "") () else println(s)
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
     7
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def crawl(url: String, n: Int) : Unit = {
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  if (n == 0) ()
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  else {
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    11
    println(s"Visiting: $n $url")
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    val page = get_page(url)
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
    13
    print_str(email_pattern.findAllIn(page).mkString("\n"))
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 97
diff changeset
    14
    for (u <- get_all_URLs(page).par) crawl(u, n - 1)
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  }
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}