Created
January 26, 2010 04:12
-
-
Save mgng/286538 to your computer and use it in GitHub Desktop.
Twitterのバックアップとるやつ
This file contains hidden or 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 // twitterのバックアップを取るとかなんとか | |
class TwitArchive | |
{ | |
private static $_user_id = 'mgng'; // ユーザid | |
private static $_count = 200; // 一度に取得する件数 | |
private static $_max_id = null; // 再帰処理でつかうmax_id格納用 | |
public static function init() | |
{ | |
$ini = array( | |
'default_charset' => 'UTF-8', | |
'date.timezone' => 'Asia/Tokyo', | |
'output_handler' => '', | |
'mbstring.language' => 'Japanese', | |
'mbstring.internal_encoding' => 'UTF-8', | |
'mbstring.encoding_translation' => 'On', | |
'mbstring.http_input' => 'UTF-8', | |
'mbstring.http_output' => 'UTF-8', | |
'display_errors' => 'On', | |
); | |
foreach($ini as $key=>$val){ | |
ini_set($key, $val); | |
} | |
set_time_limit(0); | |
} | |
public static function run() | |
{ | |
$params = http_build_query(array( | |
'count' => self::$_count, | |
'max_id' => self::$_max_id, | |
)); | |
$url = 'http://twitter.com/statuses/user_timeline/'.self::$_user_id.'.json?'.$params; | |
echo 'get : ' . $url . PHP_EOL; | |
$src = @file_get_contents($url); | |
if ($src === false) { | |
exit('file get error'); | |
} | |
$obj = @json_decode($src); | |
if ($obj === null) { | |
exit('json decode error'); | |
} | |
if (count($obj) === 0) { | |
exit('end'); | |
} | |
// 出力 | |
self::_write($src); | |
// すこしのやさしさ | |
sleep(1); | |
// 再帰 | |
self::$_max_id = $obj[count($obj)-1]->id - 1; | |
self::run(); | |
} | |
private static function _write($src){ | |
$file_path = self::$_user_id .'_'. microtime(true) . '.json'; | |
file_put_contents($file_path, $src); | |
echo 'put : ' . $file_path . PHP_EOL; | |
} | |
} | |
TwitArchive::init(); | |
TwitArchive::run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment