progs/crawler.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 26 Apr 2024 17:36:41 +0100
changeset 487 efad9725dfd8
parent 322 755d165633ec
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
322
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A Web-Crawler
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//================
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// call parallel version with
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
//
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// scala -cp scala-parallel-collections_2.13-0.2.0.jar crawler.scala 
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
import io.Source
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
import scala.util._
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
import scala.collection.parallel.CollectionConverters._
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// the idea is to look for links using the
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// regular expression "https?://[^"]*" and for
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// email addresses using yet another regex.
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// gets the first 10K of a web-page
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def get_page(url: String) : String = {
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    getOrElse { println(s" Problem with: $url"); ""}
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
}
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// regex for URLs and emails
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
val http_pattern = """"https?://[^"]*"""".r
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//  val s = "foo bla christian@kcl.ac.uk 1234567"
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//  email_pattern.findAllIn(s).toList
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// drops the first and last character from a string
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def unquote(s: String) = s.drop(1).dropRight(1)
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
def get_all_URLs(page: String): Set[String] = 
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  http_pattern.findAllIn(page).map(unquote).toSet
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// a naive version of crawl - searches until a given depth,
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// visits pages potentially more than once
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
def crawl(url: String, n: Int) : Unit = {
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  if (n == 0) ()
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  else {
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
    println(s"  Visiting: $n $url")
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    val page = get_page(url)
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    for (u <- get_all_URLs(get_page(url)).par) crawl(u, n - 1)
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  }
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
}
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// some starting URLs for the crawler
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//crawl(startURL, 2)
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// a primitive email harvester
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
def emails(url: String, n: Int) : Set[String] = {
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  if (n == 0) Set()
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  else {
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
    println(s"  Visiting: $n $url")
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
    val page = get_page(url)
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    val new_emails = email_pattern.findAllIn(page).toSet
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
    new_emails ++ (for (u <- get_all_URLs(page).par) yield emails(u, n - 1)).flatten
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  }
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
println(emails(startURL, 3))
755d165633ec updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65