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):
38 if data.has_key(
'media'):
53 url = self._BuildUrl(url, extra_params=data)
62 url =
'https://api.twitter.com/1.1/statuses/update.json' 64 data = {
'status': StringIO(status)}
66 data = simplejson.loads(json.content)
72 url =
'https://api.twitter.com/1.1/statuses/update_with_media.json' 74 data = {
'status': StringIO(status)}
75 data[
'media'] = open(str(media),
'rb').read()
77 data = simplejson.loads(json.content)
85 rospy.loginfo(rospy.get_name() +
" sending %s", message)
88 m = re.search(
'/\S+\.(jpeg|jpg|png|gif)', message)
92 message = re.sub(filename,
"",message)
93 if os.path.exists(filename):
94 rospy.loginfo(rospy.get_name() +
" tweet %s with file %s", message, filename)
95 ret = Api.PostMedia(message[0:116], filename)
98 rospy.logerr(rospy.get_name() +
" %s could not find", filename)
100 ret = Api.PostUpdate(message[0:140])
103 rospy.loginfo(rospy.get_name() +
" receiving %s", ret)
107 global CKEY, CSECRET, AKEY, ASECRET
108 account_info = rospy.get_param(
'account_info',
'/var/lib/robot/account.yaml')
111 key = yaml.load(open(account_info))
113 CSECRET = key[
'CSECRET']
115 ASECRET = key[
'ASECRET']
117 rospy.logerr(
'"%s" not found'%account_info)
118 rospy.logerr(
"$ get access token from https://apps.twitter.com/")
119 rospy.logerr(
"cat /var/lib/robot/%s <<EOF"%account_info)
120 rospy.logerr(
"CKEY: xxx")
121 rospy.logerr(
"CSECRET: xxx")
122 rospy.logerr(
"AKEY: xxx")
123 rospy.logerr(
"ASECRET: xxx")
127 if __name__ ==
'__main__':
129 rospy.init_node(
'rostwitter', anonymous=
True)
132 consumer_secret=CSECRET,
133 access_token_key=AKEY,
134 access_token_secret=ASECRET)
135 rospy.Subscriber(
"tweet", String, tweet)