From 4592d23132bd4089ce643f90bd22a66d712e90db Mon Sep 17 00:00:00 2001 From: vunhat_minh Date: Fri, 20 Jun 2014 13:00:28 +0900 Subject: [PATCH] add trend --- rainbowstream/c_image.py | 12 +++++--- rainbowstream/config.py | 2 ++ rainbowstream/pure_image.py | 12 +++++--- rainbowstream/rainbow.py | 57 +++++++++++++++++++++++++++++++------ 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/rainbowstream/c_image.py b/rainbowstream/c_image.py index ec0e47e..bc2112e 100644 --- a/rainbowstream/c_image.py +++ b/rainbowstream/c_image.py @@ -24,20 +24,24 @@ def pixel_print(ansicolor): sys.stdout.write('\033[48;5;%sm \033[0m' % (ansicolor)) -def image_to_display(path): +def image_to_display(path,start=None,length=None): + rows, columns = os.popen('stty size', 'r').read().split() + if not start: + start = IMAGE_SHIFT + if not length: + length = int(columns) - 2 * start i = Image.open(path) i = i.convert('RGBA') w, h = i.size i.load() - rows, columns = os.popen('stty size', 'r').read().split() - width = min(w, int(columns) - 2 * IMAGE_SHIFT) + width = min(w, length) height = int(float(h) * (float(width) / float(w))) height //= 2 i = i.resize((width, height), Image.ANTIALIAS) height = min(height, IMAGE_MAX_HEIGHT) for y in xrange(height): - print ' ' * IMAGE_SHIFT, + print ' ' * start, for x in xrange(width): p = i.getpixel((x, y)) r, g, b = p[:3] diff --git a/rainbowstream/config.py b/rainbowstream/config.py index ed9b348..27b01cc 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -4,6 +4,8 @@ SEARCH_MAX_RECORD = 5 HOME_TWEET_NUM = 5 # Default direct message's number MESSAGES_DISPLAY = 5 +# Max trending topics display +TREND_MAX = 5 # Autocomplete history HISTORY_FILENAME = 'completer.hist' diff --git a/rainbowstream/pure_image.py b/rainbowstream/pure_image.py index 8e76c95..87b9f1b 100644 --- a/rainbowstream/pure_image.py +++ b/rainbowstream/pure_image.py @@ -316,20 +316,24 @@ def rgb2short(r, g, b): return RGB2SHORT_DICT[rgb_to_hex(m)] -def image_to_display(path): +def image_to_display(path,start=None,length=None): + rows, columns = os.popen('stty size', 'r').read().split() + if not start: + start = IMAGE_SHIFT + if not length: + length = int(columns) - 2 * start i = Image.open(path) i = i.convert('RGBA') w, h = i.size i.load() - rows, columns = os.popen('stty size', 'r').read().split() - width = min(w, int(columns) - 2 * IMAGE_SHIFT) + width = min(w, length) height = int(float(h) * (float(width) / float(w))) height //= 2 i = i.resize((width, height), Image.BICUBIC) height = min(height, IMAGE_MAX_HEIGHT) for y in xrange(height): - print ' ' * IMAGE_SHIFT, + print ' ' * start, for x in xrange(width): p = i.getpixel((x, y)) r, g, b = p[:3] diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 12254e8..c0929b4 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -31,6 +31,7 @@ g = {} db = RainbowDB() cmdset = [ 'switch', + 'trend', 'home', 'view', 'mentions', @@ -227,20 +228,23 @@ def show_profile(u): followers_count = green(str(followers_count) + ' followers') count = statuses_count + ' ' + friends_count + ' ' + followers_count user = cycle_color(name) + grey(' ' + screen_name + ' : ') + count - profile_image_url = 'Profile photo: ' + cyan(profile_image_url) + profile_image_raw_url = 'Profile photo: ' + cyan(profile_image_url) description = ''.join(map(lambda x: x+' '*4 if x=='\n' else x,description)) description = yellow(description) location = 'Location : ' + magenta(location) url = 'URL : ' + (cyan(url) if url else '') - created_at = 'Join at ' + white(created_at) + date = parser.parse(created_at) + date = date - datetime.timedelta(seconds=time.timezone) + clock = date.strftime('%Y/%m/%d %H:%M:%S') + clock = 'Join at ' + white(clock) # Format line1 = u"{u:>{uw}}".format( u=user, uw=len(user) + 2, ) line2 = u"{p:>{pw}}".format( - p=profile_image_url, - pw=len(profile_image_url) + 4, + p=profile_image_raw_url, + pw=len(profile_image_raw_url) + 4, ) line3 = u"{d:>{dw}}".format( d=description, @@ -254,17 +258,35 @@ def show_profile(u): u=url, uw=len(url) + 4, ) - line6 = u"{j:>{jw}}".format( - j=created_at, - jw=len(created_at) + 4, + line6 = u"{c:>{cw}}".format( + c=clock, + cw=len(clock) + 4, ) # Display printNicely('') - for line in [line1,line2,line3,line4,line5,line6]: + printNicely(line1) + if g['iot']: + response = requests.get(profile_image_url) + image_to_display(StringIO(response.content),2,20) + else: + printNicely(line2) + for line in [line3,line4,line5,line6]: printNicely(line) printNicely('') +def print_trends(trends): + """ + Display topics + """ + for topic in trends[:TREND_MAX]: + name = topic['name'] + url = topic['url'] + line = cycle_color("#" + name) + ': ' + cyan(url) + printNicely(line) + printNicely('') + + def parse_arguments(): """ Parse the arguments @@ -400,6 +422,22 @@ def switch(): printNicely(red('Sorry I can\'t understand.')) +def trend(): + """ + Trend + """ + t = Twitter(auth=authen()) + avail = t.trends.available() + try: + country = g['stuff'].split()[0] + except: + country = '' + for location in avail: + if location['country'] == country: + trends = t.trends.place(_id=location['woeid'])[0]['trends'] + print_trends(trends) + + def home(): """ Home @@ -845,6 +883,7 @@ def help(): usage += s * 3 + '(see ' + grey('rainbowstream/config.py') + ').\n' usage += s + 'For more action: \n' + usage += s * 2 + green('trend') + ' will show closet trending topic.\n' usage += s * 2 + green('home') + ' will show your timeline. ' + \ green('home 7') + ' will show 7 tweets.\n' usage += s * 2 + green('view @mdo') + \ @@ -940,6 +979,7 @@ def process(cmd): cmdset, [ switch, + trend, home, view, mentions, @@ -977,6 +1017,7 @@ def listen(): cmdset, [ ['public', 'mine'], # switch + [], # trend [], # home ['@'], # view [], # mentions -- 2.25.1