Exemple #1
0
function download($url)
{
    $cache_file = url_cache_file($url);
    if (is_cache_valid($cache_file)) {
        return file_get_contents($cache_file);
    }
    $curl_options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3);
    $ch = curl_init($url);
    curl_setopt_array($ch, $curl_options);
    $data = curl_exec($ch);
    file_put_contents($cache_file, $data);
    return $data;
}
 /** !Route GET */
 function index()
 {
     $page = isset($this->request->get['page']) ? (int) $this->request->get['page'] : 1;
     $this->page = $page;
     $url = "http://macthemes2.net/forum/viewforum.php?id=24&p={$page}";
     if (!is_cache_valid(url_cache_file($url))) {
         $html = download($url);
         $doc = phpQuery::newDocument($html);
         $rows = $doc['#punviewforum table tr > td.tcl']->parent();
         $viewtopic_length = strlen('viewtopic.php?id=');
         foreach ($rows as $row) {
             $row = pq($row);
             $a = $row['a:first'][0];
             $name = trim($a->text());
             if (preg_match('/^\\[(theme|contest|contest theme)\\]/iu', $name)) {
                 $id = substr($a->attr('href'), $viewtopic_length);
                 if (!Make::a('Theme')->like('topic_id', $id)->exists()) {
                     $artist = $row['td.tc4'][0]->text();
                     $byPos = strrpos($name, ' by ');
                     if ($byPos !== false) {
                         $name = substr($name, 0, $byPos);
                     }
                     $name = strip_tag_prefix($name);
                     $theme = new Theme();
                     $theme->topic_id = $id;
                     $theme->name = $name;
                     $theme->artist = $artist;
                     $theme->save();
                 }
             }
         }
     }
     $this->themeSet = $this->theme->all();
     if (isset($this->request->get['flash'])) {
         $this->flash = $this->request->get['flash'];
     }
 }