From: Orakaro Date: Sat, 7 Jun 2014 06:26:26 +0000 (+0900) Subject: image viewer + input history + follow + unfollow X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f5677fb1d3a9e2aa06971500b2c244dcdbe43a3f;p=rainbowstream.git image viewer + input history + follow + unfollow --- diff --git a/README.md b/README.md index 5a4713e..5be9245 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,9 @@ Just click the "Authorize access" button and paste PIN number to the terminal, t #### The interactive mode While your personal stream is continued, you are also ready to tweet, search, reply, retweet... directly from console. -Simply type "h" and hit the Enter key to see the help +Simply type "h" and hit the Enter key to see the help. -Input is in interactive mode. It means that you can use arrow key to move up and down history, tab-autocomplete or 2 tab to view available suggestion +Input is in interactive mode. It means that you can use arrow key to move up and down history, tab-autocomplete or 2 tab to view available suggestion. Input history from previous run is available as well. Here is full list of supported command @@ -73,9 +73,11 @@ __Action Command__ * ```s #noah```will search the word *'noah'*. Result will come back with highlight. -* ```fr```will list all friend (You are following people). +* ```show image 12``` will show the image in tweet with *[id=12]* in your OS's image viewer. -* ```fl```will list all follower. +* ```fl @dtvd88```will follow @dtvd88. + +* ```ufl @dtvd88```will unfollow @dtvd88. * ```h```will show the help. diff --git a/README.rst b/README.rst index f2c760d..4e0516d 100644 --- a/README.rst +++ b/README.rst @@ -39,7 +39,7 @@ Just type and see your stream. -In the case you want to see photos directly in terminal, the command is +In the case you want to see photos directly in terminal, the command is .. code:: bash @@ -54,11 +54,11 @@ The interactive mode While your personal stream is continued, you are also ready to tweet, search, reply, retweet… directly from console. Simply type “h” and hit -the Enter key to see the help +the Enter key to see the help. Input is in interactive mode. It means that you can use arrow key to move up and down history, tab-autocomplete or 2 tab to view available -suggestion +suggestion. Input history from previous run is available as well. Here is full list of supported command @@ -104,9 +104,11 @@ Here is full list of supported command - ``s #noah`` will search the word *‘noah’*. Result will come back with highlight. -- ``fr`` will list all friend (You are following people). +- ``show image 12`` will show the image in tweet with *[id=12]* in your OS's image viewer. -- ``fl`` will list all follower. +- ``fl @dtvd88`` will follow @dtvd88. + +- ``ufl @dtvd88`` will unfollow @dtvd88. - ``h`` will show the help. diff --git a/rainbowstream/c_image.py b/rainbowstream/c_image.py index 59e64b0..ec0e47e 100644 --- a/rainbowstream/c_image.py +++ b/rainbowstream/c_image.py @@ -1,5 +1,4 @@ from PIL import Image -from functools import partial from os.path import join, dirname, getmtime, exists, expanduser from .config import * @@ -34,7 +33,7 @@ def image_to_display(path): width = min(w, int(columns) - 2 * IMAGE_SHIFT) height = int(float(h) * (float(width) / float(w))) height //= 2 - i = i.resize((width, height), Image.BICUBIC) + i = i.resize((width, height), Image.ANTIALIAS) height = min(height, IMAGE_MAX_HEIGHT) for y in xrange(height): diff --git a/rainbowstream/config.py b/rainbowstream/config.py index 24306de..af03a1b 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -2,6 +2,8 @@ SEARCH_MAX_RECORD = 5 # Default home tweet HOME_TWEET_NUM = 5 +# Autocomplete history +HISTORY_FILENAME = 'completer.hist' # Stream Domain USER_DOMAIN = 'userstream.twitter.com' @@ -14,6 +16,6 @@ DOMAIN = USER_DOMAIN ONLY_LIST = [] IGNORE_LIST = [] -# Image size +# Image shift and size IMAGE_SHIFT = 10 -IMAGE_MAX_HEIGHT = 30 \ No newline at end of file +IMAGE_MAX_HEIGHT = 300 \ No newline at end of file diff --git a/rainbowstream/image.c b/rainbowstream/image.c index 96e7a01..10aa6bb 100644 --- a/rainbowstream/image.c +++ b/rainbowstream/image.c @@ -1,3 +1,8 @@ +/* + * This source is borrowed from folowwing link + * https://github.com/jart/fabulous/blob/master/fabulous/_xterm256.c + * I make a slightly change to fit my module here + */ typedef struct { int r; int g; diff --git a/rainbowstream/interactive.py b/rainbowstream/interactive.py index b7fabbc..47a2576 100644 --- a/rainbowstream/interactive.py +++ b/rainbowstream/interactive.py @@ -1,5 +1,9 @@ import readline import rlcompleter +import os.path + +from .config import * + class RainbowCompleter(object): @@ -49,6 +53,31 @@ class RainbowCompleter(object): return response +def get_history_items(): + """ + Get all history item + """ + return [ + readline.get_history_item(i) + for i in xrange(1, readline.get_current_history_length() + 1) + ] + + +def read_history(): + """ + Read history file + """ + if os.path.isfile(HISTORY_FILENAME): + readline.read_history_file(HISTORY_FILENAME) + + +def save_history(): + """ + Save history to file + """ + readline.write_history_file(HISTORY_FILENAME) + + def init_interactive_shell(d): """ Init the rainbow shell diff --git a/rainbowstream/pure_image.py b/rainbowstream/pure_image.py index 11beaa5..8e76c95 100644 --- a/rainbowstream/pure_image.py +++ b/rainbowstream/pure_image.py @@ -5,6 +5,12 @@ from .config import * import sys import os +""" +This file is borrowed from following gist: +https://gist.github.com/MicahElliott/719710 +It's too slow in compare with C program, so bot be used here +""" + CLUT = [ # color look-up table # 8-bit, RGB hex diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index b19ae66..b400529 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -40,8 +40,9 @@ cmdset = [ 'del', 'ufav', 's', - 'fr', + 'show', 'fl', + 'ufl', 'h', 'c', 'q' @@ -81,7 +82,7 @@ def draw(t, imgflg = 0, keyword=None, fil=[], ig=[]): media_url = [] media = t['entities']['media'] for m in media: - media_url = m['media_url'] + media_url.append(m['media_url']) except: media_url = None @@ -145,8 +146,9 @@ def draw(t, imgflg = 0, keyword=None, fil=[], ig=[]): # Display Image if imgflg and media_url: - response = requests.get(media_url) - image_to_display(StringIO(response.content)) + for mu in media_url: + response = requests.get(mu) + image_to_display(StringIO(response.content)) def parse_arguments(): @@ -418,30 +420,57 @@ def search(): printNicely(red('Sorry I can\'t understand.')) -def friend(): +def show(): """ - List of friend (following) + Show image """ t = Twitter(auth=authen()) - g['friends'] = t.friends.ids()['ids'] - for i in g['friends']: - name = t.users.lookup(user_id=i)[0]['name'] - screen_name = '@' + t.users.lookup(user_id=i)[0]['screen_name'] - user = cycle_color(name) + grey(' ' + screen_name + ' ') - print user + try: + target = g['stuff'].split()[0] + if target != 'image': + return + id = int(g['stuff'].split()[1]) + tid = db.rainbow_query(id)[0].tweet_id + tweet = t.statuses.show(id=tid) + media = tweet['entities']['media'] + for m in media: + res = requests.get(m['media_url']) + img = Image.open(StringIO(res.content)) + img.show() + except: + printNicely(red('Sorry I can\'t show this image.')) -def follower(): +def follow(): """ - List of follower + Follow a user """ t = Twitter(auth=authen()) - g['followers'] = t.followers.ids()['ids'] - for i in g['followers']: - name = t.users.lookup(user_id=i)[0]['name'] - screen_name = '@' + t.users.lookup(user_id=i)[0]['screen_name'] - user = cycle_color(name) + grey(' ' + screen_name + ' ') - print user + 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.')) + else: + printNicely(red('Sorry I can\'t understand.')) + + +def unfollow(): + """ + Unfollow a user + """ + 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.')) + else: + printNicely(red('Sorry I can\'t understand.')) def help(): @@ -494,8 +523,12 @@ def help(): yellow('[id=12]') + '.\n' usage += s * 2 + green('s #AKB48') + ' will search for "' + \ yellow('AKB48') + '" and return 5 newest tweet.\n' - usage += s * 2 + green('fr') + ' will list out your following people.\n' - usage += s * 2 + green('fl') + ' will list out your follower.\n' + usage += s * 2 + green('show image 12') + ' will show image in ' + \ + yellow('[id=12]') + 'in your OS\'s image viewer.\n' + usage += s * 2 + green('fl @dtvd88') + ' will follow ' + \ + yellow('@dtvd88') + '.\n' + usage += s * 2 + green('ufl @dtvd88') + ' will unfollow ' + \ + yellow('@dtvd88') + '.\n' usage += s * 2 + green('h') + ' will show this help again.\n' usage += s * 2 + green('c') + ' will clear the screen.\n' usage += s * 2 + green('q') + ' will quit.\n' @@ -516,6 +549,7 @@ def quit(): """ Exit all """ + save_history() os.system('rm -rf rainbow.db') os.kill(g['stream_pid'], signal.SIGKILL) sys.exit() @@ -547,8 +581,9 @@ def process(cmd): delete, unfavorite, search, - friend, - follower, + show, + follow, + unfollow, help, clear, quit @@ -568,17 +603,21 @@ def listen(): ['@'], # view [], # tweet [], # retweet + [], # favorite [], # reply [], # delete + [], # unfavorite ['#'], # search - [], # friend - [], # follower + ['image'], # show image + [], # follow + [], # unfollow [], # help [], # clear [], # quit ] )) init_interactive_shell(d) + read_history() reset() while True: if g['prefix']: