7 from StringIO
import StringIO
9 from std_msgs.msg
import String
11 global Api, CKEY, CSECRET, AKEY, ASECRET
14 from requests_oauthlib
import OAuth1
16 import json
as simplejson
24 access_token_key=
None,
25 access_token_secret=
None):
37 if data.has_key(
'media'):
52 url = self._BuildUrl(url, extra_params=data)
61 url =
'https://api.twitter.com/1.1/statuses/update.json' 63 data = {
'status': StringIO(status)}
65 data = simplejson.loads(json.content)
71 url =
'https://api.twitter.com/1.1/statuses/update_with_media.json' 73 data = {
'status': StringIO(status)}
74 data[
'media'] = open(str(media),
'rb').read()
76 data = simplejson.loads(json.content)
84 rospy.loginfo(rospy.get_name() +
" sending %s", message)
87 m = re.search(
'/\S+\.(jpeg|jpg|png|gif)', message)
91 message = re.sub(filename,
"",message)
92 if os.path.exists(filename):
93 rospy.loginfo(rospy.get_name() +
" tweet %s with file %s", message, filename)
94 ret = Api.PostMedia(message[0:116], filename)
97 rospy.logerr(rospy.get_name() +
" %s could not find", filename)
99 ret = Api.PostUpdate(message[0:140])
102 rospy.loginfo(rospy.get_name() +
" receiving %s", ret)
106 global CKEY, CSECRET, AKEY, ASECRET
107 account_info = rospy.get_param(
'account_info',
'/var/lib/robot/account.yaml')
110 key = yaml.load(open(account_info))
112 CSECRET = key[
'CSECRET']
114 ASECRET = key[
'ASECRET']
116 rospy.logerr(
'"%s" not found'%account_info)
117 rospy.logerr(
"$ get access token from https://apps.twitter.com/")
118 rospy.logerr(
"cat /var/lib/robot/%s <<EOF"%account_info)
119 rospy.logerr(
"CKEY: xxx")
120 rospy.logerr(
"CSECRET: xxx")
121 rospy.logerr(
"AKEY: xxx")
122 rospy.logerr(
"ASECRET: xxx")
126 if __name__ ==
'__main__':
128 rospy.init_node(
'rostwitter', anonymous=
True)
131 consumer_secret=CSECRET,
132 access_token_key=AKEY,
133 access_token_secret=ASECRET)
134 rospy.Subscriber(
"tweet", String, tweet)