00001
00002 import imp
00003 import rospy
00004 import yaml,sys
00005 import re, os
00006 from io import BytesIO
00007 from StringIO import StringIO
00008
00009 from std_msgs.msg import String
00010
00011 global Api, CKEY, CSECRET, AKEY, ASECRET
00012
00013 import requests
00014 from requests_oauthlib import OAuth1
00015 import base64
00016 import json as simplejson
00017
00018
00019
00020 class twitter(object):
00021 def __init__(self,
00022 consumer_key=None,
00023 consumer_secret=None,
00024 access_token_key=None,
00025 access_token_secret=None):
00026 self._consumer_key = consumer_key
00027 self._consumer_secret = consumer_secret
00028 self._access_token_key = access_token_key
00029 self._access_token_secret = access_token_secret
00030
00031 self.__auth = OAuth1(self._consumer_key, self._consumer_secret,
00032 self._access_token_key, self._access_token_secret)
00033 self._requests_timeout = 60
00034
00035 def _RequestUrl(self, url, verb, data=None):
00036 if verb == 'POST':
00037 print(data)
00038 if data.has_key('media'):
00039 return requests.post(
00040 url,
00041 files=data,
00042 auth=self.__auth,
00043 timeout=self._requests_timeout
00044 )
00045 else:
00046 return requests.post(
00047 url,
00048 data=data,
00049 auth=self.__auth,
00050 timeout=self._requests_timeout
00051 )
00052 if verb == 'GET':
00053 url = self._BuildUrl(url, extra_params=data)
00054 return requests.get(
00055 url,
00056 auth=self.__auth,
00057 timeout=self._requests_timeout
00058 )
00059 return 0
00060
00061 def PostUpdate(self, status):
00062 url = 'https://api.twitter.com/1.1/statuses/update.json'
00063
00064 data = {'status': StringIO(status)}
00065 json = self._RequestUrl(url, 'POST', data=data)
00066 data = simplejson.loads(json.content)
00067 if 'error' in data:
00068 raise Exception(data)
00069 return data
00070
00071 def PostMedia(self, status, media):
00072 url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'
00073
00074 data = {'status': StringIO(status)}
00075 data['media'] = open(str(media), 'rb').read()
00076 json = self._RequestUrl(url, 'POST', data=data)
00077 data = simplejson.loads(json.content)
00078 if 'errors' in data:
00079 raise Exception(data)
00080 return data
00081
00082 def tweet(dat):
00083 global Api
00084 message = dat.data
00085 rospy.loginfo(rospy.get_name() + " sending %s", message)
00086
00087
00088 m = re.search('/\S+\.(jpeg|jpg|png|gif)', message)
00089 ret = None
00090 if m:
00091 filename = m.group(0)
00092 message = re.sub(filename,"",message)
00093 if os.path.exists(filename):
00094 rospy.loginfo(rospy.get_name() + " tweet %s with file %s", message, filename)
00095 ret = Api.PostMedia(message[0:116], filename)
00096
00097 else:
00098 rospy.logerr(rospy.get_name() + " %s could not find", filename)
00099 else:
00100 ret = Api.PostUpdate(message[0:140])
00101
00102
00103 rospy.loginfo(rospy.get_name() + " receiving %s", ret)
00104 return
00105
00106 def load_oauth_settings():
00107 global CKEY, CSECRET, AKEY, ASECRET
00108 account_info = rospy.get_param('account_info', '/var/lib/robot/account.yaml')
00109
00110 try:
00111 key = yaml.load(open(account_info))
00112 CKEY = key['CKEY']
00113 CSECRET = key['CSECRET']
00114 AKEY = key['AKEY']
00115 ASECRET = key['ASECRET']
00116 except IOError as e:
00117 rospy.logerr('"%s" not found'%account_info)
00118 rospy.logerr("$ get access token from https://apps.twitter.com/")
00119 rospy.logerr("cat /var/lib/robot/%s <<EOF"%account_info)
00120 rospy.logerr("CKEY: xxx")
00121 rospy.logerr("CSECRET: xxx")
00122 rospy.logerr("AKEY: xxx")
00123 rospy.logerr("ASECRET: xxx")
00124 rospy.logerr("EOF")
00125 sys.exit(-1)
00126
00127 if __name__ == '__main__':
00128 global Api
00129 rospy.init_node('rostwitter', anonymous=True)
00130 load_oauth_settings()
00131 Api = twitter(consumer_key=CKEY,
00132 consumer_secret=CSECRET,
00133 access_token_key=AKEY,
00134 access_token_secret=ASECRET)
00135 rospy.Subscriber("tweet", String, tweet)
00136 rospy.spin()
00137
00138
00139
00140