Example #1
0
 public static function saveAnswer($base_url, $username, $answer_link_list)
 {
     foreach ($answer_link_list as $url) {
         if (preg_match('%^/question/(\\d+)/answer/(\\d+)%', $url, $matches)) {
             $qid = $matches[1];
             $aid = $matches[2];
         } else {
             echo "{$url} not good\n";
             exit(1);
         }
         $url = $base_url . $url;
         echo "\t{$url}";
         $t = microtime(true);
         list($code, $content) = odie_get($url);
         echo "\t[{$code}]";
         if ($code != 200) {
             // fail fast
             echo "\tfail\n";
             slog("{$url} [{$code}] error");
             $success_ratio = get_average(0, 'success_ratio');
             continue;
         } else {
             $success_ratio = get_average(1, 'success_ratio');
         }
         $t = intval((microtime(true) - $t) * 1000);
         $avg = intval(get_average($t));
         echo "\t{$t} ms\n";
         if (empty($content)) {
             echo "content is empty\n";
             slog("{$url} [{$code}] empty");
             return false;
         }
         list($question, $descript, $content, $vote) = parse_answer_pure($content);
         slog("{$url} [{$code}] ^{$vote}\t{$question}");
         Question::saveQuestion($qid, $question, $descript);
         Answer::_saveAnswer($aid, $qid, $username, $content, $vote);
     }
     if (isset($success_ratio) && isset($avg)) {
         $success_ratio = intval($success_ratio * 100) . '%';
         echo "\tAvg: {$avg} ms\tsuccess_ratio: {$success_ratio}\n";
     }
 }
Example #2
0
function save_answer_to_db($base_url, $username, $answer_link_list)
{
    global $pdo;
    foreach ($answer_link_list as $url) {
        echo "\t{$base_url}{$url}";
        if (preg_match('%^/question/(\\d+)/answer/(\\d+)%', $url, $matches)) {
            $qid = $matches[1];
            $aid = $matches[2];
        } else {
            echo "{$url} not good\n";
            exit(1);
        }
        $url = $base_url . $url;
        list($code, $content) = odie_get($url);
        echo "\t{$code}\n";
        // 自动重刷
        $i = 0;
        while ($code != 200) {
            list($code, $content) = odie_get($url);
            echo "\t{$code}\n";
            if ($i > 5) {
                echo 'can not fetch', "\n";
                return false;
            }
            $i++;
        }
        if (empty($content)) {
            echo "content is empty\n";
            return false;
        }
        list($question, $descript, $content, $vote) = parse_answer_pure($content);
        echo "\t^{$vote}\t{$question}\n";
        $stmt = $pdo->prepare('INSERT INTO question (id, title, description) VALUES (?,?,?) ON DUPLICATE KEY UPDATE title=?,description=?');
        if (!$stmt->execute(array($qid, $question, $descript, $question, $descript))) {
            print_r($stmt->errorInfo());
        }
        $stmt = $pdo->prepare('INSERT INTO answer (id, q_id, user, text, vote) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE text=?, vote=?');
        if (!$stmt->execute(array($aid, $qid, $username, $content, $vote, $content, $vote))) {
            print_r($stmt->errorInfo());
        }
    }
}