Files can now have mirrors. Implemented SMOMatcher service to attempt to automaticall...
[rock.divinelegy.git] / Services / SMOMatcher.php
1 <?php
2
3 namespace Services;
4
5 use Services\ISMOMatcher;
6 use DOMDocument;
7 use DOMXPath;
8 use DOMNodeList;
9
10 class SMOMatcher implements ISMOMatcher
11 {
12 private $_records;
13
14 public function match($title, $filesize)
15 {
16 if(!$this->scrapeSmo()) return null;
17
18 $most_likely = array('confidence' => 0);
19 foreach($this->_records as $rowNum => $row) {
20 $cells = $row->getElementsByTagName('td');
21
22 if($rowNum > 0) {
23 $candidate = $this->cellToCandidateArray($cells);
24 $this->setCandidateSimilarty($title, $filesize, $candidate);
25 $most_likely = $candidate['confidence'] > $most_likely['confidence'] ? $candidate : $most_likely;
26 }
27 }
28
29 return $most_likely;
30 }
31
32 private function scrapeSmo()
33 {
34 if($this->_records) return true;
35
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...)
39
40 $html = curl_exec($c);
41
42 if (curl_error($c)) return false;
43
44 // Get the status code
45 $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
46
47 curl_close($c);
48 //$html = file_get_contents('smo.html');
49 $dom = new DOMDocument();
50 @$dom->loadHTML($html);
51 $xpath = new DOMXPath($dom);
52
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");
55
56 return true;
57 }
58
59 private function cellToCandidateArray(DOMNodeList $cells)
60 {
61 return array(
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
65 );
66 }
67
68 private function setCandidateSimilarty($title, $filesize, array &$candidate)
69 {
70 similar_text($title, $candidate['title'], $percent);
71 $r = $candidate['filesize'] > $filesize ? $filesize/$candidate['filesize'] : $candidate['filesize']/$filesize;
72 $candidate['confidence'] = $percent*$r;
73 }
74 }