-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.php
159 lines (133 loc) · 4 KB
/
crawl.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
include("config.php");
include("classes/DomDocumentParser.php");
$alreadyCrawled = array();
$crawling = array();
$alreadyFoundImages = array();
function linkExists($url)
{
global $con;
$query = $con->prepare("SELECT * FROM sites WHERE url = :url");
$query->bindParam(":url", $url);
$query->execute();
return $query->rowCount() != 0;
}
function insertLink($url, $title, $description ,$keywords)
{
global $con;
$query = $con->prepare("INSERT INTO sites(url,title,description,keywords)
VALUES(:url,:title,:description,:keywords)");
$query->bindParam(":url", $url);
$query->bindParam(":title", $title);
$query->bindParam(":description", $description);
$query->bindParam(":keywords", $keywords);
return $query->execute();
}
function insertImage($url, $src, $alt ,$title)
{
global $con;
$query = $con->prepare("INSERT INTO images(siteurl,imageurl,alt,title)
VALUES(:siteurl,:imageurl,:alt,:title)");
$query->bindParam(":siteurl", $url);
$query->bindParam(":imageurl", $src);
$query->bindParam(":alt", $alt);
$query->bindParam(":title", $title);
$query->execute();
}
function createLink($src, $url)
{
$scheme = parse_url($url)["scheme"]; //http
$host = parse_url($url)["host"]; //www.example.com
if(substr($src,0,2) == "//")
$src = $scheme. ":" . $src;
else if(substr($src,0,1) == "/")
$src = $scheme . "://" . $host . $src;
else if(substr($src,0,2) == "./")
$src = $scheme. "://" . $host . dirname($parseurl($url)["path"]) . substr($src,1);
else if(substr($src,0,3) == "../")
$src = $scheme. "://" . $host . "/" .$src;
else if(substr($src,0,5) !="https" && substr($src,0,4) !="http")
$src = $scheme. "://" . $host . "/" .$src;
return $src;
}
function getDetails($url)
{
global $alreadyFoundImages;
$parser = new DomDocumentParser($url);
$titleArray = $parser->getTitleTags();
$title = $titleArray->item(0)->nodeValue;
if(sizeof($titleArray) ==0 || $titleArray->item(0) == NULL)
return;
$title = str_replace("\n","",$title);
if($title == "")
return;
$description = "";
$keywords = "";
$metasArray = $parser->getMetatags();
foreach ($metasArray as $meta)
{
if($meta->getAttribute("name") == "description")
{
$description = $meta->getAttribute("content");
}
if($meta->getAttribute("name") == "keywords")
{
$keywords = $meta->getAttribute("content");
}
}
$description = str_replace("\n","",$description);
$keywords = str_replace("\n","",$keywords);
if(linkExists($url))
echo "url already exists: $url<br>";
else if(insertLink($url,$title,$description,$keywords))
echo "success: $url<br>";
else
echo "failed: $url<br>";
$imageArray = $parser->getImages();
foreach($imageArray as $image)
{
$src = $image->getAttribute("src");
$alt = $image->getAttribute("alt");
$title = $image->getAttribute("title");
if(!$title && !$alt)
continue;
$src = createLink($src,$url);
if(!in_array($src, $alreadyFoundImages))
{
$alreadyFoundImages[]=$src;
insertImage($url,$src,$alt,$title);
}
}
}
function followLinks($url)
{
global $alreadyCrawled;
global $crawling;
$parser = new DomDocumentParser($url);
$linksLists = $parser->getLinks();
foreach ($linksLists as $link)
{
$href = $link->getAttribute("href");
if(Strpos($href,"#") !== false||substr($href,0,11)=="javascript:")
continue;
$href = createLink($href,$url);
if(!in_array($href, $alreadyCrawled))
{
$alreadyCrawled[] = $href;
$crawling[] = $href;
getDetails($href);
}
else
return;
echo $href."<br>";
//see for crawled links
}
array_shift($crawling);
foreach($crawling as $site)
{
followLinks($site);
}
}
$startUrl="http://www.jiit.ac.in";
followLinks($startUrl);
?>