Created
August 18, 2010 13:21
-
-
Save vim13/534705 to your computer and use it in GitHub Desktop.
許可したsenderからのDM本文をtweet
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
#!/usr/lib/python | |
#vim:fileencoding=utf-8 | |
import time | |
import re | |
import twitter | |
class tweetDM: | |
def __init__(self, account, passwd, access_token_key, access_token_secret, id_file, user_file): | |
self.api = twitter.Api(account, passwd, access_token_key, access_token_secret) | |
self.id_file = id_file | |
self.user_file = user_file | |
def readFile(self): | |
idfile = open(self.id_file, 'r') | |
latest_id = int(idfile.readline()) | |
idfile.close() | |
return latest_id | |
def writeFile(self, status_id): | |
idfile = open(self.id_file, 'w') | |
idfile.write(str(status_id)) | |
idfile.close() | |
def getDM(self): | |
latest_id = self.readFile() | |
dms = self.api.GetDirectMessages(since_id = latest_id) | |
messages = [] | |
if dms: | |
status_id = dms[0].id | |
self.writeFile(status_id) | |
usrfile = open(self.user_file, 'r').readlines() | |
user_list = [user[:-1] for user in usrfile] | |
for s in dms: | |
if s.sender_screen_name in user_list: | |
messages.append(s.text.encode('utf-8')) | |
return messages | |
def main(self): | |
messages = self.getDM() | |
if messages: | |
for s in messages: | |
self.api.PostUpdates(s) | |
time.sleep(5) | |
if __name__ == '__main__': | |
account = 'Consumer key' | |
passwd = 'Consumer secret' | |
access_token_key= 'Access token' | |
access_token_secret= 'Access token secret' | |
id_file = 'hoge' #最後に取得したDMのstatus_id | |
user_file = 'hoge' #許可するユーザー | |
obj = tweetDM(account, passwd, access_token_key, access_token_secret, id_file, user_file) | |
obj.main() |
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
screen_name1 | |
screen_name2 | |
screen_name3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment