Check tweet length
[rainbowstream.git] / rainbowstream / util.py
1 import json
2
3 from twitter.util import printNicely
4 from .colors import *
5 from .config import *
6
7
8 def detail_twitter_error(twitterException):
9 """
10 Display Twitter Errors nicely
11 """
12 try:
13 # twitterException.response_data can be byte string on Python 3
14 # or nornal dict on Python 2
15 loadedJson = json.loads(twitterException.response_data.decode('utf8'))
16 for m in loadedJson.get('errors', dict()):
17 info = "Error " + str(m.get('code')) + ": " + m.get('message')
18 printNicely(yellow(info))
19 except:
20 info = "Error: " + twitterException.response_data.decode('utf8')
21 printNicely(yellow(info))
22
23
24 def format_prefix(listname='', keyword=''):
25 """
26 Format the custom prefix
27 """
28 formattedPrefix = c['PREFIX']
29 owner = '@' + c['original_name']
30 place = ''
31 # Public stream
32 if keyword:
33 formattedPrefix = ''.join(formattedPrefix.split('#owner'))
34 formattedPrefix = ''.join(formattedPrefix.split('#place'))
35 formattedPrefix = ''.join(formattedPrefix.split('#me'))
36 # List stream
37 elif listname:
38 formattedPrefix = ''.join(formattedPrefix.split('#keyword'))
39 formattedPrefix = ''.join(formattedPrefix.split('#me'))
40 owner, place = listname.split('/')
41 place = '/' + place
42 # Personal stream
43 else:
44 formattedPrefix = ''.join(formattedPrefix.split('#keyword'))
45 formattedPrefix = ''.join(formattedPrefix.split('#owner'))
46 formattedPrefix = ''.join(formattedPrefix.split('#place'))
47
48 formattedPrefix = formattedPrefix.replace('#owner', owner)
49 formattedPrefix = formattedPrefix.replace('#place', place)
50 formattedPrefix = formattedPrefix.replace('#keyword', keyword)
51 formattedPrefix = formattedPrefix.replace('#me', '@' + c['original_name'])
52
53 return formattedPrefix