From: Orakaro Date: Fri, 6 Jun 2014 16:01:21 +0000 (+0900) Subject: photo done X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=commitdiff_plain;h=991c30af2171bac652a3c8402f938e23b955a8d1;hp=0418d4438a6d413aacdb630b8a0bcbbc76a23e0e;ds=sidebyside photo done --- diff --git a/rainbowstream/c_image.py b/rainbowstream/c_image.py index a9e73de..95e1c66 100644 --- a/rainbowstream/c_image.py +++ b/rainbowstream/c_image.py @@ -2,7 +2,10 @@ from PIL import Image from functools import partial from os.path import join, dirname, getmtime, exists, expanduser -import ctypes, sys, os +import ctypes +import sys +import os + def call_c(): library = expanduser('~/.image.so') @@ -10,33 +13,35 @@ def call_c(): if not exists(library) or getmtime(sauce) > getmtime(library): build = "gcc -fPIC -shared -o %s %s" % (library, sauce) assert os.system(build + " >/dev/null 2>&1") == 0 - image_c= ctypes.cdll.LoadLibrary(library) + image_c = ctypes.cdll.LoadLibrary(library) image_c.init() return image_c.rgb_to_ansi rgb2short = call_c() - + + def pixel_print(ansicolor): sys.stdout.write('\033[48;5;%sm \033[0m' % (ansicolor)) - + + def image_to_display(path): - i = Image.open(path) + i = Image.open(path) i = i.convert('RGBA') - w,h = i.size + w, h = i.size i.load() rows, columns = os.popen('stty size', 'r').read().split() - width = min(w, int(columns)-2*6) + width = min(w, int(columns) - 2 * 6) height = int(float(h) * (float(width) / float(w))) height //= 2 i = i.resize((width, height), Image.BICUBIC) - height = min(height,30) + height = min(height, 30) for y in xrange(height): - print ' '*6 , + print ' ' * 6, for x in xrange(width): - p = i.getpixel((x,y)) + p = i.getpixel((x, y)) r, g, b = p[:3] - pixel_print(rgb2short(r,g,b)) + pixel_print(rgb2short(r, g, b)) print '' if __name__ == '__main__': diff --git a/rainbowstream/image.c b/rainbowstream/image.c index 5de90b0..96e7a01 100644 --- a/rainbowstream/image.c +++ b/rainbowstream/image.c @@ -4,7 +4,7 @@ typedef struct { int b; } rgb_t; int CUBE_STEPS[] = { 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF }; -rgb_t BASIC16[] = +rgb_t BASIC16[] = { { 0, 0, 0 }, { 205, 0, 0}, { 0, 205, 0 }, { 205, 205, 0 }, { 0, 0, 238}, { 205, 0, 205 }, { 0, 205, 205 }, { 229, 229, 229}, { 127, 127, 127 }, @@ -14,7 +14,7 @@ rgb_t BASIC16[] = rgb_t COLOR_TABLE[256]; -rgb_t xterm_to_rgb(int xcolor) +rgb_t ansi_to_rgb(int xcolor) { rgb_t res; if (xcolor < 16) { @@ -34,7 +34,7 @@ int init() { int c; for (c = 0; c < 256; c++) { - COLOR_TABLE[c] = xterm_to_rgb(c); + COLOR_TABLE[c] = ansi_to_rgb(c); } return 0; } diff --git a/rainbowstream/pure_image.py b/rainbowstream/pure_image.py index 464f93f..f4cb751 100644 --- a/rainbowstream/pure_image.py +++ b/rainbowstream/pure_image.py @@ -1,116 +1,117 @@ from PIL import Image from functools import partial -import sys,os +import sys +import os CLUT = [ # color look-up table -# 8-bit, RGB hex + # 8-bit, RGB hex # Primary 3-bit (8 colors). Unique representation! - ('00', '000000'), - ('01', '800000'), - ('02', '008000'), - ('03', '808000'), - ('04', '000080'), - ('05', '800080'), - ('06', '008080'), - ('07', 'c0c0c0'), + ('00', '000000'), + ('01', '800000'), + ('02', '008000'), + ('03', '808000'), + ('04', '000080'), + ('05', '800080'), + ('06', '008080'), + ('07', 'c0c0c0'), # Equivalent "bright" versions of original 8 colors. - ('08', '808080'), - ('09', 'ff0000'), - ('10', '00ff00'), - ('11', 'ffff00'), - ('12', '0000ff'), - ('13', 'ff00ff'), - ('14', '00ffff'), - ('15', 'ffffff'), + ('08', '808080'), + ('09', 'ff0000'), + ('10', '00ff00'), + ('11', 'ffff00'), + ('12', '0000ff'), + ('13', 'ff00ff'), + ('14', '00ffff'), + ('15', 'ffffff'), # Strictly ascending. - ('16', '000000'), - ('17', '00005f'), - ('18', '000087'), - ('19', '0000af'), - ('20', '0000d7'), - ('21', '0000ff'), - ('22', '005f00'), - ('23', '005f5f'), - ('24', '005f87'), - ('25', '005faf'), - ('26', '005fd7'), - ('27', '005fff'), - ('28', '008700'), - ('29', '00875f'), - ('30', '008787'), - ('31', '0087af'), - ('32', '0087d7'), - ('33', '0087ff'), - ('34', '00af00'), - ('35', '00af5f'), - ('36', '00af87'), - ('37', '00afaf'), - ('38', '00afd7'), - ('39', '00afff'), - ('40', '00d700'), - ('41', '00d75f'), - ('42', '00d787'), - ('43', '00d7af'), - ('44', '00d7d7'), - ('45', '00d7ff'), - ('46', '00ff00'), - ('47', '00ff5f'), - ('48', '00ff87'), - ('49', '00ffaf'), - ('50', '00ffd7'), - ('51', '00ffff'), - ('52', '5f0000'), - ('53', '5f005f'), - ('54', '5f0087'), - ('55', '5f00af'), - ('56', '5f00d7'), - ('57', '5f00ff'), - ('58', '5f5f00'), - ('59', '5f5f5f'), - ('60', '5f5f87'), - ('61', '5f5faf'), - ('62', '5f5fd7'), - ('63', '5f5fff'), - ('64', '5f8700'), - ('65', '5f875f'), - ('66', '5f8787'), - ('67', '5f87af'), - ('68', '5f87d7'), - ('69', '5f87ff'), - ('70', '5faf00'), - ('71', '5faf5f'), - ('72', '5faf87'), - ('73', '5fafaf'), - ('74', '5fafd7'), - ('75', '5fafff'), - ('76', '5fd700'), - ('77', '5fd75f'), - ('78', '5fd787'), - ('79', '5fd7af'), - ('80', '5fd7d7'), - ('81', '5fd7ff'), - ('82', '5fff00'), - ('83', '5fff5f'), - ('84', '5fff87'), - ('85', '5fffaf'), - ('86', '5fffd7'), - ('87', '5fffff'), - ('88', '870000'), - ('89', '87005f'), - ('90', '870087'), - ('91', '8700af'), - ('92', '8700d7'), - ('93', '8700ff'), - ('94', '875f00'), - ('95', '875f5f'), - ('96', '875f87'), - ('97', '875faf'), - ('98', '875fd7'), - ('99', '875fff'), + ('16', '000000'), + ('17', '00005f'), + ('18', '000087'), + ('19', '0000af'), + ('20', '0000d7'), + ('21', '0000ff'), + ('22', '005f00'), + ('23', '005f5f'), + ('24', '005f87'), + ('25', '005faf'), + ('26', '005fd7'), + ('27', '005fff'), + ('28', '008700'), + ('29', '00875f'), + ('30', '008787'), + ('31', '0087af'), + ('32', '0087d7'), + ('33', '0087ff'), + ('34', '00af00'), + ('35', '00af5f'), + ('36', '00af87'), + ('37', '00afaf'), + ('38', '00afd7'), + ('39', '00afff'), + ('40', '00d700'), + ('41', '00d75f'), + ('42', '00d787'), + ('43', '00d7af'), + ('44', '00d7d7'), + ('45', '00d7ff'), + ('46', '00ff00'), + ('47', '00ff5f'), + ('48', '00ff87'), + ('49', '00ffaf'), + ('50', '00ffd7'), + ('51', '00ffff'), + ('52', '5f0000'), + ('53', '5f005f'), + ('54', '5f0087'), + ('55', '5f00af'), + ('56', '5f00d7'), + ('57', '5f00ff'), + ('58', '5f5f00'), + ('59', '5f5f5f'), + ('60', '5f5f87'), + ('61', '5f5faf'), + ('62', '5f5fd7'), + ('63', '5f5fff'), + ('64', '5f8700'), + ('65', '5f875f'), + ('66', '5f8787'), + ('67', '5f87af'), + ('68', '5f87d7'), + ('69', '5f87ff'), + ('70', '5faf00'), + ('71', '5faf5f'), + ('72', '5faf87'), + ('73', '5fafaf'), + ('74', '5fafd7'), + ('75', '5fafff'), + ('76', '5fd700'), + ('77', '5fd75f'), + ('78', '5fd787'), + ('79', '5fd7af'), + ('80', '5fd7d7'), + ('81', '5fd7ff'), + ('82', '5fff00'), + ('83', '5fff5f'), + ('84', '5fff87'), + ('85', '5fffaf'), + ('86', '5fffd7'), + ('87', '5fffff'), + ('88', '870000'), + ('89', '87005f'), + ('90', '870087'), + ('91', '8700af'), + ('92', '8700d7'), + ('93', '8700ff'), + ('94', '875f00'), + ('95', '875f5f'), + ('96', '875f87'), + ('97', '875faf'), + ('98', '875fd7'), + ('99', '875fff'), ('100', '878700'), ('101', '87875f'), ('102', '878787'), @@ -271,6 +272,7 @@ CLUT = [ # color look-up table ('255', 'eeeeee'), ] + def _create_dicts(): short2rgb_dict = dict(CLUT) rgb2short_dict = {} @@ -292,7 +294,7 @@ def pixel_print(ansicolor): def hex_to_rgb(value): value = value.lstrip('#') lv = len(value) - return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3)) + return tuple(int(value[i:i + lv / 3], 16) for i in range(0, lv, lv / 3)) def rgb_to_hex(rgb): @@ -300,31 +302,32 @@ def rgb_to_hex(rgb): def rgb2short(r, g, b): - dist = lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2+(s[2]-d[2])**2 + dist = lambda s, d: (s[0] - d[0]) ** 2 + \ + (s[1] - d[1]) ** 2 + (s[2] - d[2]) ** 2 ary = [hex_to_rgb(hex) for hex in RGB2SHORT_DICT] - m = min(ary, key=partial(dist, (r,g,b))) + m = min(ary, key=partial(dist, (r, g, b))) return RGB2SHORT_DICT[rgb_to_hex(m)] + def image_to_display(path): - i = Image.open(path) + i = Image.open(path) i = i.convert('RGBA') - w,h = i.size + w, h = i.size i.load() rows, columns = os.popen('stty size', 'r').read().split() - width = min(w, int(columns)-2*6) + width = min(w, int(columns) - 2 * 6) height = int(float(h) * (float(width) / float(w))) height //= 2 i = i.resize((width, height), Image.BICUBIC) - height = min(height,30) + height = min(height, 30) for y in xrange(height): - print ' '*6 , + print ' ' * 6, for x in xrange(width): - p = i.getpixel((x,y)) + p = i.getpixel((x, y)) r, g, b = p[:3] - pixel_print(rgb2short(r,g,b)) + pixel_print(rgb2short(r, g, b)) print '' if __name__ == '__main__': image_to_display(sys.argv[1]) - diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 8b71c34..d3aa46c 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -11,18 +11,21 @@ import signal import argparse import time import datetime +import requests from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup from twitter.api import * from twitter.oauth import OAuth, read_token_file from twitter.oauth_dance import oauth_dance from twitter.util import printNicely +from StringIO import StringIO from .colors import * from .config import * from .consumer import * from .interactive import * from .db import * +from .c_image import * g = {} db = RainbowDB() @@ -61,6 +64,27 @@ def draw(t, keyword=None, fil=[], ig=[]): date = date - datetime.timedelta(seconds=time.timezone) clock = date.strftime('%Y/%m/%d %H:%M:%S') + # Get expanded url + try: + expanded_url = [] + url = [] + urls = t['entities']['urls'] + for u in urls: + expanded_url.append(u['expanded_url']) + url.append(u['url']) + except: + expanded_url = None + url = None + + # Get media + try: + media_url = [] + media = t['entities']['media'] + for m in media: + media_url = m['media_url'] + except: + media_url = None + # Filter and ignore screen_name = '@' + screen_name if fil and screen_name not in fil: @@ -80,6 +104,12 @@ def draw(t, keyword=None, fil=[], ig=[]): if favorited: meta = meta + green(u'\u2605') tweet = text.split() + # Replace url + if expanded_url: + for index in range(len(expanded_url)): + tweet = map( + lambda x: expanded_url[index] if x == url[index] else x, + tweet) # Highlight RT tweet = map(lambda x: grey(x) if x == 'RT' else x, tweet) # Highlight screen_name @@ -94,6 +124,7 @@ def draw(t, keyword=None, fil=[], ig=[]): else x, tweet ) + # Recreate tweet tweet = ' '.join(tweet) # Draw rainbow @@ -112,6 +143,11 @@ def draw(t, keyword=None, fil=[], ig=[]): printNicely(line2) printNicely(line3) + # Display Image + if media_url: + response = requests.get(media_url) + image_to_display(StringIO(response.content)) + def parse_arguments(): """ diff --git a/requirements.txt b/requirements.txt index a40b42c..4f9f78a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,5 @@ termcolor==1.1.0 twitter==1.14.3 SQLAlchemy==0.9.4 pysqlite==2.6.3 +Pillow==2.4.0 +requests==2.3.0 diff --git a/setup.py b/setup.py index 046a182..bcd25fa 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,9 @@ install_requires = [ "pyfiglet", "python-dateutil", "termcolor", - "twitter" + "twitter", + "Pillow", + "requests", ] setup(name='rainbowstream',