| Uutiset | Koodikirjasto | Wiki | Keskustelut | FAQ | Info |
AMGRiptheril 06.07.04 22:51 Luokka, joka hakee artistin (levyhaku tulossa ehkä joskus) tietoja allmusic.com-sivustolta.
<?php /* AMGRip * Hakee artistin (levyhaku tulossa ehkä joskus) tietoja allmusic.com-sivustolta * HUOM: Allmusic.com:in tiedot ovat tekijänoikeussuojattuja, joten haetut * tiedot vain yksityiseen käyttöön! * * Päivittelen mureakuhan koodikijastoon sitä mukaan, kun/jos tulee lisää * ominaisuuksia/bugeja * * Allmusic.comin käyttöliittymä muuttui, joten tämä skripti ei enää toimi. Päivitys tullee. */ error_reporting(E_ALL); /* AMGRip luokan ainoa (tarkoitettu) käyttötapa tällä hetkellä on luoda olio ja * käyttää getArtistInfo-metodia, joka palauttaa artistin tiedot taulukossa */ class AMGRip { // Public /* metodi getArtistInfo * Parametrit: * name: Artistin nimi * id: Viitataan listaan, jossa on haun tulokset * Palautusarvo: * Taulukko, jossa seuraavat artistin tiedot: * name: Artistin nimi (haetaan sivulta, eli voi olla eri * kuin haettu nimi * genre: Artistin edustama genre * img: URL artistin kuvaan * bio: Artistin biografia * choices: Haun tuottamat vaihtoehdot * Taulukon elementti on false, ellei kyseistä tietoa löydy * Palauttaa false, jos tietoja ei saada haettua (kts. getErrors) * * TODO: Lisää tietoja */ function getArtistInfo($name, $id = 0) { $info = $this->_getArtistPage($name, $id); $data = $info[0]; if($this->err($data, "Couldn't find artist page")) return false; $ret['name'] = $this->_parseArtistName($data); $ret['genre'] = $this->_parseArtistGenre($data); $ret['img'] = $this->_parseArtistImageURL($data); $ret['bio'] = $this->_parseArtistBio($data); $ret['choices'] = $info[1]; return $ret; } /* Metodi getErrors * Palauttaa "error stackin", eli taulukon, johon virheilmoitukset on * tallennettu * * TODO: Myös numeeriset virheilmoitukset * TODO: Enemmän pinonhallintafunktioita (esim. flush), odotellessa voi * käyttää suoraan $_errors-attribuuttia */ function getErrors() { global $_errors; $tmp = $_errors; $_errors = array(); return $tmp; } // Private var $_errors; function _pushError($error) { global $_errors; $_errors[] = $error; } function err($cond, $msg) { if(!$cond) { $this->_pushError($msg); return true; } return false; } /* TODO: Geneerinen parsintametodi, joka voisi ottaa esim. regexpin * tai vaihtoehtoisesti callback-funktion, sekä taulukon, johon * tiedot tallennetaan */ function _parseArtistBio($data) { $n = preg_match('@<div id="artbio" .*?>(.*?)</div>@', $data, $m); if($this->err($n, "Coudln't find artist genre")) return false; $ret = strip_tags($m[1]); return $ret; } function _parseArtistGenre($data) { $n = preg_match('@Genres </td><TD class="co1" colspan=3 WIDTH="487"><a href="javascript:z\(\'.*?\'\)">(.*?)</a>@', $data, $m); if($this->err($n, "Coudln't find artist genre")) return false; return $m[1]; } function _parseArtistName($data) { $n = preg_match( '@<div id="iL" style="position:absolute;"></div><TR><TD width=610 colspan=4 align=center><font size=\+2><B>(.*?)</b>@', $data, $m); if($this->err($n, "Couldn't find artist name")) return false; return $m[1]; } function _parseArtistImageURL($data) { if($this->err(preg_match( "@<span id=\"artpic\"><img src=(.+?) width=@", $data, $m), 'Artist image url not found')) return false; return $m[1]; } function _getArtistPage($name, $id = 0) { if($this->err( $data = $this->_search($name), "Couldn't get artist page")) return false; switch($this->_detectPageType($data)) { case "artistpage": $ret[0] = $data; return $ret; case "artistlist": $tmp = $this->_parseArtistList($data); $ret[0] = $this->_getPage($tmp[$id]['url']); $ret[1] = $tmp; return $ret; default: $this->_pushError("Couldn't find artist page"); return false; } } function _search($query, $type = 1) { $query = str_replace(' ', '|', $query); $content = $this->_getPage("http://www.allmusic.com/cg/amg.dll?p=amg&sql=$type$query~C"); $this->err($content, "Couldn't retive content"); return $content; } function _parseArtistList($data, $hits = 1) { if($this->err($this->_detectPageType($data) == "artistlist", 'Page not artist list in parseArtistList()')) return false; $ret = array(); if($this->err(preg_match_all( "@<TD width=335>[<B>]{0,3}<a href=\"javascript:z\('(.+?)\'\)\">(.*?)</a>[<B/>]{0,4}@", $data, $matches), "Couldn't match artist in list")) return false; $n = count($matches[1]); for($i = 0; $i < $n; $i++) { $ret[$i]['name'] = $matches[2][$i]; $ret[$i]['url'] = "http://allmusic.com/cg/amg.dll?p=amg&sql=". $matches[1][$i]; } return $ret; } function _detectPageType($data) { if(strpos($data, "hbio1.gif")) return "artistpage"; if(strpos($data, "artists with names like")) return "artistlist"; $this->_pushError('Unknown page type'); return false; } function _getPage($url) { $this->err($ret = file_get_contents($url), "Couldn't retive content"); return $ret; } } ?> |
![]() Haku
|