Created
August 3, 2014 00:16
-
-
Save yokozawa/01f2eaded18afe72e80a to your computer and use it in GitHub Desktop.
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 | |
date_default_timezone_set('Asia/Tokyo'); | |
// twitter setting | |
require_once('twitteroauth/twitteroauth/twitteroauth.php'); | |
$consumer_key = "YOUR CONSUMER KEY"; | |
$consumer_secret = "YOUR CONSUMER SECRET"; | |
$access_token = "ACCESS TOKEN"; | |
$access_token_secret = "ACCESS TOKEN SECRET"; | |
// blog setting | |
require_once('simple_html_dom.php'); | |
$blog_id = "YOUR LIVEDOOR BLOG ID"; | |
$blog_uri = "http://blog.livedoor.jp/$blog_id/archives/"; | |
// get all feed | |
$target_year = 2014; | |
$all_posts = array(); | |
for ($month = 1; $month <= 12; $month++) { | |
for ($p = 1; $p < 10; $p++) { | |
$get_uri = $blog_uri . $target_year . "-" . sprintf("%02d", $month) . ".html" . "?p=$p"; | |
$html = @file_get_html($get_uri); | |
if ($html && is_object($html) && isset($html->nodes)) { | |
foreach ($html->find(".article-title") as $title) { | |
array_push($all_posts, array( | |
"title" => $title->plaintext, | |
"link_uri" => $title->find("a", 0)->href, | |
)); | |
} | |
} | |
} | |
} | |
// set baknumber | |
$post = $all_posts[array_rand($all_posts, 1)]; | |
$message = "post back number:" . $post["title"] . " " . $post["link_uri"]; | |
$conn = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret); | |
//random post | |
$conn->OAuthRequest( | |
"https://api.twitter.com/1.1/statuses/update.json", | |
"POST", | |
array( | |
"status" => $message | |
)); | |
// // follow back | |
if ($conn) { | |
$followers = $conn->get('followers/ids', array('cursor' => -1)); | |
$friends = $conn->get('friends/ids', array('cursor' => -1)); | |
if ($followers && $friends && !empty($friends->ids)) { | |
foreach ($followers->ids as $i => $id) { | |
if (!in_array($id, $friends->ids)) { | |
$conn->post('friendships/create', array('user_id' => $id)); | |
} | |
} | |
} | |
} | |
// search follow | |
$search_word = urlencode('twitter bot'); | |
$req = $conn->OAuthRequest( | |
'https://api.twitter.com/1.1/search/tweets.json','GET', | |
array( | |
'q' => $search_word, | |
'count' => '10', | |
'include_entities' => 'true', | |
)); | |
$res = json_decode($req); | |
$recs = $res->{'statuses'}; | |
for( $i = 0; $i < sizeof($recs); $i++) { | |
$conn->post('friendships/create', array('user_id' => $recs[$i]->user->id)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment