-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetgraphdata.php
executable file
·184 lines (147 loc) · 5.23 KB
/
getgraphdata.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
//
// This code scrapes Blekko's HTML search result pages for information about a given
// domain
//
// By Pete Warden <[email protected]>, freely reusable, see http://petewarden.typepad.com for more
require_once('parallelcurl.php');
define('INBOUND_LINK_RE',
'@'.
'\s*<tr>\s*'.
'\s*<td\s+class="number">[^<]+</td>\s*'.
'\s*<td class="urlInfo" title="[^"]+">\s*'.
'\s*<a href="[^"]+">([^<]+)</a>\s*'.
'\s*</td>\s*'.
'\s*<td\s+class="number" title="[^"]+">\s*'.
'\s*(\S+)\s*'.
'\s*</td>\s*'.
'\s*<td\s+class="number"\s+title="[^"]+"><a href="javascript:;" onclick="[^"]+">(\S+)</a></td>\s*'.
'@');
define('DOMAIN_STATS_RE',
'@'.
'\s*<li>\s*'.
'\s*<a href="javascript:;" onclick="[^"]+" title="see host stats" class="linkNr">(\S+)</a>\s*'.
'\s*hostrank\s*'.
'\s*</li>\s*'.
'\s*<li>\s*'.
'\s*<a href="javascript:;" class="linkNr" onclick="[^"]+">(\S+)</a>\s*'.
'\s*inbound links\s*'.
'\s*</li>\s*'.
'\s*<li>\s*'.
'\s*<a href="javascript:;" onclick="[^"]+" title="see outbound links" class="linkNr">(\S+)</a>\s*'.
'@');
$g_domain_info = array();
$g_retry_count = 0;
// This function gets called back once the main domain's result page has been fetched
function on_main_request_done($content, $url, $ch, $domain) {
global $g_domain_info;
global $g_parallel_curl;
global $g_retry_count;
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode !== 200)
{
if (($httpcode>=500)&&($httpcode<600)&&($g_retry_count<100))
{
error_log("Temporary error $httpcode for '$url' - pausing and retrying\n");
sleep(2);
$g_parallel_curl->startRequest($url, 'on_main_request_done', $domain);
$g_retry_count += 1;
}
else
{
error_log("Fetch error $httpcode for '$url'\n");
}
return;
}
$g_domain_info = scrape_domain_info_from_html($content);
$g_domain_info['domain'] = $domain;
$inbound_links = $g_domain_info['inbound_links'];
foreach ($inbound_links as $link_domain => $inbound_link)
{
$page_rank = $inbound_link['page_rank'];
$link_count = $inbound_link['link_count'];
$g_parallel_curl->startRequest(blekko_seo_url($link_domain), 'on_link_request_done', $link_domain);
}
}
function on_link_request_done($content, $url, $ch, $link_domain) {
global $g_domain_info;
global $g_parallel_curl;
global $g_retry_count;
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode !== 200) {
if (($httpcode>=500)&&($httpcode<600)&&($g_retry_count<100))
{
error_log("Temporary error $httpcode for '$url' - pausing and retrying\n");
sleep(2);
$g_parallel_curl->startRequest($url, 'on_link_request_done', $link_domain);
$g_retry_count += 1;
}
else
{
error_log("Fetch error $httpcode for '$url'\n");
}
return;
}
$g_domain_info['inbound_links'][$link_domain]['domain_info'] =
scrape_domain_info_from_html($content);
}
// Takes Blekko's raw HTML and extracts the statistics we're interested in
function scrape_domain_info_from_html($content)
{
$content = str_replace("\n", ' ', $content);
preg_match_all(INBOUND_LINK_RE, $content, $inbound_matches, PREG_SET_ORDER);
preg_match(DOMAIN_STATS_RE, $content, $domain_stats);
if (empty($domain_stats))
die("Couldn't find domain stats on page\n");
$main_page_rank = pete_as_numeric($domain_stats[1]);
$inbound_link_count = pete_as_numeric($domain_stats[2]);
$site_page_count = pete_as_numeric($domain_stats[3]);
$inbound_links = array();
foreach ($inbound_matches as $inbound_match)
{
$domain = $inbound_match[1];
$page_rank = pete_as_numeric($inbound_match[2]);
$link_count = pete_as_numeric($inbound_match[3]);
$inbound_links[$domain] = array(
'page_rank' => $page_rank,
'link_count' => $link_count,
);
}
$result = array(
'page_rank' => $main_page_rank,
'inbound_link_count' => $inbound_link_count,
'page_count' => $site_page_count,
'inbound_links' => $inbound_links,
);
return $result;
}
function blekko_seo_url($domain)
{
return 'http://blekko.com/ws/'.urlencode($domain).'+/seo';
}
// Take a string with extra cruft in it, and attempt to strip it out and return a number
function pete_as_numeric($input_value)
{
$clean_value = trim($input_value);
$clean_value = str_replace(',', '', $clean_value);
$clean_value = str_replace('%', '', $clean_value);
$clean_value = str_replace('+', '', $clean_value);
$clean_value = str_replace('$', '', $clean_value);
if (is_numeric($clean_value))
$result= $clean_value;
else
$result = null;
return $result;
}
set_time_limit(0);
$domain = $_GET['domain'];
$curl_options = array(
CURLOPT_USERAGENT, 'PageRankGraph - contact [email protected]',
);
$max_requests = 3;
$main_url = blekko_seo_url($domain);
$g_parallel_curl = new ParallelCurl($max_requests, $curl_options);
$g_parallel_curl->startRequest($main_url, 'on_main_request_done', $domain);
$g_parallel_curl->finishAllRequests();
print json_encode($g_domain_info);
?>