* Created by PhpStorm. * User: kmasaya * Date: 30/11/2015 * Time: 19:07 */ if (empty($argv[1])) { exit('Usage:php cron.php screen_name' . PHP_EOL); } $screen_name = $argv[1]; if (!empty($argv[2])) { $rcount = $argv[2]; } else { $rcount = 3; } require '../config.php'; require 'TwistOAuth.phar'; $to = new TwistOAuth(CK, CS, AT, ATS); $count = '5000'; //取得数 //$rcount = '20'; //リムーブ数 /* Follow取得 */ $follow = $to->get('friends/ids', array('screen_name' => $screen_name, 'count' => $count)); /* FollowerID取得 */ $follower = $to->get('followers/ids', array('screen_name' => $screen_name, 'count' => $count)); $f_difference = array_diff($follow->ids, $follower->ids); $remove = array_slice($f_difference, 0, $rcount); foreach ($remove as $rem) { $to->post('friendships/destroy', array('user_id' => $rem)); print $rem . ' '; sleep(3); }
* Date: 2015/12/02 * Time: 13:05 */ if (empty($argv[1])) { exit('Usage:php cron.php screen_name' . PHP_EOL); } $screen_name = $argv[1]; if (!empty($argv[2])) { $fcount = $argv[2]; } else { $fcount = 3; } require '../config.php'; require 'TwistOAuth.phar'; $to = new TwistOAuth(CK, CS, AT, ATS); $count = '5000'; //取得数 $myscreen_name = 'HaloDsny'; //自分のTwitter名 /* MyFollowing取得 */ $mefollowing = $to->get('friends/ids', array('screen_name' => $myscreen_name, 'count' => $count)); /* Target FollowerID取得 */ $target = $to->get('followers/ids', array('screen_name' => $screen_name, 'count' => $count)); $f_difference = array_diff($target->ids, $mefollowing->ids); //自フォローとターゲットフォロワー差分 $following = array_slice($f_difference, 100, $fcount); foreach ($following as $fol) { $to->post('friendships/create', array('user_id' => $fol)); print $fol . ' '; sleep(5); }
if (!is_readable($path)) { switch (true) { case !($local = @fopen($path, 'wb')): case !($remote = @fopen('https://raw.githubusercontent.com/mpyw/TwistOAuth/master/build/TwistOAuth.phar', 'rb')): case !@stream_copy_to_stream($remote, $local): $error = error_get_last(); throw new \RuntimeException($error['message']); } } require $path; // Create TwistOAuth $to = new \TwistOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); // Create DateTimeZone $tz = new \DateTimeZone($timezone); // Get home timeline $statuses = $to->get('statuses/home_timeline', array('count' => 200)); } catch (\Exception $e) { header('Content-Type: text/plain; charset=UTF-8', true, 500); echo $e->getMessage() . "\n"; exit; } header('Content-Type: text/html; charset=UTF-8'); ?> <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <?php foreach ($statuses as $i => $status) {
<?php require_once 'lib/TwistOAuth.php'; require_once 'lib/TwistException.php'; define('CONSUMER_KEY', '取得したキー'); define('CONSUMER_SECRET', '取得したキー'); define('ACCESS_TOKEN', '取得したキー'); define('ACCESS_TOKEN_SECRET', '取得したキー'); try { $to = new TwistOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); } catch (TwistException $e) { $error = $e->getMessage(); echo $error . PHP_EOL; } // parameter $params = array("count" => 5); // end point $url = "https://api.twitter.com/1.1/statuses/home_timeline.json"; // response $res = $to->get($url, $params); foreach ($res as $tweet) { echo $tweet->text . "\n"; }
$Cache = new Cache($TwistOAuth); $filter = []; $user = filter_input(INPUT_GET, "user"); if (!empty($user) && is_string($user)) { foreach (explode(",", $user) as $sn) { $filter[] = $sn; } } $user_ids = []; $names = []; $create_cache = []; foreach ($SaveTweet->tweetsList() as $row) { $user_ids[] = $row["user_id"]; } if (count($user_ids) !== 0) { $statuses = $TwistOAuth->get("users/lookup", ["user_id" => implode(",", array_values(array_unique($user_ids)))]); foreach ($statuses as $status) { if ($Cache->exists($status->id_str)) { $json = json_decode(file_get_contents(config("cache.path") . "/{$status->id_str}/data.json")); $names[$status->id_str]["name"] = $json->name; $names[$status->id_str]["screen_name"] = $json->screen_name; $names[$status->id_str]["icon"] = $json->icon; continue; } $create_cache[] = $status->id_str; $names[$status->id_str] = ["name" => $status->name, "icon" => str_replace("_normal", "", $status->profile_image_url_https)]; } foreach ($create_cache as $id) { if ($Cache->exists($id) && $Cache->update_at($id) >= 1) { $Cache->update($id); } else {
/* crontab -e 0 1 * * * cd /your_dir/backend/; php cron.php 39ff */ /*if (array_shift(get_included_files()) === __FILE__) die('Only cli'); */ if (empty($argv[1])) { exit('Usage:php cron.php screen_name' . PHP_EOL); } $screen_name = $argv[1]; require 'TwistOAuth.phar'; require '../config.php'; try { $pdo = new PDO(sprintf('mysql:dbname=%s;host=%s;charset=%s', DBNAME, DBHOST, DBCHARSET), DBUSER, DBPASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false)); $to = new TwistOAuth(CK, CS, AT, ATS); $data = $to->get('users/show', array('screen_name' => $screen_name)); //friends_count //followers_count $pdo->beginTransaction(); $sql = 'INSERT INTO analytics(screen_name,twitter_id,follower,following,fetched_date) VALUES(?,?,?,?,?)'; $stmt = $pdo->prepare($sql); $stmt->execute(array($screen_name, $data->id, $data->followers_count, $data->friends_count, date('Y-m-d H:i:s'))); $stmt->closeCursor(); $pdo->commit(); } catch (Exception $e) { //var_dump($e->getMessage()); $export['code'] = 500; //rollback when failed. $pdo->rollBack(); }