Twitterで過去記事を自動で呟くのをやってみた。凄く簡単「Tweetily」の使い方

ENJI

2013-09-24_214228

Tweetily

WordPressの過去記事を自動ツイートを設定できるプラグイン

Twitterで過去記事を自動で呟く仕組みいいなぁ。。と。
で、そのやり方も@mk_mizuhoさんの自動ツイートで知った次第です。

 

[Å] WordPressプラグイン「Tweetily」を使って
過去記事をランダムにツイートするようにしてみた!

Tweetily

Tweetilyをまずダウンロード。直接「プラグインの新規追加」でもOK
で、有効化したら注意点。あかめさんのブログにもあるけど

日本語未対応!

なので、ちょっとコアファイルの修正が必要です。

日本語に対応する修正方法

2013-09-24_211126

①プラグイン編集から②で「Tweetily」を選択して「tweetily-tweet-wordpress-posts-automatically/top-core.php」をクリック。

で、これを変更するコードがあかめさんの記事に書いてあり、

古いかもしれないから気をつけろよ!

と書いてあるのを全開で無視してバックアップを取らず盲目的にコピーペースト!
⇒「ファイルを保存」!

エラー!!!!!

いう事は聞かないとダメですね。気を取り直して、再インストール。

下記コード作成して全部コピペ(2013.09.24現在)

10){$as_number_tweet = 10;}

//trying to fix multiposts
//if($last<1){$as_number_tweet = 0;}
$pt='';
if($as_post_type!='all'){
	$pt = "post_type = '$as_post_type' AND";
}
    $sql = "SELECT ID
            FROM $wpdb->posts
            WHERE $pt post_status = 'publish' ";
    
   if(is_numeric($ageLimit))
    {
        if($ageLimit > 0)
                $sql = $sql . " AND post_date <= curdate( ) - INTERVAL " . $ageLimit . " day";
    }
    
    if ($maxAgeLimit != 0) {
        $sql = $sql . " AND post_date >= curdate( ) - INTERVAL " . $maxAgeLimit . " day";
    }

    if (isset($exposts)) {
        if (trim($exposts) != '') {
            $sql = $sql . " AND ID Not IN (" . $exposts . ") ";
        }
    }

    if (isset($already_tweeted)) {
    if(trim($already_tweeted) !="")
    {
        $sql = $sql . " AND ID Not IN (" . $already_tweeted . ") ";
    }
    }
   if ($omitCats != '') {
        $sql = $sql . " AND NOT (ID IN (SELECT tr.object_id FROM " . $wpdb->prefix . "term_relationships AS tr INNER JOIN " . $wpdb->prefix . "term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN (" . $omitCats . ")))";
    }
    $sql = $sql . "
            ORDER BY RAND() 
            LIMIT $as_number_tweet ";

    $oldest_post = $wpdb->get_results($sql);
    
    if($oldest_post == null)
    {
        if($can_requery)
        {
            $top_opt_tweeted_posts=array();
            update_option('top_opt_tweeted_posts', $top_opt_tweeted_posts);
           return top_generate_query(false);
        }
        else
        {
           return "No post found to tweet. Please check your settings and try again.--".$sql; 
        }
    }



    if(isset($oldest_post)){
		 $ret = '';
		 foreach($oldest_post as $k=>$odp){
                    array_push($top_opt_tweeted_posts, $odp->ID);
         	    $ret .= 'Tweet '.$k.' : '.top_opt_tweet_post($odp->ID).'
'; } update_option('top_opt_tweeted_posts', $top_opt_tweeted_posts); if ( function_exists('w3tc_pgcache_flush') ) { w3tc_pgcache_flush(); w3tc_dbcache_flush(); w3tc_minify_flush(); w3tc_objectcache_flush(); $cache = ' and W3TC Caches cleared'; } $next_tweet_time = time()+ get_option('top_opt_interval') * 60 * 60; update_option('next_tweet_time', $next_tweet_time); return $ret; } return $rtrn_msg; } //tweet for the passed random post function top_opt_tweet_post($oldest_post) { global $wpdb; $post = get_post($oldest_post); $content = ""; $to_short_url = true; $shorturl = ""; $tweet_type = get_option('top_opt_tweet_type'); $additional_text = get_option('top_opt_add_text'); $additional_text_at = get_option('top_opt_add_text_at'); $include_link = get_option('top_opt_include_link'); $custom_hashtag_option = get_option('top_opt_custom_hashtag_option'); $custom_hashtag_field = get_option('top_opt_custom_hashtag_field'); $twitter_hashtags = get_option('top_opt_hashtags'); $url_shortener = get_option('top_opt_url_shortener'); $custom_url_option = get_option('top_opt_custom_url_option'); $to_short_url = get_option('top_opt_use_url_shortner'); $use_inline_hashtags = get_option('top_opt_use_inline_hashtags'); $hashtag_length = get_option('top_opt_hashtag_length'); if ($include_link != "false") { $permalink = get_permalink($oldest_post); if ($custom_url_option) { $custom_url_field = get_option('top_opt_custom_url_field'); if (trim($custom_url_field) != "") { $permalink = trim(get_post_meta($post->ID, $custom_url_field, true)); } } if ($to_short_url) { if ($url_shortener == "bit.ly") { $bitly_key = get_option('top_opt_bitly_key'); $bitly_user = get_option('top_opt_bitly_user'); $shorturl = shorten_url($permalink, $url_shortener, $bitly_key, $bitly_user); } else { $shorturl = shorten_url($permalink, $url_shortener); } } else { $shorturl = $permalink; } } if ($tweet_type == "title" || $tweet_type == "titlenbody") { $title = stripslashes($post->post_title); $title = strip_tags($title); $title = preg_replace('/\s\s+/', ' ', $title); } else { $title = ""; } if ($tweet_type == "body" || $tweet_type == "titlenbody") { $body = stripslashes($post->post_content); $body = strip_tags($body); $body = preg_replace('/\s\s+/', ' ', $body); } else { $body = ""; } if ($tweet_type == "titlenbody") { if ($title == null) { $content = $body; } elseif ($body == null) { $content = $title; } else { $content = $title . " - " . $body; } } elseif ($tweet_type == "title") { $content = $title; } elseif ($tweet_type == "body") { $content = $body; } if ($additional_text != "") { if ($additional_text_at == "end") { $content = $content . " - " . $additional_text; } elseif ($additional_text_at == "beginning") { $content = $additional_text . ": " . $content; } } $hashtags = ""; $newcontent = ""; if ($custom_hashtag_option != "nohashtag") { if ($custom_hashtag_option == "common") { //common hashtag $hashtags = $twitter_hashtags; } //post custom field hashtag elseif ($custom_hashtag_option == "custom") { if (trim($custom_hashtag_field) != "") { $hashtags = trim(get_post_meta($post->ID, $custom_hashtag_field, true)); } } elseif ($custom_hashtag_option == "categories") { $post_categories = get_the_category($post->ID); if ($post_categories) { foreach ($post_categories as $category) { $tagname = str_replace(".", "", str_replace(" ", "", $category->cat_name)); if ($use_inline_hashtags) { if (mb_strrpos($content, $tagname) === false) { $hashtags = $hashtags . "#" . $tagname . " "; } else { $newcontent = preg_replace('/\b' . $tagname . '\b/i', "#" . $tagname, $content, 1); } } else { $hashtags = $hashtags . "#" . $tagname . " "; } } } } elseif ($custom_hashtag_option == "tags") { $post_tags = get_the_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { $tagname = str_replace(".", "", str_replace(" ", "", $tag->name)); if ($use_inline_hashtags) { if (mb_strrpos($content, $tagname) === false) { $hashtags = $hashtags . "#" . $tagname . " "; } else { $newcontent = preg_replace('/\b' . $tagname . '\b/i', "#" . $tagname, $content, 1); } } else { $hashtags = $hashtags . "#" . $tagname . " "; } } } } if ($newcontent != "") $content = $newcontent; } if ($include_link != "false") { if (!is_numeric($shorturl) && (strncmp($shorturl, "http", mb_strlen("http")) == 0)) { } else { return "OOPS!!! problem with your URL shortning service. Some signs of error " . $shorturl . "."; } } $message = set_tweet_length($content, $shorturl, $hashtags, $hashtag_length); $status = urlencode(stripslashes(urldecode($message))); if ($status) { $poststatus = top_update_status($message); if ($poststatus == true) { return "Success! Just tweeted successfully."; } else { return "Oh no! Something went wrong. Please try again."; } } return "Oh no! Looks like there are problems. Please email flavio@themana.gr."; } //send request to passed url and return the response function send_request($url, $method='GET', $data='', $auth_user='', $auth_pass='') { $ch = curl_init($url); if (strtoupper($method) == "POST") { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($auth_user != '' && $auth_pass != '') { curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_pass}"); } $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode != 200) { return $httpcode; } return $response; } /* returns a result form url */ function top_curl_get_result($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } function top_get_bitly_short_url($url,$login,$appkey,$format='txt') { $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format; return top_curl_get_result($connectURL); } //Shorten long URLs with is.gd or bit.ly. function shorten_url($the_url, $shortener='is.gd', $api_key='', $user='') { if (($shortener == "bit.ly") && isset($api_key) && isset($user)) { $response = top_get_bitly_short_url($the_url, $user, $api_key); } elseif ($shortener == "su.pr") { $url = "http://su.pr/api/simpleshorten?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "tr.im") { $url = "http://api.tr.im/api/trim_simple?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "3.ly") { $url = "http://3.ly/?api=em5893833&u={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "tinyurl") { $url = "http://tinyurl.com/api-create.php?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "u.nu") { $url = "http://u.nu/unu-api-simple?url={$the_url}"; $response = send_request($url, 'GET'); } elseif ($shortener == "1click.at") { $url = "http://1click.at/api.php?action=shorturl&url={$the_url}&format=simple"; $response = send_request($url, 'GET'); } else { $url = "http://is.gd/api.php?longurl={$the_url}"; $response = send_request($url, 'GET'); } return $response; } //Shrink a tweet and accompanying URL down to 140 chars. function set_tweet_length($message, $url, $twitter_hashtags="", $hashtag_length=0) { $tags = $twitter_hashtags; $message_length = mb_strlen($message); $url_length = mb_strlen($url); //$cur_length = mb_strlen($tags); if ($hashtag_length == 0) $hashtag_length = mb_strlen($tags); if ($twitter_hashtags != "") { if (mb_strlen($tags) > $hashtag_length) { $tags = mb_substr($tags, 0, $hashtag_length); $tags = mb_substr($tags, 0, mb_strrpos($tags, ' ')); } $hashtag_length = mb_strlen($tags); } if ($message_length + $url_length + $hashtag_length > 140) { $shorten_message_to = 140 - $url_length - $hashtag_length; $shorten_message_to = $shorten_message_to - 4; //$message = $message." "; if (mb_strlen($message) > $shorten_message_to) { $message = mb_substr($message, 0, $shorten_message_to); $message = mb_substr($message, 0, mb_strrpos($message, ' ')); } $message = $message . "..."; } return $message . " " . $url . " " . $tags; } //check time and update the last tweet time function top_opt_update_time() { return top_to_update(); } if ( function_exists('w3tc_pgcache_flush') ) { w3tc_pgcache_flush(); w3tc_dbcache_flush(); w3tc_minify_flush(); w3tc_objectcache_flush(); $cache = ' and W3TC Caches cleared'; } function top_to_update() { global $wpdb; //have to use normal query to prevent the caching plug-in from caching the last update time $last = $wpdb->get_var("select SQL_NO_CACHE option_value from $wpdb->options where option_name = 'top_opt_last_update';"); //$last_test = get_option('top_opt_last_update'); $interval = get_option('top_opt_interval'); $slop = get_option('top_opt_interval_slop'); if (!(isset($interval))) { $interval = top_opt_INTERVAL; } else if(!(is_numeric($interval))) { $interval = top_opt_INTERVAL; } if (!(isset($slop))) { $slop = top_opt_INTERVAL_SLOP; } else if(!(is_numeric($slop))) { $slop = top_opt_INTERVAL_SLOP; } $passed = time() - $last; //old testing code to test the actual values going into the DB //$wpdb->query("insert into wp_timetable (time,last,ret,url) values('".time()."',$last,$passed,'".$_SERVER['PHP_SELF']."');"); $interval = $interval * 60 * 60; $slop = $slop * 60 * 60; if (false === $last) { $ret = 1; } else if (is_numeric($last)) { $ret = (time() - $last) > $interval; } return $ret; } function top_get_auth_url() { global $top_oauth; $settings = top_get_settings(); $token = $top_oauth->get_request_token(); if ($token) { $settings['oauth_request_token'] = $token['oauth_token']; $settings['oauth_request_token_secret'] = $token['oauth_token_secret']; top_save_settings($settings); return $top_oauth->get_auth_url($token['oauth_token']); } } function top_update_status($new_status) { global $top_oauth; $settings = top_get_settings(); if (isset($settings['oauth_access_token']) && isset($settings['oauth_access_token_secret'])) { return $top_oauth->update_status($settings['oauth_access_token'], $settings['oauth_access_token_secret'], $new_status); } return false; } function top_has_tokens() { $settings = top_get_settings(); return ( $settings['oauth_access_token'] && $settings['oauth_access_token_secret'] ); } function top_is_valid() { return twit_has_tokens(); } function top_do_tweet($post_id) { $settings = top_get_settings(); $message = top_get_message($post_id); // If we have a valid message, Tweet it // this will fail if the Tiny URL service is done if ($message) { // If we successfully posted this to Twitter, then we can remove it from the queue eventually if (twit_update_status($message)) { return true; } } return false; } function top_get_settings() { global $top_defaults; $settings = $top_defaults; $wordpress_settings = get_option('top_settings'); if ($wordpress_settings) { foreach ($wordpress_settings as $key => $value) { $settings[$key] = $value; } } return $settings; } function top_save_settings($settings) { update_option('top_settings', $settings); } function top_reset_settings() { delete_option('top_settings'); update_option('top_enable_log',''); update_option('top_opt_add_text',''); update_option('top_opt_add_text_at','beginning'); update_option('top_opt_age_limit',0); update_option('top_opt_bitly_key',''); update_option('top_opt_bitly_user',''); update_option('top_opt_custom_hashtag_field',''); update_option('top_opt_custom_hashtag_option','nohashtag'); update_option('top_opt_custom_url_field',''); update_option('top_opt_custom_url_option',''); update_option('top_opt_excluded_post',''); update_option('top_opt_hashtags',''); update_option('top_opt_hashtag_length','20'); update_option('top_opt_include_link','no'); update_option('top_opt_interval',4); update_option('top_opt_interval_slop',4); delete_option('top_opt_last_update'); update_option('top_opt_max_age_limit',0); update_option('top_opt_omit_cats',''); update_option('top_opt_tweet_type','title'); delete_option('top_opt_tweeted_posts'); update_option('top_opt_url_shortener','is.gd'); update_option('top_opt_use_inline_hashtags',''); update_option('top_opt_use_url_shortner',''); } ?>

 ※要バックアップ。自己責任で(今回もそうですがプラグインのアップデートで不具合出る可能性あり。都度、再修正)

要は「strlen」と「substr」と「strrpos」を「mb_strlen」、「mb_substr」、「mb_strrpos」に修正しろよって事です。

Twitterとの連携

まずは自分のTwitterアカウントにログインしておきます。

2013-09-24_210317

②の「Sign with Twitter」をクリック

2013-09-24_210342

連携アプリを認証

以上!

超簡単・・・他のツイートアプリもっと面倒なのに。

Twittilyの基本設定

2013-09-24_215915

①でツイートタイトルの前の文字。ちゃんと「過去記事」というのは分かるようにしておいた方が良いと思います。②が「リンク」の追加!(これないと意味ない)。。で、③が呟く間隔。多いとウザイので20時間(1日1回くらい)にしてみました。

その他は、見ればわかると思いますので適宜変更を。

2013-09-24_210445

最後に「Updatete Tweetily Options」を押せば完了ですね。
「Tweet Now!」を押せばすぐにツイートして動作確認できます。

ぜひ、皆さんも!

-Twitter, WordPress

PAGE TOP