Example #1
0
 public function boardAction()
 {
     list(, , $board) = explode('/', $this->getURI());
     if (!RankData::search(array('board' => $board))->first()) {
         return $this->redirect('/');
     }
     $this->view->board = $board;
 }
Example #2
0
 public static function getData($board, $period, $time)
 {
     if (0 == $period) {
         // year: format: YYYY, 一天一筆最高的
         $max_time = mktime(0, 0, 0, 1, 1, $time + 1);
         $min_time = mktime(0, 0, 0, 1, 1, $time);
     } elseif (1 == $period) {
         // month: format: YYYYMM, 一天12筆
         list($year, $month) = sscanf($time, "%4d%2d");
         $min_time = mktime(0, 0, 0, $month, 1, $year);
         $max_time = strtotime('+1 month', $min_time);
     } elseif (3 == $period) {
         // daily: format: YYYYMMDD, 全部
         list($year, $month, $day) = sscanf($time, "%4d%2d%2d");
         $min_time = mktime(1, 1, 1, $month, $day, $year);
         $max_time = $min_time + 86400;
     }
     $last_update = RankData::search(array('board' => $board))->search("`time` < {$max_time}")->order("`time` DESC")->first()->time;
     if (!$last_update) {
         return array();
     }
     if ($data = CacheData::search(array("board" => $board, "period" => $period, "time" => $time, "updated_at" => $last_update))->first()) {
         return json_decode($data->data);
     }
     if (0 == $period) {
         $datas = array();
         for ($t = $min_time; $t < $max_time; $t += 86400) {
             $max_data = RankData::search(array('board' => $board))->search("`time` >= {$t} and `time` < {$t} + 86400")->max('count');
             if (!$max_data) {
                 continue;
             }
             $datas[] = array($max_data->time, $max_data->count);
         }
     }
     try {
         CacheData::insert(array('board' => $board, 'period' => $period, 'time' => $time, 'updated_at' => $last_update, 'data' => json_encode($datas, JSON_UNESCAPED_UNICODE)));
     } catch (Pix_Table_DuplicateException $e) {
         CacheData::search(array('board' => $board, 'period' => $period, 'time' => $time))->update(array('updated_at' => $last_update, 'data' => json_encode($datas, JSON_UNESCAPED_UNICODE)));
     }
     return $datas;
 }
Example #3
0
$last_time = intval(KeyValue::get('snapshot_at'));
// dump count
while (true) {
    $min_time = RankData::search("`time` > {$last_time}")->min('time')->time;
    if (!$min_time) {
        break;
    }
    $month_start = mktime(0, 0, 0, date('m', $min_time), 1, date('Y', $min_time));
    $month_end = strtotime('+1 month', $month_start);
    $filename = 'ptthot-' . date('Ym', $month_start) . '.csv.gz';
    $temp = tmpfile();
    $meta_data = stream_get_meta_data($temp);
    $tmp_filename = $meta_data['uri'];
    $stream = gzopen($tmp_filename, 'w');
    fwrite($stream, "#board,timestamp,count\n");
    foreach (RankData::search("`time` >= {$month_start} AND `time` < {$month_end}")->order(array('board', 'time'))->volumemode(10000) as $rankdata) {
        $last_time = max($last_time, $rankdata->time);
        fwrite($stream, "{$rankdata->board},{$rankdata->time},{$rankdata->count}\n");
    }
    fclose($stream);
    DropboxLib::putFile($tmp_filename, $filename);
    fclose($temp);
    KeyValue::set('snapshot_at', $last_time);
}
//dump titlehistory
$filename = 'ptthot-title.csv.gz';
$temp = tmpfile();
$meta_data = stream_get_meta_data($temp);
$tmp_filename = $meta_data['uri'];
$stream = gzopen($tmp_filename, 'w');
fwrite($stream, "#board,timestamp,title\n");
Example #4
0
curl_setopt($curl, CURLOPT_URL, 'http://www.ptt.cc/hotboard.html');
if (!($content = curl_exec($curl))) {
    exit;
}
$content = iconv('Big5', 'UTF-8//IGNORE', $content);
$content = preg_replace('/([\\x{0fffe}-\\x{10ffff}]+)/u', '', $content);
if (!preg_match('#\\(本文約每小時更新,最後更新時間 ([^)]*)#', $content, $matches)) {
    throw new Exception('找不到時間');
}
if (!($time = strtotime($matches[1]))) {
    throw new Exception('找不到時間');
}
$content = preg_replace('#<td[^>]*>#', '', $content);
$content = preg_replace('#<a [^>]*>#', '', $content);
$content = preg_replace('#</[^>]+>#', '', $content);
preg_match_all("#人氣:([0-9]*)\n([^\\s]*)\n(.*)#m", $content, $matches);
$latest_data = array();
foreach ($matches[0] as $id => $data) {
    try {
        $board = strval($matches[2][$id]);
        $count = intval($matches[1][$id]);
        $name = strval($matches[3][$id]);
        RankData::insert(array('time' => $time, 'board' => $board, 'count' => $count));
    } catch (Pix_Table_DuplicateException $e) {
        RankData::search(array('time' => $time, 'board' => $board))->update(array('count' => $count));
    }
    $latest_data[] = array($board, $count, $name);
    TitleHistory::updateTitle($board, $time, $name);
}
KeyValue::set('latest_hot', json_encode(array('time' => $time, 'boards' => $latest_data)));
echo '完成: ' . date('c', $time) . "\n";