wordpress記事をtwitterに投稿する

いきさつ

記事をtwitterにアップしてみたかった。
WordPress 4.1.7を使っているが、「Tweet old post」、「Tweetily」を使ったが正常に投稿できない。
「Buffer My Post」も使ってみたが、無料だと10回までしか投稿できなかった。
そのため、自作で対応した。

対応内容

script: python, bash
定期実行:cron

script

OAuth1Session、requests_oauthlib、MySQL-python、python-develが必要となる。
easy_install OAuth1Session
# easy_install requests requests_oauthlib
# easy_install MySQL-python
# yum install python-devel
※mysql以外を使用している人は対象DB用のコネクタが必要
twit_up.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import MySQLdb
import random
import os
from requests_oauthlib import OAuth1Session
 
#
# function to take out one at random
#
def random_choice_unique(lists, num=1):
    if not lists:
        return []
 
    if len(lists) <= num:
        return lists
 
    copy_lists = lists[:]
    results = []
    for i in range(num):
        t = random.choice(copy_lists)
        results.append(t)
 
    return results
 
#
# function to retrieve the article from db
#
def get_article():
    connection = MySQLdb.connect(host="localhost", db="*****", user="*****", passwd="*****", charset="utf8")
    cursor = connection.cursor()
    # SQL
    cursor.execute("""select wpp.post_name
         , wpp.post_title
         , wpt.slug
         , CONCAT((SELECT `wp_options`.`option_value` FROM `wp_options` WHERE `wp_options`.`option_id` = 1), '/', wpt.slug, '/', wpp.post_name) AS 'url'
      from wp_posts wpp
         , wp_term_relationships wptr
         , wp_term_taxonomy wptt
         , wp_terms wpt
     where wpp.id = wptr.object_id
       and wptr.term_taxonomy_id = wptt.term_taxonomy_id
       and wpt.term_id = wptt.term_id
       and wpp.post_status in ('publish')
       and wpp.post_type = 'post'
    """)
    result = cursor.fetchall()
 
    r = random_choice_unique(result)
 
    param = {}
    for row in r:
        params = {"status": row[1]+" "+row[3]}
 
    cursor.close()
    connection.close()
 
    return params
 
if __name__ == '__main__':
 
    CK = '**********************'                             # Consumer Key
    CS = '******************************************'         # Consumer Secret
    AT = '********-*****************************************' # Access Token
    AS = '******************************************'         # Accesss Token Secert
 
    # url for the tweet post
 
    # tweet text
    params = get_article()
 
    # it will post in the POST methed with OAth authentication
    twitter = OAuth1Session(CK, CS, AT, AS)
    req = twitter.post(url, params = params)
 
    # we see the response
    if req.status_code == 200:
        print ("OK")
    else:
        print ("Error: %d" % req.status_code)
※consumer keyなどの情報は下記を参照した。
Twitterアプリケーションの作成
twit_up.sh
mysqlclientは別途インストールしたものを使用するため、作成。
私の環境では、postfixが古いmysqlclientを使っており、ld.so.confを書き換えられなかった。
そのため、プログラムごとに「LD_LIBRARY_PATH」を設定している。
 システムで統一して同一のmysqlclientを使用する場合は、不要
#!/bin/bash
 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib/
 
/root/script/twit_up.py
cron設定
4時間ごとに動作させる。
0 */4 * * * /root/script/twit_up.sh > /root/script/twit_up.log 2>&1

参考

ありがとうございました。
以上

このブログの人気の投稿

RAC環境でimpdpをパラレルで実行するとき

TrustedInstaller.exe メモリリーク

ctrl+s 操作が不能に