超簡単 n秒後にツイートするスクリプト

2015.6.17

th_Python-logo-notext.svgどういう時に使うかは微妙なのですが、Twitterに任意の時間経過後に自動的にツイートするPythonスクリプトです。cron叩くほどでもないや、という場合には使えるかもしれません。実用性は??どうでしょう。直ぐに試せるサンプルスクリプト本体をどうぞ。

簡単ツイート予約

スクリプト本体

#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import sys, codecs
import threading

C_KEY = "***************************"
C_SECRET = "***************************"
A_KEY = "***************************"
A_SECRET = "***************************"


def Post_msg():
    url = "https://api.twitter.com/1.1/statuses/update.json"
    params = {
        "status": u"タイマーのテスト from Pytimer",
        "lang": "ja"
            }
    tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
    req = tw.post(url, params = params)
    if req.status_code == 200:
        print "Success! Your Tweet"
    else:
        print req.status_code
    return Post_msg

t=threading.Timer(5,Post_msg)
t.start()

タイマーの秒数設定は

t=threading.Timer(5,Post_msg)

の箇所で変更できます。

bottleなどで実装して、タイマーツイートとかいうWebサービスにも応用はできそうですね。