From b8c1f42a02da01fba1757c510a34801f65e9b416 Mon Sep 17 00:00:00 2001 From: Orakaro Date: Sun, 22 Jun 2014 23:33:21 +0900 Subject: [PATCH] refactoring --- README.md | 68 ++++++------ README.rst | 70 +++++++------ rainbowstream/rainbow.py | 220 +++++++++++++++++++-------------------- setup.py | 2 +- 4 files changed, 177 insertions(+), 183 deletions(-) diff --git a/README.md b/README.md index 7027ca9..4fd4ddf 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -## Rainbow Stream +## Rainbow Stream [![Download](http://img.shields.io/pypi/dm/rainbowstream.svg?style=flat)](https://pypi.python.org/pypi/rainbowstream) [![Version](http://img.shields.io/pypi/v/rainbowstream.svg?style=flat)](https://pypi.python.org/pypi/rainbowstream) -Terminal-based Twitter Client. +Terminal-based Twitter Client. Realtime tweetstream, compose, search , favorite ... and much more fun directly from terminal. This package build on the top of [Python Twitter Tool](http://mike.verdone.ca/twitter/) and [Twitter API](https://dev.twitter.com/docs/api/1.1). @@ -46,73 +46,73 @@ Here is full list of supported command __Action Commands__ -* ```trend```will show global trending topics. ```trend US``` will show trends in United States while ```trend JP Tokyo``` will show trends in Tokyo/Japan. +* `trend` will show global trending topics. `trend US` will show trends in United States while `trend JP Tokyo` will show trends in Tokyo/Japan. -* ```home```will show your timeline. ```home 10``` will print exactly 10 tweets. +* `home` will show your timeline. `home 10` will print exactly 10 tweets. -* ```view @mdo```will show @mdo 's timeline. ```view @dmo 9``` will print exactly 9 tweets. +* `view @mdo` will show @mdo 's timeline. `view @dmo 9` will print exactly 9 tweets. -* ```mentions```will show mentions timeline. ```mentions 7``` will show 7 mention tweets. +* `mentions` will show mentions timeline. `mentions 7` will show 7 mention tweets. -* ```t the rainbow is god's promise to noah```will tweet exactly *'the rainbow is god's promise to noah'*. +* `t the rainbow is god's promise to noah` will tweet exactly *'the rainbow is god's promise to noah'*. -* ```rt 12```will retweet the tweet with *[id=12]*. You can see id of each tweet beside the time. +* `rt 12` will retweet the tweet with *[id=12]*. You can see id of each tweet beside the time. -* ```fav 12```will favorite the tweet with *[id=12]*. +* `fav 12` will favorite the tweet with *[id=12]*. -* ```rep 12 Really```will reply *'Really'* to the tweet with *[id=12]*. +* `rep 12 Really` will reply *'Really'* to the tweet with *[id=12]*. -* ```del 12```will delete tweet with *[id=12]*. +* `del 12` will delete tweet with *[id=12]*. -* ```ufav 12```will unfavorite tweet with *[id=12]*. +* `ufav 12` will unfavorite tweet with *[id=12]*. -* ```s #noah```will search the word *'noah'*. Result will come back with highlight. +* `s #noah` will search the word *'noah'*. Result will come back with highlight. -* ```mes @dtvd88 hi```will send a ```hi``` message to @dtvd88. +* `mes @dtvd88 hi` will send a `hi` message to @dtvd88. -* ```show image 12``` will show the image in tweet with *[id=12]* in your OS's image viewer. +* `show image 12` will show the image in tweet with *[id=12]* in your OS's image viewer. -* ```ls fl```will list all your followers (people who are following you). +* `ls fl` will list all your followers (people who are following you). -* ```ls fr```will list all your friends (people who you are following). +* `ls fr` will list all your friends (people who you are following). -* ```inbox```will show inbox messages. ```inbox 7``` will show newest 7 messages. +* `inbox` will show inbox messages. `inbox 7` will show newest 7 messages. -* ```sent```will show sent messages. ```sent 7``` will show newest 7 messages. +* `sent` will show sent messages. `sent 7` will show newest 7 messages. -* ```trash 5```will remove message with *[message_id=5]* +* `trash 5` will remove message with *[message_id=5]* -* ```whois @dtvd88```will show profile of @dtvd88. +* `whois @dtvd88` will show profile of @dtvd88. -* ```fl @dtvd88```will follow @dtvd88. +* `fl @dtvd88` will follow @dtvd88. -* ```ufl @dtvd88```will unfollow @dtvd88. +* `ufl @dtvd88` will unfollow @dtvd88. -* ```h```will show the help. +* `h` will show the help. -* ```c```will clear the screen. +* `c` will clear the screen. -* ```q```will quit. +* `q` will quit. __Stream Commands__ -* ```switch public #AKB48``` will switch current stream to public stream and track keyword ```AKB48``` +* `switch public #AKB48` will switch current stream to public stream and track keyword `AKB48` -* ```switch public #AKB48 -f ``` will do exactly as above but will ask you to provide 2 list: +* `switch public #AKB48 -f ` will do exactly as above but will ask you to provide 2 list: - ```Only nicks``` decide what nicks will be include only. + `Only nicks` decide what nicks will be include only. - ```Ignore nicks```decide what nicks will be exclude. + `Ignore nicks`decide what nicks will be exclude. -* ```switch public #AKB48 -d ``` will apply filter to *ONLY_LIST* and *IGNORE_LIST*. -You can setup 2 list above at ```config.py``` +* `switch public #AKB48 -d ` will apply filter to *ONLY_LIST* and *IGNORE_LIST*. +You can setup 2 list above at `config.py` -* ```switch mine``` will switch current stream to personal stream. ```-f``` and ```-d``` will work as well. +* `switch mine` will switch current stream to personal stream. `-f` and `-d` will work as well. For example see the screenshot above. ## Bug Report -Please [create an issue](https://github.com/DTVD/rainbowstream/issues/new) +Please [create an issue](https://github.com/DTVD/rainbowstream/issues/new) or contact me at [@dtvd88](https://twitter.com/dtvd88) ## License diff --git a/README.rst b/README.rst index 9a774bb..3128de7 100644 --- a/README.rst +++ b/README.rst @@ -1,25 +1,18 @@ Rainbow Stream -------------- -.. image:: http://img.shields.io/pypi/dm/rainbowstream.svg?style=flat - :target: https://pypi.python.org/pypi/rainbowstream - -.. image:: http://img.shields.io/pypi/v/rainbowstream.svg?style=flat - :target: https://pypi.python.org/pypi/rainbowstream +|Download| |Version| Terminal-based Twitter Client. Realtime tweetstream, compose, search , favorite … and much more fun directly from terminal. -This package build on the top of `Python Twitter Tool`_ and `Twitter API`_. +This package build on the top of `Python Twitter Tool`_ and `Twitter +API`_. Screenshot ---------- -.. figure:: https://raw.githubusercontent.com/DTVD/rainbowstream/master/screenshot/RainbowStream.png - :alt: rainbowstream - -.. figure:: https://raw.githubusercontent.com/DTVD/rainbowstream/master/screenshot/RainbowStreamIOT.png - :alt: rainbowstreamIOT +|rainbowstream| |rainbowstreamIOT| Install ------- @@ -44,8 +37,8 @@ Just type and see your stream. -I shipped a feature which can display **tweet's images directly on terminal**. -You can try it with: +I shipped a feature which can display **tweet’s images directly on +terminal**. You can try it with: .. code:: bash @@ -53,7 +46,8 @@ You can try it with: In the first time you will be asked for authorization of Rainbow Stream app at Twitter. Just click the “Authorize access” button and paste PIN -number to the terminal, the rainbow will start. +number to the terminal, the rainbow will start + The interactive mode ^^^^^^^^^^^^^^^^^^^^ @@ -71,8 +65,8 @@ Here is full list of supported command **Action Commands** - ``trend`` will show global trending topics. ``trend US`` will show - trends in United States while ``trend JP Tokyo`` will show trends - in Tokyo/Japan. + trends in United States while ``trend JP Tokyo`` will show trends in + Tokyo/Japan. - ``home`` will show your timeline. ``home 10`` will print exactly 10 tweets. @@ -80,11 +74,11 @@ Here is full list of supported command - ``view @mdo`` will show @mdo ’s timeline. ``view @dmo 9`` will print exactly 9 tweets. -- ``mentions`` will show mentions timeline. ``mentions 7`` will show - 7 mention tweets. +- ``mentions`` will show mentions timeline. ``mentions 7`` will show 7 + mention tweets. - ``t the rainbow is god's promise to noah`` will tweet exactly *‘the - rainbow is god’s promise to noah’* + rainbow is god’s promise to noah’*. - ``rt 12`` will retweet the tweet with *[id=12]*. You can see id of each tweet beside the time. @@ -97,24 +91,26 @@ Here is full list of supported command - ``ufav 12`` will unfavorite tweet with *[id=12]*. -- ``s #noah`` will search the word *‘noah’*. Result will come back - with highlight. +- ``s #noah`` will search the word *‘noah’*. Result will come back with + highlight. - ``mes @dtvd88 hi`` will send a ``hi`` message to @dtvd88. -- ``show image 12`` will show the image in tweet with *[id=12]* in your OS's image viewer. +- ``show image 12`` will show the image in tweet with *[id=12]* in your + OS’s image viewer. -- ``ls fl`` will list all your followers (people who are following you). +- ``ls fl`` will list all your followers (people who are following + you). - ``ls fr`` will list all your friends (people who you are following). -- ``inbox`` will show inbox messages. ``inbox 7`` will show newest - 7 message. +- ``inbox`` will show inbox messages. ``inbox 7`` will show newest 7 + messages. -- ``sent`` will show sent messages. ``sent 7`` will show newest - 7 message. +- ``sent`` will show sent messages. ``sent 7`` will show newest 7 + messages. -- ``trash 5`` will remove message with *[message_id=5]*. +- ``trash 5`` will remove message with *[message\_id=5]* - ``whois @dtvd88`` will show profile of @dtvd88. @@ -130,14 +126,15 @@ Here is full list of supported command **Stream Commands** -- ``switch public #AKB48`` will switch current stream to public stream and track keyword ``AKB48`` +- ``switch public #AKB48`` will switch current stream to public stream + and track keyword ``AKB48`` - ``switch public #AKB48 -f`` will do exactly as above but will ask you to provide 2 list: ``Only nicks`` decide what nicks will be include only. - ``Ignore nicks`` decide what nicks will be exclude. + ``Ignore nicks``\ decide what nicks will be exclude. - ``switch public #AKB48 -d`` will apply filter to *ONLY\_LIST* and *IGNORE\_LIST*. You can setup 2 list above at ``config.py`` @@ -145,13 +142,13 @@ Here is full list of supported command - ``switch mine`` will switch current stream to personal stream. ``-f`` and ``-d`` will work as well. - For example see the screenshot above. Bug Report ---------- -Please `create an issue`_ or contact me at `@dtvd88`_ +Please `create an issue`_ or contact me at +[@dtvd88](https://twitter.com/dtvd88) License ------- @@ -162,6 +159,11 @@ details .. _Python Twitter Tool: http://mike.verdone.ca/twitter/ .. _Twitter API: https://dev.twitter.com/docs/api/1.1 -.. _EarthQuake: https://github.com/jugyo/earthquake .. _create an issue: https://github.com/DTVD/rainbowstream/issues/new -.. _@dtvd88: https://twitter.com/dtvd88 + +.. |Download| image:: http://img.shields.io/pypi/dm/rainbowstream.svg?style=flat + :target: https://pypi.python.org/pypi/rainbowstream +.. |Version| image:: http://img.shields.io/pypi/v/rainbowstream.svg?style=flat + :target: https://pypi.python.org/pypi/rainbowstream +.. |rainbowstream| image:: ./screenshot/RainbowStream.png +.. |rainbowstreamIOT| image:: ./screenshot/RainbowStreamIOT.png \ No newline at end of file diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index b0373e1..5e911b4 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -520,10 +520,11 @@ def retweet(): t = Twitter(auth=authen()) try: id = int(g['stuff'].split()[0]) - tid = db.rainbow_to_tweet_query(id)[0].tweet_id - t.statuses.retweet(id=tid, include_entities=False, trim_user=True) except: - printNicely(red('Sorry I can\'t retweet for you.')) + printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(id)[0].tweet_id + t.statuses.retweet(id=tid, include_entities=False, trim_user=True) def favorite(): @@ -533,13 +534,14 @@ def favorite(): t = Twitter(auth=authen()) try: id = int(g['stuff'].split()[0]) - tid = db.rainbow_to_tweet_query(id)[0].tweet_id - t.favorites.create(_id=tid, include_entities=False) - printNicely(green('Favorited.')) - draw(t.statuses.show(id=tid), iot=g['iot']) - printNicely('') except: - printNicely(red('Omg some syntax is wrong.')) + printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(id)[0].tweet_id + t.favorites.create(_id=tid, include_entities=False) + printNicely(green('Favorited.')) + draw(t.statuses.show(id=tid), iot=g['iot']) + printNicely('') def reply(): @@ -549,13 +551,14 @@ def reply(): t = Twitter(auth=authen()) try: id = int(g['stuff'].split()[0]) - tid = db.rainbow_to_tweet_query(id)[0].tweet_id - user = t.statuses.show(id=tid)['user']['screen_name'] - status = ' '.join(g['stuff'].split()[1:]) - status = '@' + user + ' ' + status.decode('utf-8') - t.statuses.update(status=status, in_reply_to_status_id=tid) except: printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(id)[0].tweet_id + user = t.statuses.show(id=tid)['user']['screen_name'] + status = ' '.join(g['stuff'].split()[1:]) + status = '@' + user + ' ' + status.decode('utf-8') + t.statuses.update(status=status, in_reply_to_status_id=tid) def delete(): @@ -565,11 +568,12 @@ def delete(): t = Twitter(auth=authen()) try: rid = int(g['stuff'].split()[0]) - tid = db.rainbow_to_tweet_query(rid)[0].tweet_id - t.statuses.destroy(id=tid) - printNicely(green('Okay it\'s gone.')) except: printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(rid)[0].tweet_id + t.statuses.destroy(id=tid) + printNicely(green('Okay it\'s gone.')) def unfavorite(): @@ -579,13 +583,14 @@ def unfavorite(): t = Twitter(auth=authen()) try: id = int(g['stuff'].split()[0]) - tid = db.rainbow_to_tweet_query(id)[0].tweet_id - t.favorites.destroy(_id=tid) - printNicely(green('Okay it\'s unfavorited.')) - draw(t.statuses.show(id=tid), iot=g['iot']) - printNicely('') except: - printNicely(red('Sorry I can\'t unfavorite this tweet for you.')) + printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(id)[0].tweet_id + t.favorites.destroy(_id=tid) + printNicely(green('Okay it\'s unfavorited.')) + draw(t.statuses.show(id=tid), iot=g['iot']) + printNicely('') def search(): @@ -593,22 +598,19 @@ def search(): Search """ t = Twitter(auth=authen()) - try: - if g['stuff'][0] == '#': - rel = t.search.tweets(q=g['stuff'])['statuses'] - if len(rel): - printNicely('Newest tweets:') - for i in reversed(xrange(SEARCH_MAX_RECORD)): - draw(t=rel[i], - iot=g['iot'], - keyword=g['stuff'].strip()[1:]) - printNicely('') - else: - printNicely(magenta('I\'m afraid there is no result')) + if g['stuff'].startswith('#'): + rel = t.search.tweets(q=g['stuff'])['statuses'] + if len(rel): + printNicely('Newest tweets:') + for i in reversed(xrange(SEARCH_MAX_RECORD)): + draw(t=rel[i], + iot=g['iot'], + keyword=g['stuff'].strip()[1:]) + printNicely('') else: - printNicely(red('A keyword should be a hashtag (like \'#AKB48\')')) - except: - printNicely(red('Sorry I can\'t understand.')) + printNicely(magenta('I\'m afraid there is no result')) + else: + printNicely(red('A keyword should be a hashtag (like \'#AKB48\')')) def message(): @@ -617,16 +619,16 @@ def message(): """ t = Twitter(auth=authen()) user = g['stuff'].split()[0] - if user[0] == '@': + if user[0].startswith('@'): try: content = g['stuff'].split()[1] - t.direct_messages.new( - screen_name=user[1:], - text=content - ) - printNicely(green('Message sent.')) except: printNicely(red('Sorry I can\'t understand.')) + t.direct_messages.new( + screen_name=user[1:], + text=content + ) + printNicely(green('Message sent.')) else: printNicely(red('A name should begin with a \'@\'')) @@ -660,7 +662,7 @@ def list(): # Get name try: name = g['stuff'].split()[1] - if name[0] == '@': + if name.startswith('@'): name = name[1:] else: printNicely(red('A name should begin with a \'@\'')) @@ -670,27 +672,28 @@ def list(): # Get list followers or friends try: target = g['stuff'].split()[0] - d = {'fl': 'followers', 'fr': 'friends'} - next_cursor = -1 - rel = {} - # Cursor loop - while next_cursor != 0: - list = getattr(t, d[target]).list( - screen_name=name, - cursor=next_cursor, - skip_status=True, - include_entities=False, - ) - for u in list['users']: - rel[u['name']] = '@' + u['screen_name'] - next_cursor = list['next_cursor'] - # Print out result - printNicely('All: ' + str(len(rel)) + ' people.') - for name in rel: - user = ' ' + cycle_color(name) + grey(' ' + rel[name] + ' ') - printNicely(user) except: printNicely(red('Omg some syntax is wrong.')) + # Init cursor + d = {'fl': 'followers', 'fr': 'friends'} + next_cursor = -1 + rel = {} + # Cursor loop + while next_cursor != 0: + list = getattr(t, d[target]).list( + screen_name=name, + cursor=next_cursor, + skip_status=True, + include_entities=False, + ) + for u in list['users']: + rel[u['name']] = '@' + u['screen_name'] + next_cursor = list['next_cursor'] + # Print out result + printNicely('All: ' + str(len(rel)) + ' people.') + for name in rel: + user = ' ' + cycle_color(name) + grey(' ' + rel[name] + ' ') + printNicely(user) def inbox(): @@ -766,11 +769,11 @@ def trash(): t = Twitter(auth=authen()) try: rid = int(g['stuff'].split()[0]) - mid = db.rainbow_to_message_query(rid)[0].message_id - t.direct_messages.destroy(id=mid) - printNicely(green('Message deleted.')) except: printNicely(red('Sorry I can\'t understand.')) + mid = db.rainbow_to_message_query(rid)[0].message_id + t.direct_messages.destroy(id=mid) + printNicely(green('Message deleted.')) def whois(): @@ -779,7 +782,7 @@ def whois(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': + if screen_name.startswith('@'): try: user = t.users.show( screen_name=screen_name[1:], @@ -788,7 +791,7 @@ def whois(): except: printNicely(red('Omg no user.')) else: - printNicely(red('Sorry I can\'t understand.')) + printNicely(red('A name should begin with a \'@\'')) def follow(): @@ -797,14 +800,11 @@ def follow(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': - try: - t.friendships.create(screen_name=screen_name[1:], follow=True) - printNicely(green('You are following ' + screen_name + ' now!')) - except: - printNicely(red('Sorry can not follow at this time.')) + if screen_name.startswith('@'): + t.friendships.create(screen_name=screen_name[1:], follow=True) + printNicely(green('You are following ' + screen_name + ' now!')) else: - printNicely(red('Sorry I can\'t understand.')) + printNicely(red('A name should begin with a \'@\'')) def unfollow(): @@ -813,16 +813,13 @@ def unfollow(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': - try: - t.friendships.destroy( - screen_name=screen_name[1:], - include_entities=False) - printNicely(green('Unfollow ' + screen_name + ' success!')) - except: - printNicely(red('Sorry can not unfollow at this time.')) + if screen_name.startswith('@'): + t.friendships.destroy( + screen_name=screen_name[1:], + include_entities=False) + printNicely(green('Unfollow ' + screen_name + ' success!')) else: - printNicely(red('Sorry I can\'t understand.')) + printNicely(red('A name should begin with a \'@\'')) def block(): @@ -831,17 +828,14 @@ def block(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': - try: - t.blocks.create( - screen_name=screen_name[1:], - include_entities=False, - skip_status=True) - printNicely(green('You blocked ' + screen_name + '.')) - except: - printNicely(red('Sorry something went wrong.')) + if screen_name.startswith('@'): + t.blocks.create( + screen_name=screen_name[1:], + include_entities=False, + skip_status=True) + printNicely(green('You blocked ' + screen_name + '.')) else: - printNicely(red('Sorry I can\'t understand.')) + printNicely(red('A name should begin with a \'@\'')) def unblock(): @@ -850,17 +844,14 @@ def unblock(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': - try: - t.blocks.destroy( - screen_name=screen_name[1:], - include_entities=False, - skip_status=True) - printNicely(green('Unblock ' + screen_name + ' success!')) - except: - printNicely(red('Sorry something went wrong.')) + if screen_name.startswith('@'): + t.blocks.destroy( + screen_name=screen_name[1:], + include_entities=False, + skip_status=True) + printNicely(green('Unblock ' + screen_name + ' success!')) else: - printNicely(red('Sorry I can\'t understand.')) + printNicely(red('A name should begin with a \'@\'')) def report(): @@ -869,13 +860,10 @@ def report(): """ t = Twitter(auth=authen()) screen_name = g['stuff'].split()[0] - if screen_name[0] == '@': - try: - t.users.report_spam( - screen_name=screen_name[1:]) - printNicely(green('You reported ' + screen_name + '.')) - except: - printNicely(red('Sorry something went wrong.')) + if screen_name.startswith('@'): + t.users.report_spam( + screen_name=screen_name[1:]) + printNicely(green('You reported ' + screen_name + '.')) else: printNicely(red('Sorry I can\'t understand.')) @@ -1085,8 +1073,12 @@ def listen(): except: cmd = '' # Save cmd to global variable and call process - g['stuff'] = ' '.join(line.split()[1:]) - process(cmd)() + try: + g['stuff'] = ' '.join(line.split()[1:]) + process(cmd)() + except Exception: + printNicely(red('OMG something is wrong with Twitter right now.')) + # Not redisplay prefix if cmd in ['switch', 't', 'rt', 'rep']: g['prefix'] = False else: diff --git a/setup.py b/setup.py index 2ebe87b..f4c47c6 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '0.0.6' +version = '0.0.7' install_requires = [ "SQLAlchemy", -- 2.25.1