util.py
Go to the documentation of this file.
1 import os
2 import sys
3 import unicodedata
4 import yaml
5 
6 import rospy
7 
8 
9 def load_oauth_settings(yaml_path):
10  if not os.path.exists(yaml_path):
11  rospy.logerr('"{}" not found'.format(yaml_path))
12  rospy.logerr("$ get access token from https://apps.twitter.com/")
13  rospy.logerr("cat {} <<EOF".format(yaml_path))
14  rospy.logerr("CKEY: xxx")
15  rospy.logerr("CSECRET: xxx")
16  rospy.logerr("AKEY: xxx")
17  rospy.logerr("ASECRET: xxx")
18  rospy.logerr("EOF")
19  return None, None, None, None
20  with open(yaml_path, 'r') as f:
21  key = yaml.load(f, Loader=yaml.SafeLoader)
22  ckey = key['CKEY']
23  csecret = key['CSECRET']
24  akey = key['AKEY']
25  asecret = key['ASECRET']
26  return ckey, csecret, akey, asecret
27 
28 
29 def count_tweet_text(text):
30  count = 0
31  if sys.version_info.major <= 2:
32  text = text.decode('utf-8')
33  for c in text:
34  if unicodedata.east_asian_width(c) in 'FWA':
35  count += 2
36  else:
37  count += 1
38  return count
39 
40 
41 def split_tweet_text(text, length=280):
42  texts = []
43  split_text = ''
44  count = 0
45  if sys.version_info.major <= 2:
46  text = text.decode('utf-8')
47  for c in text:
48  if count == 281:
49  # last word is zenkaku.
50  texts.append(split_text[:-1])
51  split_text = split_text[-1:]
52  count = 2
53  elif count == 280:
54  texts.append(split_text)
55  split_text = ''
56  count = 0
57  split_text += c
58  if unicodedata.east_asian_width(c) in 'FWA':
59  count += 2
60  else:
61  count += 1
62  if count != 0:
63  texts.append(split_text)
64  return texts
def split_tweet_text(text, length=280)
Definition: util.py:41
def count_tweet_text(text)
Definition: util.py:29
def load_oauth_settings(yaml_path)
Definition: util.py:9


rostwitter
Author(s):
autogenerated on Sat Jun 24 2023 02:40:41