tweet.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import imp ## for rosbuild
3 import rospy
4 import yaml,sys
5 import re, os
6 from io import BytesIO
7 from StringIO import StringIO
8 
9 from std_msgs.msg import String
10 
11 global Api, CKEY, CSECRET, AKEY, ASECRET
12 
13 import requests
14 from requests_oauthlib import OAuth1
15 import base64
16 import json as simplejson
17 
18 # https://raw.githubusercontent.com/bear/python-twitter/v1.1/twitter.py
19 
20 class twitter(object):
21  def __init__(self,
22  consumer_key=None,
23  consumer_secret=None,
24  access_token_key=None,
25  access_token_secret=None):
26  self._consumer_key = consumer_key
27  self._consumer_secret = consumer_secret
28  self._access_token_key = access_token_key
29  self._access_token_secret = access_token_secret
30 
31  self.__auth = OAuth1(self._consumer_key, self._consumer_secret,
34 
35  def _RequestUrl(self, url, verb, data=None):
36  if verb == 'POST':
37  if data.has_key('media'):
38  return requests.post(
39  url,
40  files=data,
41  auth=self.__auth,
42  timeout=self._requests_timeout
43  )
44  else:
45  return requests.post(
46  url,
47  data=data,
48  auth=self.__auth,
49  timeout=self._requests_timeout
50  )
51  if verb == 'GET':
52  url = self._BuildUrl(url, extra_params=data)
53  return requests.get(
54  url,
55  auth=self.__auth,
56  timeout=self._requests_timeout
57  )
58  return 0 # if not a POST or GET request
59 
60  def PostUpdate(self, status):
61  url = 'https://api.twitter.com/1.1/statuses/update.json'
62 
63  data = {'status': StringIO(status)}
64  json = self._RequestUrl(url, 'POST', data=data)
65  data = simplejson.loads(json.content)
66  if 'error' in data:
67  raise Exception(data)
68  return data
69 
70  def PostMedia(self, status, media):
71  url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'
72 
73  data = {'status': StringIO(status)}
74  data['media'] = open(str(media), 'rb').read()
75  json = self._RequestUrl(url, 'POST', data=data)
76  data = simplejson.loads(json.content)
77  if 'errors' in data:
78  raise Exception(data)
79  return data
80 
81 def tweet(dat):
82  global Api
83  message = dat.data
84  rospy.loginfo(rospy.get_name() + " sending %s", message)
85 
86  # search word start from / and end with {.jpeg,.jpg,.png,.gif}
87  m = re.search('/\S+\.(jpeg|jpg|png|gif)', message)
88  ret = None
89  if m:
90  filename = m.group(0)
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) # 140 - len("http://t.co/ssssssssss")
95  #ret = Api.PostUpdate(message)
96  else:
97  rospy.logerr(rospy.get_name() + " %s could not find", filename)
98  else:
99  ret = Api.PostUpdate(message[0:140])
100  ## seg faults if message is longer than 140 byte ???
101 
102  rospy.loginfo(rospy.get_name() + " receiving %s", ret)
103  return
104 
106  global CKEY, CSECRET, AKEY, ASECRET
107  account_info = rospy.get_param('account_info', '/var/lib/robot/account.yaml')
108 
109  try:
110  key = yaml.load(open(account_info))
111  CKEY = key['CKEY']
112  CSECRET = key['CSECRET']
113  AKEY = key['AKEY']
114  ASECRET = key['ASECRET']
115  except IOError as e:
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")
123  rospy.logerr("EOF")
124  sys.exit(-1)
125 
126 if __name__ == '__main__':
127  global Api
128  rospy.init_node('rostwitter', anonymous=True)
130  Api = twitter(consumer_key=CKEY,
131  consumer_secret=CSECRET,
132  access_token_key=AKEY,
133  access_token_secret=ASECRET)
134  rospy.Subscriber("tweet", String, tweet)
135  rospy.spin()
136 
137 
138 
139 
_access_token_key
Definition: tweet.py:28
def PostMedia(self, status, media)
Definition: tweet.py:70
def __init__(self, consumer_key=None, consumer_secret=None, access_token_key=None, access_token_secret=None)
Definition: tweet.py:25
def _RequestUrl(self, url, verb, data=None)
Definition: tweet.py:35
_access_token_secret
Definition: tweet.py:29
_requests_timeout
Definition: tweet.py:33
def load_oauth_settings()
Definition: tweet.py:105
_consumer_secret
Definition: tweet.py:27
def tweet(dat)
Definition: tweet.py:81
def PostUpdate(self, status)
Definition: tweet.py:60


rostwitter
Author(s):
autogenerated on Tue May 11 2021 02:55:51