-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
796 lines (616 loc) · 20.4 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
import awoo
import colors
import utils.database as database
from re import compile as re_compile, M as re_M
from utils.colortrans import rgb2short
from datetime import datetime
from subprocess import Popen, PIPE
from os import environ, name as os_name, system, sep, remove
from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
# 'more' is usually present in all relevant OSes
MORE = 'more.com' if os_name == 'nt' else 'more'
PAGER = environ.get('PAGER') or MORE
EDITOR = environ.get('EDITOR') or ('notepad.exe' if os_name == 'nt' else 'vi')
CLEAR = 'cls' if os_name == 'nt' else 'clear'
TMP_ = environ.get('TMP') if os_name == 'nt' else '/tmp'
HOME = environ.get('HOME') or environ.get('HOMEPATH')
DB_PATH = '%s%s%s' % (HOME, sep, '.awoo_threads_pinned.gz')
DB = database.load(DB_PATH) or []
PROMPT = colors.red('>>>')
RE_1 = (re_compile(r'(^>[^>].*$)', re_M), colors.red(r'\1'))
RE_2 = (re_compile(r'(>>\d+)'), colors.cyan(r'\1'))
RE_BOARD = re_compile(r'([^/]+)(\d+)?')
BOARD_BLACKLIST = []
def TMP(path):
return '%s%s%s' % (TMP_, sep, '__%s__' % path)
# need a mutable object to change
# the currently selected board or reply
class CurrentBoard:
def __init__(self):
default = awoo.get_board_description(awoo.conn.cfg['default_board'])
if not default:
raise awoo.AwooException()
self.default = default['name']
self.board = self.default
self.desc = default['desc']
self.last_cmd = None
def cd(self, board=None):
board = board if board else self.default
board = awoo.get_board_description(board)
if not board:
raise awoo.AwooException()
self.board = board['name']
self.desc = board['desc']
def tokenize(line):
return line.split(' ')
def eval_awoo(sel, line):
# strip the newline char
line = line.strip()
# ignore comments
if line and line[0] == '#':
return
# tokenize input
toks = tokenize(line)
# eval that shit
eval_cmd(sel, toks)
def load_rc(sel):
try:
with open('%s%s%s' % (HOME, sep, '.awoorc'), 'r') as f:
map(lambda line: eval_awoo(sel, line), f.read().rstrip().split('\n'))
f.close()
except IOError:
pass
def flush_database():
database.write(DB, DB_PATH)
def write_new_thr_database(thr, description):
DB.append((thr, description))
flush_database()
def remove_thr_database(thr):
[DB.remove(t) for t in DB if t[0] == thr]
flush_database()
def remove_all_thr_database():
del DB[:]
flush_database()
def less(data):
process = Popen([PAGER], stdin=PIPE)
data = data if isinstance(data, bytes) else bytes(data, encoding='utf-8')
try:
process.stdin.write(data)
process.communicate()
process.wait()
except IOError:
pass
def edit(file):
process = Popen([EDITOR, file])
process.communicate()
process.wait()
def edit_(file):
edit(file)
try:
with open(file, 'r') as f:
reply = f.read().rstrip()
f.close()
except IOError:
return None
try:
remove(file)
except OSError:
print("Temp file is already removed, skipping step.")
return reply
def safe_int(n):
try:
return int(n)
except ValueError:
return None
def get_date(t):
return datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
def color_hash(h):
_c = None
try:
_c, _ = rgb2short('#%s' % h)
_c = int(_c)
except:
_c = hash(h) % 256
return colors.color(h, fg=_c)
def cap_or_hash(post):
return post.get('capcode') or post['hash']
def comment_or_blankfag(post):
comment = post['comment']
if comment:
comment = RE_1[0].sub(RE_1[1], comment.replace('\r\n', '\n'))
comment = RE_2[0].sub(RE_2[1], comment)
return comment
else:
return colors.red('[[BLANKFAG]]')
def title_or_blankfag(thread):
return thread.get('title') or '[[BLANKFAG]]'
def color_reply_count(thread):
if thread['is_locked']:
return colors.red('%dL' % thread['number_of_replies'])
else:
return colors.yellow('%d' % thread['number_of_replies'])
def eval_cmd(sel, toks):
try:
CMD_DICT[toks[0]](sel, toks)
if toks[0] != 'r' and toks[0] != '!!' and toks[0] != 'repeat':
sel.last_cmd = toks
except KeyError:
if toks[0]:
print('Command "%s" not available. Try "help".' % toks[0])
def threads_format(sel, page, threads):
ppt = PROMPT
bar = colors.cyan('--------------------------------------------------')
fmt = '\n%s\n%s /%s/ - %s\n%s\n\n' % (bar, ppt, colors.cyan(sel.board), colors.green(sel.desc), bar)
if sel.board == sel.default and BOARD_BLACKLIST:
threads = [thr for thr in threads if 'title' in thr and thr['board'] not in BOARD_BLACKLIST]
else:
threads = [thr for thr in threads if 'title' in thr]
threads = sorted(threads, key=lambda thr: thr['last_bumped'], reverse=True)
for thr in threads:
fmt += "%s [%s] (%s) %s [%s] [%s]\n%s\n\n%s\n--------------------------------------------------\n\n" % (
'%s. %s' % (colors.red('No'), colors.green('%d' % thr['post_id'])),
color_hash(cap_or_hash(thr)),
colors.yellow(get_date(thr['date_posted'])),
colors.green(title_or_blankfag(thr)),
color_reply_count(thr),
colors.red(thr['board']),
comment_or_blankfag(thr),
colors.cyan('Last bumped on: %s' % get_date(thr['last_bumped']))
)
return '%s%s\n' % (fmt, colors.green('Page number: %d' % page))
def replies_format(replies):
fmt = "%s [%s] (%s) %s [%s] [%s]\n%s\n\n--------------------------------------------------\n\n" % (
'%s. %s' % (colors.red('No'), colors.green('%d' % replies[0]['post_id'])),
color_hash(cap_or_hash(replies[0])),
colors.yellow(get_date(replies[0]['date_posted'])),
colors.green(title_or_blankfag(replies[0])),
color_reply_count(replies[0]),
colors.red(replies[0]['board']),
comment_or_blankfag(replies[0])
)
for r in replies[1:]:
fmt += "%s [%s] (%s)\n%s\n\n--------------------------------------------------\n\n" % (
'%s. %s' % (colors.red('No'), colors.green('%d' % r['post_id'])),
color_hash(cap_or_hash(r)),
colors.yellow(get_date(r['date_posted'])),
comment_or_blankfag(r)
)
fmt += colors.cyan('Last bumped on: %s' % get_date(replies[0]['last_bumped']))
return fmt
def search_format(query, threads):
ppt = PROMPT
bar = colors.cyan('--------------------------------------------------')
text = colors.cyan('Search results for: %s' % colors.yellow(query))
fmt = '\n%s\n%s %s\n%s\n\n' % (bar, ppt, text, bar)
for thr in threads:
fmt += "%s [%s] %s [%s]\n--------------------------------------------------\n\n" % (
'%s. %s' % (colors.red('No'), colors.green('%d' % thr['post_id'])),
color_hash(cap_or_hash(thr)),
colors.green(title_or_blankfag(thr)),
colors.red(thr['board']),
)
return fmt
def cmd_help(_, toks):
"""\
Print all the commands available.
Usage: h|help
h|help [command]"""
if len(toks) < 2:
fmt = ''
for cmd in sorted(CMD_DICT):
fmt += ('%s:\n%s\n\n' % (cmd, CMD_DICT[cmd].__doc__)
or 'No help available for "%s".' % cmd)
less(fmt)
else:
try:
less('%s:\n%s' % (toks[1], CMD_DICT[toks[1]].__doc__)
or 'No help available for "%s".' % toks[1])
except KeyError:
print('Command "%s" not available.' % toks[1])
def cmd_clear(_, _0):
"""\
Clears the terminal window.
Usage: cls|clear"""
system(CLEAR)
def cmd_quit(_, _0):
"""\
Exits the client.
Usage: q|quit|exit"""
exit(0)
def cmd_get_boards(_, _0):
"""\
Fetches the board list.
Usage: ls|gb|get_boards"""
for board in awoo.get_boards():
print(board, end=' ')
print('')
def cmd_get_threads(sel, toks):
"""\
Fetches the thread list from a board.
Usage: gt|get_threads
gt|get_threads [page number]"""
page = 0 if len(toks) < 2 else safe_int(toks[1]) or 0
threads = awoo.get_threads(sel.board, page)
if threads:
thr_fmt = threads_format(sel, page, threads)
less(thr_fmt)
else:
print('No threads on page %d.' % page)
def cmd_get_replies(sel, toks):
"""\
Fetches the replies in a particular thread.
Usage: gr|get_replies [awoo thread]"""
id = None
replies = None
try:
id = int(toks[1])
except IndexError:
print('No thread id specified.')
return
except ValueError:
print('Invalid thread id "%s".' % toks[1])
return
replies = awoo.get_thread_replies(id)
if not replies:
print('Invalid thread id "%d".' % id)
else:
thr_fmt = replies_format(replies)
less(thr_fmt)
def cmd_selected(sel, _):
"""\
Lists the selected board.
Usage: sel|pwd"""
print('Browsing "%s".' % sel.board)
def cmd_cd(sel, toks):
"""\
Changes the currently selected board.
Usage: cd|cb
cd|cb [awoo board]"""
if len(toks) < 2:
if sel.board == sel.default:
print('Already browsing "%s".' % sel.default)
return
else:
sel.cd()
print('Now browsing "%s".' % sel.default)
return
try:
if sel.board == toks[1]:
print('Already browsing "%s".' % toks[1])
else:
sel.cd(toks[1])
print('Now browsing "%s".' % toks[1])
except awoo.AwooException:
print("Board \"%s\" doesn't exist." % toks[1])
def cmd_send_reply(sel, toks):
"""\
Replies to a thread, given a thread id.
Usage: re|reply [awoo thread]"""
id = None
try:
id = int(toks[1])
except IndexError:
print('No thread id specified.')
return
except ValueError:
print('Invalid thread id "%s".' % toks[1])
return
valid_thread = awoo.thread_exists(id)
if not valid_thread:
print('Invalid thread id "%d".' % id)
else:
reply = edit_(TMP('reply_body_%d' % id))
if reply:
try:
awoo.post_reply(sel.board, id, reply)
print('Successfully replied to %d.' % id)
except awoo.AwooException as e:
print(e.message)
else:
print('Empty body, not posting reply.')
def cmd_blankpost(sel, toks):
"""\
Sends a blank post to a specific thread.
Usage: bp|blankpost|blankfag [awoo thread]"""
id = None
try:
id = int(toks[1])
except IndexError:
print('No thread id specified.')
return
except ValueError:
print('Invalid thread id "%s".' % toks[1])
return
valid_thread = awoo.thread_exists(id)
if not valid_thread:
print('Invalid thread id "%d".' % id)
else:
try:
awoo.post_reply(sel.board, id, '')
print('Successfully sent blankpost to %d.' % id)
except awoo.AwooException as e:
print(e.message)
def cmd_new_thread(sel, toks):
"""\
Starts a new thread.
Usage: nt|new_thread"""
if sel.board == sel.default:
print('Please chooose a different board, "%s" is selected.' % sel.default)
return
title = edit_(TMP('thread_title_%s' % sel.board))
if not title:
print('Empty title, not posting new thread.')
return
reply = edit_(TMP('thread_body_%s' % sel.board))
if not reply:
print('Empty body, not posting new thread.')
return
try:
thr = awoo.new_thread(sel.board, title, reply)
print('Successfully started %s.' % thr)
except awoo.AwooException as e:
print(e.message)
def cmd_search2(sel, toks):
"""\
Searches for a string of text in a board, using a faster yet less detailed method.
Usage: search2|find2 [board] [search string]"""
ts = toks[2:]
if len(toks) < 2:
print('No board or search string given.')
return
elif not ts:
print('Empty search string, not performing query.')
return
elif toks[1] not in awoo.get_boards():
print("Board \"%s\" doesn't exist." % toks[1])
return
query = ' '.join(ts)
threads = awoo.search(toks[1], query)
if threads:
thr_fmt = search_format(query, threads)
less(thr_fmt)
else:
print('Nothing found for the given search string.')
def cmd_search(sel, toks):
"""\
Searches for a string of text in a board.
Usage: search|find [board][/starting_page] [search string]"""
ts = toks[2:]
if len(toks) < 2:
print('No board or search string given.')
return
elif not ts:
print('Empty search string, not performing query.')
return
board_info = RE_BOARD.findall(toks[1])
toks[1] = board_info[0][0]
if toks[1] not in awoo.get_boards():
print("Board \"%s\" doesn't exist." % toks[1])
return
query = re_compile(' '.join(ts).lower(), re_M)
page = None
try:
page = int(board_info[1][0]) if len(board_info) > 1 else 0
except ValueError:
print('Invalid starting page \"%s\" specified.' % board_info[1][0])
return
threads = awoo.get_threads(toks[1], page)
while threads:
try:
print('Searching page %s.' % colors.red(str(page)))
_threads = None
if toks[1] == sel.default and BOARD_BLACKLIST:
_threads = [thr for thr in threads if 'title' in thr and thr['board'] not in BOARD_BLACKLIST]
else:
_threads = [thr for thr in threads if 'title' in thr]
for thr in _threads:
print(' ', colors.green('>'), 'Searching thread %s.' % colors.magenta('/%s/%d' % (toks[1], thr['post_id'])))
replies = awoo.get_thread_replies(thr['post_id'])
if replies:
comment = replies[0]['title'].lower()
m = query.search(comment)
if m:
found = comment[m.start():41].replace('\r', '').replace('\n', ' ')
fmt = colors.white('(...%s...)' % found, style='faint')
id = colors.green(str(replies[0]['post_id']))
print(' ', colors.cyan('|'), 'Found in title %s %s.' % (id, fmt))
del m
for r in replies:
comment = r['comment'].lower()
m = query.search(comment)
if m:
found = comment[m.start():41].replace('\r', '').replace('\n', ' ')
fmt = colors.white('(...%s...)' % found, style='faint')
id = colors.green(str(r['post_id']))
print(' ', colors.cyan('|'), 'Found in reply %s %s.' % (id, fmt))
del m
page += 1
threads = awoo.get_threads(toks[1], page)
except KeyboardInterrupt:
print('Search interrupted.')
break
def cmd_pin_thread(_, toks):
"""\
Saves a thread to read it later on another occasion.
Usage: pin [awoo thread] [description]"""
if len(toks) < 3:
print('No thread or description given.')
return
id = None
ts = toks[2:]
try:
id = int(toks[1])
except ValueError:
print('Invalid thread id "%s".' % toks[1])
return
if id in [x for (x, _) in DB]:
print('Thread "%d" is already in database.' % id)
return
valid_thread = awoo.thread_exists(id)
if not valid_thread:
print('Invalid thread id "%d".' % id)
return
desc = ' '.join(ts)
write_new_thr_database(id, desc)
print('Successfully saved thread "%d" in database.' % id)
def cmd_unpin_thread(_, toks):
"""\
Removes a thread from the pinned thread list.
Usage: unpin [awoo thread]"""
if not DB:
print('No threads in pinned list.')
return
if len(toks) < 2:
print('No thread given.')
return
id = None
try:
id = int(toks[1])
except ValueError:
if toks[1] == 'all':
remove_all_thr_database()
print('Successfully removed all threads from pinned list.')
return
else:
print('Invalid thread id "%s".' % toks[1])
return
if id not in [_id for _id, _ in DB]:
print("Can't find \"%d\" in pinned list." % id)
return
remove_thr_database(id)
print('Successfully removed thread "%d" from pinned list.' % id)
def cmd_pinned(_, _0):
"""\
Returns the list of pinned threads.
Usage: pinned"""
if not DB:
print('No threads in pinned list.')
return
fmt = ''
for thr in DB:
fmt += '%d:\n %s\n\n' % thr
less(fmt)
def cmd_last_cmd(sel, _):
"""\
Executes the last command again.
Usage: r|repeat|!!"""
if not sel.last_cmd:
print('No last command successfully executed in history.')
return
print(' '.join(sel.last_cmd))
eval_cmd(sel, sel.last_cmd)
def cmd_blacklist(_, _0):
"""\
Lists the blacklisted boards.
Usage: blacklist"""
if BOARD_BLACKLIST:
for board in BOARD_BLACKLIST:
print(board, end=' ')
print('')
else:
print('Board blacklist is empty.')
def cmd_filter(sel, toks):
"""\
Temporarily filters a board.
Usage: filter [awoo board]
filter all"""
if len(toks) < 2:
print('No board to filter.')
return
global BOARD_BLACKLIST
if toks[1] == 'all':
BOARD_BLACKLIST = awoo.get_boards()
print('Filtered all boards.')
return
if toks[1] in BOARD_BLACKLIST:
print('Board "%s" is already being filtered.' % toks[1])
return
if toks[1] in awoo.get_boards():
BOARD_BLACKLIST.append(toks[1])
print('Added "%s" to board blacklist.' % toks[1])
else:
print("Board \"%s\" doesn't exist." % toks[1])
def cmd_unfilter(_, toks):
"""\
Removes a board from the blacklist.
Usage: unfilter [awoo board]
unfilter all"""
if len(toks) < 2:
print('No board to unfilter.')
return
global BOARD_BLACKLIST
if toks[1] == 'all':
del BOARD_BLACKLIST[:]
print('Cleared the blacklist.')
return
try:
BOARD_BLACKLIST.remove(toks[1])
print('Removed "%s" from the blacklist.' % toks[1])
except ValueError:
print("Can't find \"%s\" in the blacklist." % toks[1])
# dictionary contains the appropriate functions
# to call upon a certain command being read
CMD_DICT = {
'h': cmd_help,
'help': cmd_help,
'cls': cmd_clear,
'clear': cmd_clear,
'q': cmd_quit,
'quit': cmd_quit,
'exit': cmd_quit,
'ls': cmd_get_boards,
'gb': cmd_get_boards,
'get_boards': cmd_get_boards,
'gr': cmd_get_replies,
'get_replies': cmd_get_replies,
'gt': cmd_get_threads,
'get_threads': cmd_get_threads,
'cd': cmd_cd,
'cb': cmd_cd,
'sel': cmd_selected,
'pwd': cmd_selected,
're': cmd_send_reply,
'reply': cmd_send_reply,
'bp': cmd_blankpost,
'blankfag': cmd_blankpost,
'blankpost': cmd_blankpost,
'nt': cmd_new_thread,
'new_thread': cmd_new_thread,
'pin': cmd_pin_thread,
'unpin': cmd_unpin_thread,
'pinned': cmd_pinned,
'search': cmd_search,
'find': cmd_search,
'search2': cmd_search2,
'find2': cmd_search2,
'!!': cmd_last_cmd,
'r': cmd_last_cmd,
'repeat': cmd_last_cmd,
'blacklist': cmd_blacklist,
'filter': cmd_filter,
'unfilter': cmd_unfilter,
}
def main():
try:
sel = CurrentBoard()
except awoo.AwooException:
print('%s:%d is down.' % (awoo.conn.cfg['host'], awoo.conn.cfg['port']))
exit(1)
# load '$HOME/.awoorc' if it exists
load_rc(sel)
# command auto completion
completer = WordCompleter(list(CMD_DICT.keys()))
while True:
# read line from stdin and eval commands read
try:
line = prompt('>>> ', completer=completer)
eval_awoo(sel, line)
# exit on 'EOF'
except EOFError:
exit(0)
# ignore ctrl-c and such
except (IOError, KeyboardInterrupt):
continue
if __name__ == '__main__':
main()