progs/crawler2.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 16 Oct 2019 11:06:32 +0100
changeset 658 a18e3c027c1f
parent 550 71fc4a7a7039
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     1
// This version of the crawler only
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     2
// checks links in the "domain" urbanc
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     3
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import io.Source
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util.matching.Regex
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     6
import scala.util._
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
     8
// gets the first 10K of a web-page
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     9
def get_page(url: String) : String = {
428
a47c4227a0c6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 427
diff changeset
    10
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString). 
a47c4227a0c6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 427
diff changeset
    11
    getOrElse { println(s"  Problem with: $url"); ""}
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
    12
}
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
    14
// regexes for URLs and "my" domain
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 116
diff changeset
    15
val http_pattern = """"https?://[^"]*"""".r
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    16
val my_urls = """urban""".r       /*@\label{myurlline}@*/
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    17
//val my_urls = """kcl.ac.uk""".r 
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def unquote(s: String) = s.drop(1).dropRight(1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
    21
def get_all_URLs(page: String) : Set[String] = 
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
    22
  http_pattern.findAllIn(page).map(unquote).toSet
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def crawl(url: String, n: Int) : Unit = {
427
546f2090ce12 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 421
diff changeset
    25
  if (n == 0) ()                   /*@\label{changestartline}@*/
116
010ae7288327 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
    26
  else if (my_urls.findFirstIn(url) == None) { 
010ae7288327 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
    27
    println(s"Visiting: $n $url")
010ae7288327 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
    28
    get_page(url); () 
427
546f2090ce12 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 421
diff changeset
    29
  }                                /*@\label{changeendline}@*/
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  else {
95
dbe49327b6c5 added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    31
    println(s"Visiting: $n $url")
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    32
    for (u <- get_all_URLs(get_page(url)).par) crawl(u, n - 1)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 116
diff changeset
    36
// starting URL for the crawler
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    37
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    38
//val startURL = """https://nms.kcl.ac.uk/christian.urban/bsc-projects-17.html"""
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    39
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
    40
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
    41
// can now deal with depth 3 and beyond
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    42
crawl(startURL, 3)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
    44