tweet.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib
00004 roslib.load_manifest('jsk_pr2_startup')
00005 import rospy
00006 import twoauth,yaml,sys
00007 import re, os
00008 from std_msgs.msg import String
00009 
00010 # see http://d.hatena.ne.jp/gumilab/20101004/1286154912 to setup CKEY/AKEY
00011 try:
00012     key = yaml.load(open('/var/lib/robot/twitter_acount_pr2jsk.yaml'))
00013     CKEY = key['CKEY']
00014     CSECRET = key['CSECRET']
00015     AKEY = key['AKEY']
00016     ASECRET = key['ASECRET']
00017 except IOError as e:
00018     rospy.logerr('"/var/lib/robot/twitter_acount_pr2jsk.yaml" not found')
00019     rospy.logerr("$ rosrun python_twoauth get_access_token.py")
00020     rospy.logerr("cat /var/lib/robot/twitter_acount_pr2jsk.yaml <<EOF")
00021     rospy.logerr("CKEY: xxx")
00022     rospy.logerr("CSECRET: xxx")
00023     rospy.logerr("AKEY: xxx")
00024     rospy.logerr("ASECRET: xxx")
00025     rospy.logerr("EOF")
00026     rospy.logerr('see http://d.hatena.ne.jp/gumilab/20101004/1286154912 for detail')
00027     sys.exit(-1)
00028 
00029 def twit_lst(tw_str):
00030     ret = []
00031     tw_len = 140
00032     while True:
00033         a = tw_str[tw_len-140:tw_len]
00034         if a == '':
00035             return ret
00036         tw_len = tw_len + 140
00037         ret.append(a)
00038 
00039 def twit(dat):
00040     message = dat.data
00041     rospy.loginfo(rospy.get_name() + " sending %s", message)
00042     # search word start from / and end with {.jpeg,.jpg,.png,.gif}
00043     m = re.search('/\S+\.(jpeg|jpg|png|gif)', message)
00044     if m :
00045         filename = m.group(0)
00046         message = re.sub(filename,"",message)
00047         if os.path.exists(filename):
00048             ##rospy.logdebug(rospy.get_name() + " tweet %s with file %s", message, filename)
00049             twitter.status_update_with_media(message, filename)
00050             return
00051 
00052     lst = twit_lst(message)
00053     lst.reverse() ## tweet bottom line first
00054     for sub_msg in lst:
00055         ##rospy.logdebug(rospy.get_name() + " tweet %s", sub_msg)
00056         twitter.status_update(sub_msg)
00057     ## seg faults if message is longer than 140 byte ???
00058     ##twitter.status_update(message)
00059     return
00060 
00061 if __name__ == '__main__':
00062     twitter = twoauth.api(CKEY, CSECRET, AKEY, ASECRET)
00063     rospy.init_node('rostwitter', anonymous=True)
00064     rospy.Subscriber("pr2twit", String, twit)
00065     rospy.spin()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


jsk_pr2_startup
Author(s): Manabu Saito
autogenerated on Sat Mar 23 2013 17:11:33