Created
December 11, 2010 05:27
-
-
Save riaf/737172 to your computer and use it in GitHub Desktop.
Twitter 検索
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($argv[1])) { | |
$word = rawurlencode($argv[1]); | |
} else { | |
echo <<<USAGE | |
Usage: | |
php {$argv[0]} SEARCH_WORD [ > OUTPUT ] | |
USAGE; | |
exit(1); | |
} | |
$url = function ($word, $page=1) { | |
return sprintf('http://pcod.no-ip.org/yats/search?query=%s&lang=ja&rss&page=%d', $word, $page); | |
}; | |
$context = stream_context_create(array( | |
'http' => array( | |
'method' => 'GET', | |
'header' => 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', | |
), | |
)); | |
$stdout = fopen('php://stdout', 'w'); | |
$stderr = fopen('php://stderr', 'w'); | |
$page = 1; | |
while (true) { | |
fwrite($stderr, "fetching rss page $page ...\n"); | |
$xml = simplexml_load_string(file_get_contents($url($word, $page++), false, $context)); | |
if (empty($xml->entry)) { | |
fwrite($stderr, "entries is empty. abort."); | |
break; | |
} else { | |
foreach ($xml->entry as $entry) { | |
fputcsv($stdout, array( | |
$entry->title, | |
$entry->link->attributes()->href, | |
date('Y-m-d H:i:s', strtotime($entry->updated)), | |
$entry->author->name, | |
$entry->id, | |
$entry->summary, | |
)); | |
} | |
} | |
} | |
fclose($stdout); | |
fclose($stderr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment