import threading
import requests
import webbrowser
+import traceback
+import pkg_resources
from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup
from twitter.api import *
from .interactive import *
from .c_image import *
from .py3patch import *
+from .emoji import *
# Global values
g = {}
return screen_name_list
+def debug_option():
+ """
+ Save traceback when run in debug mode
+ """
+ if g['debug']:
+ g['traceback'].append(traceback.format_exc())
+
+
+def upgrade_center():
+ """
+ Check latest and notify to upgrade
+ """
+ try:
+ current = pkg_resources.get_distribution("rainbowstream").version
+ url = 'https://raw.githubusercontent.com/DTVD/rainbowstream/master/setup.py'
+ readme = requests.get(url).text
+ latest = readme.split("version = \'")[1].split("\'")[0]
+ if current != latest:
+ notice = light_magenta('RainbowStream latest version is ')
+ notice += light_green(latest)
+ notice += light_magenta(' while your current version is ')
+ notice += light_yellow(current) + '\n'
+ notice += light_magenta('You should upgrade with ')
+ notice += light_green('pip install -U rainbowstream')
+ printNicely(notice)
+ except:
+ pass
+
+
def init(args):
"""
Init function
# Handle Ctrl C
ctrl_c_handler = lambda signum, frame: quit()
signal.signal(signal.SIGINT, ctrl_c_handler)
+ # Upgrade notify
+ upgrade_center()
# Get name
t = Twitter(auth=authen())
credential = t.account.verify_credentials()
name = credential['name']
if not get_config('PREFIX'):
set_config('PREFIX', screen_name)
+ c['PREFIX'] = emojize(c['PREFIX'])
g['PREFIX'] = u2str(c['PREFIX'])
c['original_name'] = g['original_name'] = screen_name[1:]
g['full_name'] = name
g['decorated_name'] = lambda x: color_func(
- c['DECORATED_NAME'])('[' + x + ']: ')
+ c['DECORATED_NAME'])('[' + x + ']: ', rl=True)
# Theme init
files = os.listdir(os.path.dirname(__file__) + '/colorset')
themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
g['message_threads'] = {}
# Startup cmd
g['cmd'] = ''
+ # Debug option default = True
+ g['debug'] = True
+ g['traceback'] = []
+ # Events
+ c['events'] = []
# Semaphore init
c['lock'] = False
# Init tweet dict and message dict
printNicely('')
+def notification():
+ """
+ Show notifications
+ """
+ if c['events']:
+ for e in c['events']:
+ print_event(e)
+ printNicely('')
+ else:
+ printNicely(magenta('Nothing at this time.'))
+
+
def mentions():
"""
Mentions timeline
include_entities=False)
show_profile(user)
except:
- printNicely(red('Omg no user.'))
+ debug_option()
+ printNicely(red('No user.'))
else:
printNicely(red('A name should begin with a \'@\''))
num = int(g['stuff'].split()[1])
except:
num = c['HOME_TWEET_NUM']
- for tweet in reversed(t.statuses.user_timeline(count=num, screen_name=user[1:])):
+ for tweet in reversed(
+ t.statuses.user_timeline(count=num, screen_name=user[1:])):
draw(t=tweet)
printNicely('')
else:
if not formater:
return
# Get comment
- prefix = light_magenta('Compose your ') + light_green('#comment: ')
+ prefix = light_magenta('Compose your ', rl=True) + \
+ light_green('#comment: ', rl=True)
comment = raw_input(prefix)
if comment:
quote = comment.join(formater.split('#comment'))
printNicely('')
+def share():
+ """
+ Copy url of a tweet to clipboard
+ """
+ t = Twitter(auth=authen())
+ try:
+ id = int(g['stuff'].split()[0])
+ except:
+ printNicely(red('Sorry I can\'t understand.'))
+ return
+ tid = c['tweet_dict'][id]
+ tweet = t.statuses.show(id=tid)
+ url = 'https://twitter.com/' + \
+ tweet['user']['screen_name'] + '/status/' + str(tid)
+ os.system("echo '%s' | pbcopy" % url)
+ printNicely(green('Copied tweet\'s url to clipboard.'))
+
+
def delete():
"""
Delete
img = Image.open(BytesIO(res.content))
img.show()
except:
+ debug_option()
printNicely(red('Sorry I can\'t show this image.'))
for link in link_ary:
webbrowser.open(link)
except:
+ debug_option()
printNicely(red('Sorry I can\'t open url in this tweet.'))
g['original_name'],
g['full_name'])
except Exception:
+ debug_option()
printNicely(red('No such thread.'))
else:
printNicely(red('A name should begin with a \'@\''))
except:
+ debug_option()
printNicely(red('Sorry I can\'t understand.'))
else:
printNicely(red(rel))
except:
+ debug_option()
printNicely(red('Something is wrong, can not mute now :('))
else:
printNicely(red('A name should begin with a \'@\''))
def get_slug():
"""
- Get Slug Decorator
+ Get slug
"""
# Get list name
- list_name = raw_input(light_magenta('Give me the list\'s name: '))
+ list_name = raw_input(
+ light_magenta('Give me the list\'s name ("@owner/list_name"): ', rl=True))
# Get list name and owner
try:
owner, slug = list_name.split('/')
"""
owner, slug = get_slug()
# Add
- user_name = raw_input(light_magenta('Give me name of the newbie: '))
+ user_name = raw_input(
+ light_magenta(
+ 'Give me name of the newbie: ',
+ rl=True))
if user_name.startswith('@'):
user_name = user_name[1:]
try:
screen_name=user_name)
printNicely(green('Added.'))
except:
+ debug_option()
printNicely(light_magenta('I\'m sorry we can not add him/her.'))
"""
owner, slug = get_slug()
# Remove
- user_name = raw_input(light_magenta('Give me name of the unlucky one: '))
+ user_name = raw_input(
+ light_magenta(
+ 'Give me name of the unlucky one: ',
+ rl=True))
if user_name.startswith('@'):
user_name = user_name[1:]
try:
screen_name=user_name)
printNicely(green('Gone.'))
except:
+ debug_option()
printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
owner_screen_name=owner)
printNicely(green('Done.'))
except:
+ debug_option()
printNicely(
light_magenta('I\'m sorry you can not subscribe to this list.'))
owner_screen_name=owner)
printNicely(green('Done.'))
except:
+ debug_option()
printNicely(
light_magenta('I\'m sorry you can not unsubscribe to this list.'))
"""
Create a new list
"""
- name = raw_input(light_magenta('New list\'s name: '))
- mode = raw_input(light_magenta('New list\'s mode (public/private): '))
- description = raw_input(light_magenta('New list\'s description: '))
+ name = raw_input(light_magenta('New list\'s name: ', rl=True))
+ mode = raw_input(
+ light_magenta(
+ 'New list\'s mode (public/private): ',
+ rl=True))
+ description = raw_input(
+ light_magenta(
+ 'New list\'s description: ',
+ rl=True))
try:
t.lists.create(
name=name,
description=description)
printNicely(green(name + ' list is created.'))
except:
+ debug_option()
printNicely(red('Oops something is wrong with Twitter :('))
"""
Update a list
"""
- slug = raw_input(light_magenta('Your list that you want to update: '))
- name = raw_input(light_magenta('Update name (leave blank to unchange): '))
- mode = raw_input(light_magenta('Update mode (public/private): '))
- description = raw_input(light_magenta('Update description: '))
+ slug = raw_input(
+ light_magenta(
+ 'Your list that you want to update: ',
+ rl=True))
+ name = raw_input(
+ light_magenta(
+ 'Update name (leave blank to unchange): ',
+ rl=True))
+ mode = raw_input(light_magenta('Update mode (public/private): ', rl=True))
+ description = raw_input(light_magenta('Update description: ', rl=True))
try:
if name:
t.lists.update(
description=description)
printNicely(green(slug + ' list is updated.'))
except:
+ debug_option()
printNicely(red('Oops something is wrong with Twitter :('))
"""
Delete a list
"""
- slug = raw_input(light_magenta('Your list that you want to delete: '))
+ slug = raw_input(
+ light_magenta(
+ 'Your list that you want to delete: ',
+ rl=True))
try:
t.lists.destroy(
slug='-'.join(slug.split()),
owner_screen_name=g['original_name'])
printNicely(green(slug + ' list is deleted.'))
except:
+ debug_option()
printNicely(red('Oops something is wrong with Twitter :('))
g['original_name']))
th.daemon = True
th.start()
+ # Stream base on list
+ elif target == 'list':
+ owner, slug = get_slug()
+ # Force python 2 not redraw readline buffer
+ g['cmd'] = '/'.join([owner, slug])
+ printNicely(light_yellow('getting list members ...'))
+ # Get members
+ t = Twitter(auth=authen())
+ members = []
+ next_cursor = -1
+ while next_cursor != 0:
+ m = t.lists.members(
+ slug=slug,
+ owner_screen_name=owner,
+ cursor=next_cursor,
+ include_entities=False)
+ for u in m['users']:
+ members.append('@' + u['screen_name'])
+ next_cursor = m['next_cursor']
+ printNicely(light_yellow('... done.'))
+ # Build thread filter array
+ args.filter = members
+ # Kill old thread
+ g['stream_stop'] = True
+ # Start new thread
+ th = threading.Thread(
+ target=stream,
+ args=(
+ c['USER_DOMAIN'],
+ args,
+ slug))
+ th.daemon = True
+ th.start()
printNicely('')
if args.filter:
- printNicely(cyan('Only: ' + str(args.filter)))
+ printNicely(cyan('Include: ' + str(len(args.filter)) + ' people.'))
if args.ignore:
- printNicely(red('Ignore: ' + str(args.ignore)))
+ printNicely(red('Ignore: ' + str(len(args.ignore)) + ' people.'))
printNicely('')
- except:
+ except Exception:
+ debug_option()
printNicely(red('Sorry I can\'t understand.'))
light_green('trend JP Tokyo') + '.\n'
usage += s * 2 + light_green('home') + ' will show your timeline. ' + \
light_green('home 7') + ' will show 7 tweets.\n'
+ usage += s * 2 + \
+ light_green('notification') + ' will show your recent notification.\n'
usage += s * 2 + light_green('mentions') + ' will show mentions timeline. ' + \
light_green('mentions 7') + ' will show 7 mention tweets.\n'
usage += s * 2 + light_green('whois @mdo') + ' will show profile of ' + \
usage += s * 2 + \
light_green('ufav 12 ') + ' will unfavorite tweet with ' + \
light_yellow('[id=12]') + '.\n'
+ usage += s * 2 + \
+ light_green('share 12 ') + ' will copy the url of the tweet with ' + \
+ light_yellow('[id=12]') + ' to your clipboard.\n'
usage += s * 2 + \
light_green('del 12 ') + ' will delete tweet with ' + \
light_yellow('[id=12]') + '.\n'
' filter will decide nicks will be EXCLUDE.\n'
usage += s * 2 + light_green('switch mine -d') + \
' will use the config\'s ONLY_LIST and IGNORE_LIST.\n'
+ usage += s * 2 + light_green('switch list') + \
+ ' will switch to a Twitter list\'s stream. You will be asked for list name\n'
printNicely(usage)
'switch',
'trend',
'home',
+ 'notification',
'view',
'mentions',
't',
'rep',
'del',
'ufav',
+ 'share',
's',
'mes',
'show',
switch,
trend,
home,
+ notification,
view,
mentions,
tweet,
reply,
delete,
unfavorite,
+ share,
search,
message,
show,
d = dict(zip(
cmdset,
[
- ['public', 'mine'], # switch
+ ['public', 'mine', 'list'], # switch
[], # trend
[], # home
+ [], # notification
['@'], # view
[], # mentions
[], # tweet
[], # reply
[], # delete
[], # unfavorite
+ [], # url
['#'], # search
['@'], # message
['image'], # show image
try:
# raw_input
if g['prefix']:
+ # Only use PREFIX as a string with raw_input
line = raw_input(g['decorated_name'](g['PREFIX']))
else:
line = raw_input()
except EOFError:
printNicely('')
except Exception:
+ debug_option()
printNicely(red('OMG something is wrong with Twitter right now.'))
- break
+
+
+def reconn_notice():
+ """
+ Notice when Hangup or Timeout
+ """
+ guide = light_magenta("You can use ") + \
+ light_green("switch") + \
+ light_magenta(" command to return to your stream.\n")
+ guide += light_magenta("Type ") + \
+ light_green("h stream") + \
+ light_magenta(" for more details.")
+ printNicely(guide)
+ sys.stdout.write(g['decorated_name'](c['PREFIX']))
+ sys.stdout.flush()
+
def stream(domain, args, name='Rainbow Stream'):
"""
if tweet is None:
printNicely("-- None --")
elif tweet is Timeout:
+ # Because the stream check for each 0.3s
+ # so we shouldn't output anything here
if(g['stream_stop']):
StreamLock.release()
break
elif tweet is HeartbeatTimeout:
printNicely("-- Heartbeat Timeout --")
- guide = light_magenta("You can use ") + \
- light_green("switch") + \
- light_magenta(" command to return to your stream.\n")
- guide += light_magenta("Type ") + \
- light_green("h stream") + \
- light_magenta(" for more details.")
- printNicely(guide)
- sys.stdout.write(g['decorated_name'](g['PREFIX']))
- sys.stdout.flush()
+ reconn_notice()
StreamLock.release()
break
elif tweet is Hangup:
printNicely("-- Hangup --")
+ reconn_notice()
+ StreamLock.release()
+ break
elif tweet.get('text'):
# Check the semaphore pause and lock (stream process only)
if g['pause']:
# the 1st character of that word
if current_buffer and g['cmd'] != current_buffer:
sys.stdout.write(
- g['decorated_name'](g['PREFIX']) + str2u(current_buffer))
+ g['decorated_name'](c['PREFIX']) + str2u(current_buffer))
sys.stdout.flush()
elif not c['HIDE_PROMPT']:
- sys.stdout.write(g['decorated_name'](g['PREFIX']))
+ sys.stdout.write(g['decorated_name'](c['PREFIX']))
sys.stdout.flush()
elif tweet.get('direct_message'):
# Check the semaphore pause and lock (stream process only)
while c['lock']:
time.sleep(0.5)
print_message(tweet['direct_message'])
+ elif tweet.get('event'):
+ c['events'].append(tweet)
+ print_event(tweet)
except TwitterHTTPError:
printNicely('')
printNicely(