From 2d34102927771794440b85c6b15c4a1d35f23dd0 Mon Sep 17 00:00:00 2001 From: Orakaro Date: Sun, 13 Jul 2014 00:18:39 +0900 Subject: [PATCH] add list support and seperate help --- README.md | 2 + README.rst | 32 +++ docs/index.rst | 36 ++- rainbowstream/colorset/default.json | 11 + rainbowstream/colorset/larapaste.json | 9 + rainbowstream/colorset/monokai.json | 9 + rainbowstream/colorset/solarized.json | 9 + rainbowstream/colorset/tomorrow_night.json | 9 + rainbowstream/db.py | 20 ++ rainbowstream/draw.py | 78 ++++++ rainbowstream/rainbow.py | 279 ++++++++++++++++++--- rainbowstream/table_def.py | 13 + 12 files changed, 476 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d5b53ef..b29ef69 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ CONSUMER_SECRET = 'APISecret' # Your Twitter application's API secret * Use pip to install in local ```bash # cd to directory which contains setup.py +virtualenv venv # Python3 users: use -p to specify python3 +source venv/bin/activate pip install -e . pip list | grep rainbowstream # confirm installed version rainbowstream # local version of rainbowstream diff --git a/README.rst b/README.rst index d97214f..b73506b 100644 --- a/README.rst +++ b/README.rst @@ -110,6 +110,36 @@ Bug and feature requests Found a bug or a feature request ? Please `create an issue`_ or contact me at `@dtvd88`_ +Development +----------- + +If you want to build a runnable version yourself, follow these simple +steps + +- `Create your own Twitter Application`_ +- Get your Twitter application’s API key and secret +- Fork github's repo +- Create a file ``consumer.py`` in ```rainbowstream```_ folder with + following content + + .. code:: python + + # Consumer information + CONSUMER_KEY = 'APIKey' # Your Twitter application's API key + CONSUMER_SECRET = 'APISecret' # Your Twitter application's API secret + +- Use pip to install in local + + .. code:: bash + + # cd to directory which contains setup.py + virtualenv venv # Python3 users: use -p to specify python3 + source venv/bin/activate + pip install -e . + pip list | grep rainbowstream # confirm installed version + rainbowstream # local version of rainbowstream + + Contributing ------------ I appreciate any help and support. Feel free to `fork`_ and `create a pull request`_. @@ -131,3 +161,5 @@ details .. _Read The Docs: http://rainbowstream.readthedocs.org/en/latest/ .. _config guide: https://github.com/DTVD/rainbowstream/blob/master/theme.md .. _theme usage and customization: https://github.com/DTVD/rainbowstream/blob/master/theme.md +.. _Create your own Twitter Application: https://apps.twitter.com/app/new +.. _``rainbowstream``: https://github.com/DTVD/rainbowstream/tree/master/rainbowstream diff --git a/docs/index.rst b/docs/index.rst index 55c9092..e4ed9a0 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -178,11 +178,11 @@ Here is full list of supported command: **Screening Commands** -- ``theme`` will list available themes. - +- ``theme`` will list available themes. + + ``theme monokai`` will apply *monokai* theme immedaitely. + ``theme current_as_default`` will remember current theme as next time default. - + - ``h`` will show the help. - ``c`` will clear the screen. @@ -221,7 +221,37 @@ Theme’s screenshot: For detaile information, see `theme usage and customization`_. +Development +----------- + +If you want to build a runnable version yourself, follow these simple +steps + +- `Create your own Twitter Application`_ +- Get your Twitter application’s API key and secret +- Fork github's repo +- Create a file ``consumer.py`` in ```rainbowstream```_ folder with + following content + + .. code:: python + + # Consumer information + CONSUMER_KEY = 'APIKey' # Your Twitter application's API key + CONSUMER_SECRET = 'APISecret' # Your Twitter application's API secret + +- Use pip to install in local + + .. code:: bash + + # cd to directory which contains setup.py + virtualenv venv # Python3 users: use -p to specify python3 + source venv/bin/activate + pip install -e . + pip list | grep rainbowstream # confirm installed version + rainbowstream # local version of rainbowstream +.. _Create your own Twitter Application: https://apps.twitter.com/app/new +.. _``rainbowstream``: https://github.com/DTVD/rainbowstream/tree/master/rainbowstream .. _Python Twitter Tool: http://mike.verdone.ca/twitter/ .. _Twitter API: https://dev.twitter.com/docs/api/1.1 .. _theme usage and customization: https://github.com/DTVD/rainbowstream/blob/master/theme.md diff --git a/rainbowstream/colorset/default.json b/rainbowstream/colorset/default.json index 65bc20c..f2b23a4 100644 --- a/rainbowstream/colorset/default.json +++ b/rainbowstream/colorset/default.json @@ -9,6 +9,8 @@ "MESSAGES_DISPLAY" : 5, // 'trend': max trending topics "TREND_MAX" : 10, + // List home timeline max + "LIST_MAX" : 5, // 'switch': Filter and Ignore list ex: ['@fat','@mdo'] "ONLY_LIST" : [], "IGNORE_LIST" : [], @@ -84,5 +86,14 @@ "CAL" : { "days": "grey", "today": "on_grey" + }, + + "GROUP" : { + "name": "grey", + "member": "light_green", + "subscriber": "light_green", + "mode": "grey", + "description": "light_yellow", + "clock": "grey" } } diff --git a/rainbowstream/colorset/larapaste.json b/rainbowstream/colorset/larapaste.json index ed69b07..990b4f8 100644 --- a/rainbowstream/colorset/larapaste.json +++ b/rainbowstream/colorset/larapaste.json @@ -59,5 +59,14 @@ "CAL" : { "days": "term_209", "today": "on_light_magenta" + }, + + "GROUP" : { + "name": "term_73", + "member": "term_220", + "subscriber": "term_73", + "mode": "term_59", + "description": "term_230", + "clock": "term_220" } } diff --git a/rainbowstream/colorset/monokai.json b/rainbowstream/colorset/monokai.json index 7d9a7bf..dc71fda 100644 --- a/rainbowstream/colorset/monokai.json +++ b/rainbowstream/colorset/monokai.json @@ -59,5 +59,14 @@ "CAL" : { "days": "term_57", "today": "on_light_blue" + }, + + "GROUP" : { + "name": "term_112", + "member": "term_57", + "subscriber": "term_198", + "mode": "term_112", + "description": "term_166", + "clock": "term_57" } } diff --git a/rainbowstream/colorset/solarized.json b/rainbowstream/colorset/solarized.json index 071db1d..b3b3c2c 100644 --- a/rainbowstream/colorset/solarized.json +++ b/rainbowstream/colorset/solarized.json @@ -59,5 +59,14 @@ "CAL" : { "days": "term_64", "today": "light_green" + }, + + "GROUP" : { + "name": "term_64", + "member": "term_130", + "subscriber": "term_32", + "mode": "term_164", + "description": "term_66", + "clock": "term_32" } } diff --git a/rainbowstream/colorset/tomorrow_night.json b/rainbowstream/colorset/tomorrow_night.json index ecc5fd8..9dcd03a 100644 --- a/rainbowstream/colorset/tomorrow_night.json +++ b/rainbowstream/colorset/tomorrow_night.json @@ -59,5 +59,14 @@ "CAL" : { "days": "term_103", "today": "on_light_blue" + }, + + "GROUP" : { + "name": "term_103", + "member": "term_179", + "subscriber": "term_30", + "mode": "term_54", + "description": "term_58", + "clock": "term_173" } } diff --git a/rainbowstream/db.py b/rainbowstream/db.py index 5bfb040..2ab23bc 100644 --- a/rainbowstream/db.py +++ b/rainbowstream/db.py @@ -98,3 +98,23 @@ class RainbowDB(): session = Session() res = session.query(Theme).all() return res + + def list_store(self, list_id, list_name): + """ + Store list id and name + """ + Session = sessionmaker(bind=self.engine) + session = Session() + l = List(list_id, list_name) + session.add(l) + session.commit() + + def list_name_to_id_query(self, list_name): + """ + Query base of list id + """ + Session = sessionmaker(bind=self.engine) + session = Session() + res = session.query(List).filter_by(list_name=list_name).all() + return res + diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 57e7867..1b241c3 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -411,3 +411,81 @@ def print_trends(trends): line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) printNicely(line) printNicely('') + + +def print_list(group): + """ + Display a list + """ + for g in group: + # Format + name = g['name'] + name = color_func(c['GROUP']['name'])(name + ' : ') + member = str(g['member_count']) + member = color_func(c['GROUP']['member'])(member+' member') + subscriber = str(g['subscriber_count']) + subscriber = color_func(c['GROUP']['subscriber'])(subscriber+' subscriber') + description = g['description'].strip() + description = color_func(c['GROUP']['description'])(description) + mode = g['mode'] + mode = color_func(c['GROUP']['mode'])('Type: '+mode) + created_at = g['created_at'] + date = parser.parse(created_at) + date = date - datetime.timedelta(seconds=time.timezone) + clock = date.strftime('%Y/%m/%d %H:%M:%S') + clock = 'Created at ' + color_func(c['GROUP']['clock'])(clock) + + # Save to db + db.list_store(g['id'],g['name']) + + # Create lines + line1 = ' '*2 + name + member + ' ' + subscriber + line2 = ' '*4 + description + line3 = ' '*4 + mode + line4 = ' '*4 + clock + + # Display + printNicely('') + printNicely(line1) + printNicely(line2) + printNicely(line3) + printNicely(line4) + + printNicely('') + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 43466ec..3ecd9a6 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -60,6 +60,7 @@ cmdset = [ 'block', 'unblock', 'report', + 'list', 'cal', 'theme', 'h', @@ -514,7 +515,7 @@ def urlopen(): printNicely(red('Sorry I can\'t open url in this tweet.')) -def list(): +def ls(): """ List friends for followers """ @@ -550,9 +551,10 @@ def list(): rel[u['name']] = '@' + u['screen_name'] next_cursor = list['next_cursor'] # Print out result - printNicely('All: ' + str(len(rel)) + ' people.') + printNicely('All: ' + str(len(rel)) + ' ' + d[target] + '.') for name in rel: - user = ' ' + cycle_color(name) + grey(' ' + rel[name] + ' ') + user = ' ' + cycle_color(name) + user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') printNicely(user) @@ -744,7 +746,8 @@ def muting(): # Print out result printNicely('All: ' + str(len(rel)) + ' people.') for name in rel: - user = ' ' + cycle_color(name) + grey(' ' + rel[name] + ' ') + user = ' ' + cycle_color(name) + user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') printNicely(user) @@ -794,6 +797,139 @@ def report(): printNicely(red('Sorry I can\'t understand.')) +def show_lists(t): + """ + list list + """ + rel = t.lists.list(screen_name=g['original_name']) + if rel: + print_list(rel) + else: + printNicely(light_magenta('You belong to no lists :)')) + + +def list_home(t): + """ + List home + """ + # Get list name + list_name = raw_input(light_magenta('Give me the list\'s name: ')) + if not list_name: + printNicely(light_magenta('No list specified.')) + return + # Print timeline + l = db.list_name_to_id_query(list_name)[0] + res = t.lists.statuses( + list_id = l.list_id, + count = c['LIST_MAX'], + include_entities=False) + for tweet in res: + draw(t=tweet) + printNicely('') + + +def list_members(t): + """ + List members + """ + # Get list name + list_name = raw_input(light_magenta('Give me the list\'s name: ')) + if not list_name: + printNicely(light_magenta('No list specified.')) + return + # Members + l = db.list_name_to_id_query(list_name)[0] + rel = {} + next_cursor = -1 + while next_cursor != 0 : + m = t.lists.members( + list_id = l.list_id, + cursor = next_cursor, + include_entities=False) + for u in m['users']: + rel[u['name']] = '@' + u['screen_name'] + next_cursor = m['next_cursor'] + printNicely('All: ' + str(len(rel)) + ' members.') + for name in rel: + user = ' ' + cycle_color(name) + user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') + printNicely(user) + + +def list_subscribers(t): + """ + List subscribers + """ + # Get list name + list_name = raw_input(light_magenta('Give me the list\'s name: ')) + if not list_name: + printNicely(light_magenta('No list specified.')) + return + # Subscribers + l = db.list_name_to_id_query(list_name)[0] + rel = {} + next_cursor = -1 + while next_cursor != 0 : + m = t.lists.subscribers( + list_id = l.list_id, + cursor = next_cursor, + include_entities=False) + for u in m['users']: + rel[u['name']] = '@' + u['screen_name'] + next_cursor = m['next_cursor'] + printNicely('All: ' + str(len(rel)) + ' subscribers.') + for name in rel: + user = ' ' + cycle_color(name) + user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') + printNicely(user) + + +def list_remove(t): + """ + Remove specific user from a list + """ + # Get list name + list_name = raw_input(light_magenta('Give me the list\'s name: ')) + if not list_name: + printNicely(light_magenta('No list specified.')) + return + # Remove + l = db.list_name_to_id_query(list_name)[0] + user_name = raw_input(light_magenta('Give me the name of unlucky man: ')) + try: + t.lists.members.destroy( + list_id = l.list_id, + screen_name = user_name) + printNicely(light_green('Okay he\'s gone :)')) + except: + printNicely(light_magenta('I\'m sorry we can not remove him.')) + + +def list(): + """ + Twitter's list + """ + t = Twitter(auth=authen()) + # List all lists or base on action + try: + g['list_action'] = g['stuff'].split()[0] + except: + show_lists(t) + return + + # Sub function + action_ary = { + 'home': list_home, + 'all_mem': list_members, + 'all_sub': list_subscribers, + 'rm': list_remove, + } + try: + return action_ary[g['list_action']](t) + except: + printNicely(red('Sorry I can\'t understand.')) + + def cal(): """ Unix's command `cal` @@ -869,25 +1005,13 @@ def theme(): printNicely(red('No such theme exists.')) -def help(): +def help_discover(): """ - Help + Discover the world """ s = ' ' * 2 - h, w = os.popen('stty size', 'r').read().split() - - # Start - usage = '\n' - usage += s + 'Hi boss! I\'m ready to serve you right now!\n' - usage += s + '-' * (int(w) - 4) + '\n' - usage += s + 'You are ' + \ - light_yellow('already') + ' on your personal stream.\n' - usage += s + 'Any update from Twitter will show up ' + \ - light_yellow('immediately') + '.\n' - usage += s + 'In addtion, following commands are available right now:\n' - # Discover the world - usage += '\n' + usage = '\n' usage += s + grey(u'\u266A' + ' Discover the world \n') usage += s * 2 + light_green('trend') + ' will show global trending topics. ' + \ 'You can try ' + light_green('trend US') + ' or ' + \ @@ -902,9 +1026,16 @@ def help(): ' will show ' + magenta('@mdo') + '\'s home.\n' usage += s * 2 + light_green('s #AKB48') + ' will search for "' + \ light_yellow('AKB48') + '" and return 5 newest tweet.\n' + printNicely(usage) + +def help_tweets(): + """ + Tweets + """ + s = ' ' * 2 # Tweet - usage += '\n' + usage = '\n' usage += s + grey(u'\u266A' + ' Tweets \n') usage += s * 2 + light_green('t oops ') + \ 'will tweet "' + light_yellow('oops') + '" immediately.\n' @@ -934,9 +1065,16 @@ def help(): light_yellow('[id=12]') + ' in your OS\'s image viewer.\n' usage += s * 2 + light_green('open 12') + ' will open url in tweet with ' + \ light_yellow('[id=12]') + ' in your OS\'s default browser.\n' + printNicely(usage) + +def help_messages(): + """ + Messages + """ + s = ' ' * 2 # Direct message - usage += '\n' + usage = '\n' usage += s + grey(u'\u266A' + ' Direct messages \n') usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \ light_green('inbox 7') + ' will show newest 7 messages.\n' @@ -946,9 +1084,16 @@ def help(): magenta('@dtvd88') + '.\n' usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \ light_yellow('[message_id=5]') + '.\n' + printNicely(usage) + +def help_friends_and_followers(): + """ + Friends and Followers + """ + s = ' ' * 2 # Follower and following - usage += '\n' + usage = '\n' usage += s + grey(u'\u266A' + ' Friends and followers \n') usage += s * 2 + \ light_green('ls fl') + \ @@ -971,9 +1116,38 @@ def help(): magenta('@dtvd88') + '.\n' usage += s * 2 + light_green('report @dtvd88') + ' will report ' + \ magenta('@dtvd88') + ' as a spam account.\n' + printNicely(usage) + + +def help_list(): + """ + Lists + """ + s = ' ' * 2 + # Twitter list + usage = '\n' + usage += s + grey(u'\u266A' + ' Twitter list\n') + 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' + usage += s * 2 + light_green('list mem') + \ + ' will show list\'s all members.\n' + usage += s * 2 + light_green('list sub') + \ + ' will show list\'s all subscribers.\n' + usage += s * 2 + light_green('list rm') + \ + ' will remove specific person from a list owned by you.' + \ + ' You will be asked for list\'s name and person\'s name.\n' + printNicely(usage) + +def help_stream(): + """ + Stream switch + """ + s = ' ' * 2 # Switch - usage += '\n' + usage = '\n' usage += s + grey(u'\u266A' + ' Switching streams \n') usage += s * 2 + light_green('switch public #AKB') + \ ' will switch to public stream and follow "' + \ @@ -988,6 +1162,41 @@ def help(): ' 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' + printNicely(usage) + + +def help(): + """ + Help + """ + s = ' ' * 2 + h, w = os.popen('stty size', 'r').read().split() + + # Start + usage = '\n' + usage += s + 'Hi boss! I\'m ready to serve you right now!\n' + usage += s + '-' * (int(w) - 4) + '\n' + usage += s + 'You are ' + \ + light_yellow('already') + ' on your personal stream.\n' + usage += s + 'Any update from Twitter will show up ' + \ + light_yellow('immediately') + '.\n' + usage += s + 'In addtion, following commands are available right now:\n' + + # Twitter help section + usage += '\n' + usage += s + grey(u'\u266A' + ' Twitter help\n') + usage += s * 2 + light_green('h discover') + \ + ' will show help for discover commands.\n' + usage += s * 2 + light_green('h tweets') + \ + ' will show help for tweets commands.\n' + usage += s * 2 + light_green('h messages') + \ + ' will show help for messages commands.\n' + usage += s * 2 + light_green('h friends_and_followers') + \ + ' will show help for friends and followers commands.\n' + usage += s * 2 + light_green('h list') + \ + ' will show help for list commands.\n' + usage += s * 2 + light_green('h stream') + \ + ' will show help for stream commands.\n' # Smart shell usage += '\n' @@ -1011,7 +1220,20 @@ def help(): usage += '\n' usage += s + '-' * (int(w) - 4) + '\n' usage += s + 'Have fun and hang tight! \n' - printNicely(usage) + + # Show help + d = { + 'discover' : help_discover, + 'tweets' : help_tweets, + 'messages' : help_messages, + 'friends_and_followers' : help_friends_and_followers, + 'list' : help_list, + 'stream' : help_stream, + } + if g['stuff']: + d[g['stuff'].strip()]() + else: + printNicely(usage) def clear(): @@ -1068,7 +1290,7 @@ def process(cmd): message, show, urlopen, - list, + ls, inbox, sent, trash, @@ -1081,6 +1303,7 @@ def process(cmd): block, unblock, report, + list, cal, theme, help, @@ -1127,9 +1350,10 @@ def listen(): ['@'], # block ['@'], # unblock ['@'], # report + ['home','all_mem','all_sub','rm'], # list [], # cal g['themes'] + ['current_as_default'], # theme - [], # help + ['discover','tweets','messages','friends_and_followers','list','stream'], # help [], # clear [], # quit ] @@ -1151,8 +1375,7 @@ def listen(): try: g['stuff'] = ' '.join(line.split()[1:]) process(cmd)() - except Exception,e : - print e + except Exception: printNicely(red('OMG something is wrong with Twitter right now.')) # Not redisplay prefix if cmd in ['switch', 't', 'rt', 'rep']: diff --git a/rainbowstream/table_def.py b/rainbowstream/table_def.py index fda3948..28bbfc9 100644 --- a/rainbowstream/table_def.py +++ b/rainbowstream/table_def.py @@ -38,5 +38,18 @@ class Theme(Base): self.theme_name = theme_name +class List(Base): + + __tablename__ = "list" + + list_rid = Column(Integer, primary_key=True) + list_id = Column(Integer) + list_name = Column(String(50)) + + def __init__(self, list_id, list_name): + self.list_id = list_id + self.list_name = list_name + + def init_db(): Base.metadata.create_all(engine) -- 2.25.1