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