1451aecdae4b441c6e13cf95ed010d5d50bd630f
[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 username = '@' + c['original_name']
30 place = ''
31 if keyword != '':
32 place = '/public'
33 keyword = '#' + keyword
34
35 if listname != '':
36 username, place = listname.split('/')
37 place = "/" + place
38
39 formattedPrefix = formattedPrefix.replace("#username", username)
40 formattedPrefix = formattedPrefix.replace("#place", place)
41 formattedPrefix = formattedPrefix.replace("#keyword", keyword)
42
43 return formattedPrefix