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  print(data)
38  if data.has_key('media'):
39  return requests.post(
40  url,
41  files=data,
42  auth=self.__auth,
43  timeout=self._requests_timeout
44  )
45  else:
46  return requests.post(
47  url,
48  data=data,
49  auth=self.__auth,
50  timeout=self._requests_timeout
51  )
52  if verb == 'GET':
53  url = self._BuildUrl(url, extra_params=data)
54  return requests.get(
55  url,
56  auth=self.__auth,
57  timeout=self._requests_timeout
58  )
59  return 0 # if not a POST or GET request
60 
61  def PostUpdate(self, status):
62  url = 'https://api.twitter.com/1.1/statuses/update.json'
63 
64  data = {'status': StringIO(status)}
65  json = self._RequestUrl(url, 'POST', data=data)
66  data = simplejson.loads(json.content)
67  if 'error' in data:
68  raise Exception(data)
69  return data
70 
71  def PostMedia(self, status, media):
72  url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'
73 
74  data = {'status': StringIO(status)}
75  data['media'] = open(str(media), 'rb').read()
76  json = self._RequestUrl(url, 'POST', data=data)
77  data = simplejson.loads(json.content)
78  if 'errors' in data:
79  raise Exception(data)
80  return data
81 
82 def tweet(dat):
83  global Api
84  message = dat.data
85  rospy.loginfo(rospy.get_name() + " sending %s", message)
86 
87  # search word start from / and end with {.jpeg,.jpg,.png,.gif}
88  m = re.search('/\S+\.(jpeg|jpg|png|gif)', message)
89  ret = None
90  if m:
91  filename = m.group(0)
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) # 140 - len("http://t.co/ssssssssss")
96  #ret = Api.PostUpdate(message)
97  else:
98  rospy.logerr(rospy.get_name() + " %s could not find", filename)
99  else:
100  ret = Api.PostUpdate(message[0:140])
101  ## seg faults if message is longer than 140 byte ???
102 
103  rospy.loginfo(rospy.get_name() + " receiving %s", ret)
104  return
105 
107  global CKEY, CSECRET, AKEY, ASECRET
108  account_info = rospy.get_param('account_info', '/var/lib/robot/account.yaml')
109 
110  try:
111  key = yaml.load(open(account_info))
112  CKEY = key['CKEY']
113  CSECRET = key['CSECRET']
114  AKEY = key['AKEY']
115  ASECRET = key['ASECRET']
116  except IOError as e:
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")
124  rospy.logerr("EOF")
125  sys.exit(-1)
126 
127 if __name__ == '__main__':
128  global Api
129  rospy.init_node('rostwitter', anonymous=True)
131  Api = twitter(consumer_key=CKEY,
132  consumer_secret=CSECRET,
133  access_token_key=AKEY,
134  access_token_secret=ASECRET)
135  rospy.Subscriber("tweet", String, tweet)
136  rospy.spin()
137 
138 
139 
140 
_access_token_key
Definition: tweet.py:28
def PostMedia(self, status, media)
Definition: tweet.py:71
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:106
_consumer_secret
Definition: tweet.py:27
def tweet(dat)
Definition: tweet.py:82
def PostUpdate(self, status)
Definition: tweet.py:61


rostwitter
Author(s):
autogenerated on Wed Jul 10 2019 03:47:17