5 use Services\ISMOMatcher
;
10 class SMOMatcher
implements ISMOMatcher
14 public function match($title, $filesize)
16 if(!$this->scrapeSmo()) return null
;
18 $most_likely = array('confidence' => 0);
19 foreach($this->_records
as $rowNum => $row) {
20 $cells = $row->getElementsByTagName('td');
23 $candidate = $this->cellToCandidateArray($cells);
24 $this->setCandidateSimilarty($title, $filesize, $candidate);
25 $most_likely = $candidate['confidence'] > $most_likely['confidence'] ?
$candidate : $most_likely;
32 private function scrapeSmo()
34 if($this->_records
) return true
;
36 $c = curl_init('http://stepmaniaonline.net/index.php?page=downloads');
37 curl_setopt($c, CURLOPT_RETURNTRANSFER
, true
);
38 //curl_setopt(... other options you want...)
40 $html = curl_exec($c);
42 if (curl_error($c)) return false
;
44 // Get the status code
45 $status = curl_getinfo($c, CURLINFO_HTTP_CODE
);
48 //$html = file_get_contents('smo.html');
49 $dom = new DOMDocument();
50 @$dom->loadHTML($html);
51 $xpath = new DOMXPath($dom);
53 $table = $xpath->query("/html/body/div[@class='container']/div[@class='mid']/div[@class='content']/div[3]/div[@class='blockcontent']/table")->item(0);
54 $this->_records
= $table->getElementsByTagName("tr");
59 private function cellToCandidateArray(DOMNodeList
$cells)
62 'href' => $cells->item(0)->getElementsByTagName('a')->item(0)->getAttribute('href'),
63 'title' => $cells->item(0)->nodeValue
,
64 'filesize' => strpos($cells->item(1)->nodeValue
, 'Gb') === true ?
($cells->item(1)->nodeValue
)*1024*1024*1024 : ($cells->item(1)->nodeValue
)*1024*1024
68 private function setCandidateSimilarty($title, $filesize, array &$candidate)
70 similar_text($title, $candidate['title'], $percent);
71 $r = $candidate['filesize'] > $filesize ?
$filesize/$candidate['filesize'] : $candidate['filesize']/$filesize;
72 $candidate['confidence'] = $percent*$r;