| 1 | """ |
| 2 | Draw |
| 3 | """ |
| 4 | import requests |
| 5 | import datetime |
| 6 | import time |
| 7 | |
| 8 | from twitter.util import printNicely |
| 9 | from StringIO import StringIO |
| 10 | from dateutil import parser |
| 11 | from .c_image import * |
| 12 | from .colors import * |
| 13 | from .config import * |
| 14 | from .db import * |
| 15 | |
| 16 | db = RainbowDB() |
| 17 | |
| 18 | |
| 19 | def color_func(func_name): |
| 20 | """ |
| 21 | Call color function base on name |
| 22 | """ |
| 23 | pure = func_name.encode('utf8') |
| 24 | if pure.startswith('RGB_') and pure[4:].isdigit(): |
| 25 | return RGB(int(pure[4:])) |
| 26 | return globals()[pure] |
| 27 | |
| 28 | |
| 29 | def draw(t, iot=False, keyword=None, fil=[], ig=[]): |
| 30 | """ |
| 31 | Draw the rainbow |
| 32 | """ |
| 33 | |
| 34 | # Retrieve tweet |
| 35 | tid = t['id'] |
| 36 | text = t['text'] |
| 37 | screen_name = t['user']['screen_name'] |
| 38 | name = t['user']['name'] |
| 39 | created_at = t['created_at'] |
| 40 | favorited = t['favorited'] |
| 41 | date = parser.parse(created_at) |
| 42 | date = date - datetime.timedelta(seconds=time.timezone) |
| 43 | clock = date.strftime('%Y/%m/%d %H:%M:%S') |
| 44 | |
| 45 | # Get expanded url |
| 46 | try: |
| 47 | expanded_url = [] |
| 48 | url = [] |
| 49 | urls = t['entities']['urls'] |
| 50 | for u in urls: |
| 51 | expanded_url.append(u['expanded_url']) |
| 52 | url.append(u['url']) |
| 53 | except: |
| 54 | expanded_url = None |
| 55 | url = None |
| 56 | |
| 57 | # Get media |
| 58 | try: |
| 59 | media_url = [] |
| 60 | media = t['entities']['media'] |
| 61 | for m in media: |
| 62 | media_url.append(m['media_url']) |
| 63 | except: |
| 64 | media_url = None |
| 65 | |
| 66 | # Filter and ignore |
| 67 | screen_name = '@' + screen_name |
| 68 | if fil and screen_name not in fil: |
| 69 | return |
| 70 | if ig and screen_name in ig: |
| 71 | return |
| 72 | |
| 73 | # Get rainbow id |
| 74 | res = db.tweet_to_rainbow_query(tid) |
| 75 | if not res: |
| 76 | db.tweet_store(tid) |
| 77 | res = db.tweet_to_rainbow_query(tid) |
| 78 | rid = res[0].rainbow_id |
| 79 | |
| 80 | # Format info |
| 81 | user = cycle_color(name) + color_func(TWEET['nick'])(' ' + screen_name + ' ') |
| 82 | meta = color_func(TWEET['clock'])('[' + clock + '] ') + color_func(TWEET['id'])('[id=' + str(rid) + '] ') |
| 83 | if favorited: |
| 84 | meta = meta + color_func(TWEET['favorite'])(u'\u2605') |
| 85 | tweet = text.split() |
| 86 | # Replace url |
| 87 | if expanded_url: |
| 88 | for index in range(len(expanded_url)): |
| 89 | tweet = map( |
| 90 | lambda x: expanded_url[index] if x == url[index] else x, |
| 91 | tweet) |
| 92 | # Highlight RT |
| 93 | tweet = map(lambda x: color_func(TWEET['rt'])(x) if x == 'RT' else x, tweet) |
| 94 | # Highlight screen_name |
| 95 | tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) |
| 96 | # Highlight link |
| 97 | tweet = map(lambda x: color_func(TWEET['link'])(x) if x[0:4] == 'http' else x, tweet) |
| 98 | # Highlight search keyword |
| 99 | if keyword: |
| 100 | tweet = map( |
| 101 | lambda x: color_func(TWEET['keyword'])(x) if |
| 102 | ''.join(c for c in x if c.isalnum()).lower() == keyword.lower() |
| 103 | else x, |
| 104 | tweet |
| 105 | ) |
| 106 | # Recreate tweet |
| 107 | tweet = ' '.join(tweet) |
| 108 | |
| 109 | # Draw rainbow |
| 110 | line1 = u"{u:>{uw}}:".format( |
| 111 | u=user, |
| 112 | uw=len(user) + 2, |
| 113 | ) |
| 114 | line2 = u"{c:>{cw}}".format( |
| 115 | c=meta, |
| 116 | cw=len(meta) + 2, |
| 117 | ) |
| 118 | line3 = ' ' + tweet |
| 119 | |
| 120 | printNicely('') |
| 121 | printNicely(line1) |
| 122 | printNicely(line2) |
| 123 | printNicely(line3) |
| 124 | |
| 125 | # Display Image |
| 126 | if iot and media_url: |
| 127 | for mu in media_url: |
| 128 | response = requests.get(mu) |
| 129 | image_to_display(StringIO(response.content)) |
| 130 | |
| 131 | |
| 132 | def print_message(m): |
| 133 | """ |
| 134 | Print direct message |
| 135 | """ |
| 136 | sender_screen_name = '@' + m['sender_screen_name'] |
| 137 | sender_name = m['sender']['name'] |
| 138 | text = m['text'] |
| 139 | recipient_screen_name = '@' + m['recipient_screen_name'] |
| 140 | recipient_name = m['recipient']['name'] |
| 141 | mid = m['id'] |
| 142 | date = parser.parse(m['created_at']) |
| 143 | date = date - datetime.timedelta(seconds=time.timezone) |
| 144 | clock = date.strftime('%Y/%m/%d %H:%M:%S') |
| 145 | |
| 146 | # Get rainbow id |
| 147 | res = db.message_to_rainbow_query(mid) |
| 148 | if not res: |
| 149 | db.message_store(mid) |
| 150 | res = db.message_to_rainbow_query(mid) |
| 151 | rid = res[0].rainbow_id |
| 152 | |
| 153 | # Draw |
| 154 | sender = cycle_color(sender_name) + color_func(MESSAGE['sender'])(' ' + sender_screen_name + ' ') |
| 155 | recipient = cycle_color(recipient_name) + color_func(MESSAGE['recipient'])(' ' + recipient_screen_name + ' ') |
| 156 | user = sender + color_func(MESSAGE['to'])(' >>> ') + recipient |
| 157 | meta = color_func(MESSAGE['clock'])('[' + clock + ']') + color_func(MESSAGE['id'])(' [message_id=' + str(rid) + '] ') |
| 158 | text = ''.join(map(lambda x: x + ' ' if x == '\n' else x, text)) |
| 159 | |
| 160 | line1 = u"{u:>{uw}}:".format( |
| 161 | u=user, |
| 162 | uw=len(user) + 2, |
| 163 | ) |
| 164 | line2 = u"{c:>{cw}}".format( |
| 165 | c=meta, |
| 166 | cw=len(meta) + 2, |
| 167 | ) |
| 168 | |
| 169 | line3 = ' ' + text |
| 170 | |
| 171 | printNicely('') |
| 172 | printNicely(line1) |
| 173 | printNicely(line2) |
| 174 | printNicely(line3) |
| 175 | |
| 176 | |
| 177 | def show_profile(u, iot=False): |
| 178 | """ |
| 179 | Show a profile |
| 180 | """ |
| 181 | # Retrieve info |
| 182 | name = u['name'] |
| 183 | screen_name = u['screen_name'] |
| 184 | description = u['description'] |
| 185 | profile_image_url = u['profile_image_url'] |
| 186 | location = u['location'] |
| 187 | url = u['url'] |
| 188 | created_at = u['created_at'] |
| 189 | statuses_count = u['statuses_count'] |
| 190 | friends_count = u['friends_count'] |
| 191 | followers_count = u['followers_count'] |
| 192 | |
| 193 | # Create content |
| 194 | statuses_count = color_func(PROFILE['statuses_count'])(str(statuses_count) + ' tweets') |
| 195 | friends_count = color_func(PROFILE['friends_count'])(str(friends_count) + ' following') |
| 196 | followers_count = color_func(PROFILE['followers_count'])(str(followers_count) + ' followers') |
| 197 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
| 198 | user = cycle_color(name) + color_func(PROFILE['nick'])(' @' + screen_name + ' : ') + count |
| 199 | profile_image_raw_url = 'Profile photo: ' + color_func(PROFILE['profile_image_url'])(profile_image_url) |
| 200 | description = ''.join( |
| 201 | map(lambda x: x + ' ' * 4 if x == '\n' else x, description)) |
| 202 | description = color_func(PROFILE['description'])(description) |
| 203 | location = 'Location : ' + color_func(PROFILE['location'])(location) |
| 204 | url = 'URL : ' + (color_func(PROFILE['url'])(url) if url else '') |
| 205 | date = parser.parse(created_at) |
| 206 | date = date - datetime.timedelta(seconds=time.timezone) |
| 207 | clock = date.strftime('%Y/%m/%d %H:%M:%S') |
| 208 | clock = 'Join at ' + color_func(PROFILE['clock'])(clock) |
| 209 | |
| 210 | # Format |
| 211 | line1 = u"{u:>{uw}}".format( |
| 212 | u=user, |
| 213 | uw=len(user) + 2, |
| 214 | ) |
| 215 | line2 = u"{p:>{pw}}".format( |
| 216 | p=profile_image_raw_url, |
| 217 | pw=len(profile_image_raw_url) + 4, |
| 218 | ) |
| 219 | line3 = u"{d:>{dw}}".format( |
| 220 | d=description, |
| 221 | dw=len(description) + 4, |
| 222 | ) |
| 223 | line4 = u"{l:>{lw}}".format( |
| 224 | l=location, |
| 225 | lw=len(location) + 4, |
| 226 | ) |
| 227 | line5 = u"{u:>{uw}}".format( |
| 228 | u=url, |
| 229 | uw=len(url) + 4, |
| 230 | ) |
| 231 | line6 = u"{c:>{cw}}".format( |
| 232 | c=clock, |
| 233 | cw=len(clock) + 4, |
| 234 | ) |
| 235 | |
| 236 | # Display |
| 237 | printNicely('') |
| 238 | printNicely(line1) |
| 239 | if iot: |
| 240 | response = requests.get(profile_image_url) |
| 241 | image_to_display(StringIO(response.content), 2, 20) |
| 242 | else: |
| 243 | printNicely(line2) |
| 244 | for line in [line3, line4, line5, line6]: |
| 245 | printNicely(line) |
| 246 | printNicely('') |
| 247 | |
| 248 | |
| 249 | def print_trends(trends): |
| 250 | """ |
| 251 | Display topics |
| 252 | """ |
| 253 | for topic in trends[:TREND_MAX]: |
| 254 | name = topic['name'] |
| 255 | url = topic['url'] |
| 256 | line = cycle_color(name) + ': ' + TREND['url'](url) |
| 257 | printNicely(line) |
| 258 | printNicely('') |
| 259 | |