import time
import requests
import webbrowser
+import json
from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup
from twitter.api import *
name = '@' + t.account.verify_credentials()['screen_name']
g['original_name'] = name[1:]
g['decorated_name'] = color_func(c['DECORATED_NAME'])('[' + name + ']: ')
- g['ascii_art'] = True
files = os.listdir(os.path.dirname(__file__) + '/colorset')
themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
- themes += ['custom']
g['themes'] = themes
- db.theme_store(c['theme'])
+ db.theme_store(c['THEME'])
def switch():
if args.ignore:
printNicely(red('Ignore: ' + str(args.ignore)))
printNicely('')
- g['ascii_art'] = True
except:
printNicely(red('Sorry I can\'t understand.'))
media = tweet['entities']['media']
for m in media:
res = requests.get(m['media_url'])
- img = Image.open(StringIO(res.content))
+ img = Image.open(BytesIO(res.content))
img.show()
except:
printNicely(red('Sorry I can\'t show this image.'))
printNicely(red('Sorry I can\'t understand.'))
+def get_slug():
+ """
+ Get Slug Decorator
+ """
+ # Get list name
+ list_name = raw_input(light_magenta('Give me the list\'s name: '))
+ # Get list name and owner
+ try:
+ owner, slug = list_name.split('/')
+ if slug.startswith('@'):
+ slug = slug[1:]
+ return owner, slug
+ except:
+ printNicely(light_magenta('List name should follow "@owner/list_name" format.'))
+ raise Exception('Wrong list name')
+
+
def show_lists(t):
"""
List list
"""
List home
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
res = t.lists.statuses(
slug=slug,
owner_screen_name=owner,
"""
List members
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Get members
rel = {}
next_cursor = -1
"""
List subscribers
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Get subscribers
rel = {}
next_cursor = -1
"""
Add specific user to a list
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Add
user_name = raw_input(light_magenta('Give me name of the newbie: '))
if user_name.startswith('@'):
"""
Remove specific user from a list
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Remove
user_name = raw_input(light_magenta('Give me name of the unlucky one: '))
if user_name.startswith('@'):
"""
Subscribe to a list
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Subscribe
try:
t.lists.subscribers.create(
"""
Unsubscribe a list
"""
- # Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
- # Get list name and owner
- try:
- owner, slug = list_name.split('/')
- if slug.startswith('@'):
- slug = slug[1:]
- except:
- printNicely(light_magenta('Please follow "@owner/list_name" format.'))
- return
+ owner, slug = get_slug()
# Subscribe
try:
t.lists.subscribers.destroy(
mode=mode,
description=description)
printNicely(light_green(slug + ' list is updated.'))
- except Exception as e:
- print e
+ except:
printNicely(red('Oops something is wrong with Twitter :('))
}
try:
return action_ary[g['list_action']](t)
- except Exception as e:
- print e
- printNicely(red('Sorry I can\'t understand.'))
+ except:
+ printNicely(red('Please try again.'))
def cal():
if not g['stuff']:
# List themes
for theme in g['themes']:
- line = ''
- # Detect custom config
- if theme == 'custom':
- line += light_magenta('custom')
- custom_path = os.environ.get(
- 'HOME',
- os.environ.get('USERPROFILE',
- '')) + os.sep + '.rainbow_config.json'
- if not os.path.exists(custom_path):
- line += light_magenta(
- ' (create your own config file at ~/.rainbow_config.json)')
- else:
- line += light_magenta(' (loaded)')
- else:
- line += light_magenta(theme)
- if c['theme'] == theme:
+ line = light_magenta(theme)
+ if c['THEME'] == theme:
line = ' ' * 2 + light_yellow('* ') + line
else:
line = ' ' * 4 + line
printNicely(line)
elif g['stuff'] == 'current_as_default':
- # Set default
- path = os.path.dirname(__file__) + '/colorset/init'
- f = open(path, 'w')
- f.write(c['theme'])
- f.close()
+ # Set as default
+ def fixup(adict, k, v):
+ for key in adict.keys():
+ if key == k:
+ adict[key] = v
+ elif type(adict[key]) is dict:
+ fixup(adict[key], k, v)
+ # Modify
+ path = os.environ.get(
+ 'HOME',
+ os.environ.get(
+ 'USERPROFILE',
+ '')) + os.sep + '.rainbow_config.json'
+ data = load_config(rainbow_config)
+ fixup(data, 'THEME', c['THEME'])
+ # Save
+ with open(path, 'w') as out:
+ json.dump(data, out, indent = 4)
os.system('chmod 777 ' + path)
printNicely(light_green('Okay it will be applied from next time :)'))
else:
c[nc] = new_config[nc]
# Update db and reset colors
db.theme_update(g['stuff'])
- c['theme'] = g['stuff']
+ c['THEME'] = g['stuff']
reset_cycle()
g['decorated_name'] = color_func(
c['DECORATED_NAME'])(
'[@' + g['original_name'] + ']: ')
printNicely(green('Theme changed.'))
except:
- if g['stuff'] == 'custom':
- printNicely(red('~/.rainbow_config.json is not exists!'))
- else:
- printNicely(red('No such theme exists.'))
+ printNicely(red('No such theme exists.'))
def help_discover():
usage += s * 2 + light_green('list') + \
' will show all lists you are belong to.\n'
usage += s * 2 + light_green('list home') + \
- ' will show timeline of list. You will be asked for list\'s name\n'
+ ' will show timeline of list. You will be asked for list\'s name.\n'
usage += s * 2 + light_green('list all_mem') + \
' will show list\'s all members.\n'
usage += s * 2 + light_green('list all_sub') + \
art_dict = {
c['USER_DOMAIN']: name,
c['PUBLIC_DOMAIN']: args.track_keywords,
- c['SITE_DOMAIN']: 'Site Stream',
+ c['SITE_DOMAIN']: name,
}
- if g['ascii_art']:
+ if c['ASCII_ART']:
ascii_art(art_dict[domain])
# These arguments are optional:
except TwitterHTTPError:
printNicely('')
printNicely(
- magenta("I'm afraid we have maximum connection problem with twitter right now :("))
- printNicely(magenta("Let's try again later."))
+ magenta("Something wrong with Twitter Oauth right now :("))
+ printNicely(
+ magenta("Please delete ~/.rainbow_oauth and try again."))
save_history()
os.system('rm -rf rainbow.db')
sys.exit()