-
Notifications
You must be signed in to change notification settings - Fork 0
/
hnjobs.py
699 lines (558 loc) · 18.2 KB
/
hnjobs.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
from typing import (
List,
Optional,
Sequence,
Dict,
Tuple,
Any,
Generator,
ClassVar,
Callable,
Union,
Iterable,
)
from enum import Enum
import os
from dataclasses import dataclass
from html.parser import HTMLParser
from collections import defaultdict
import json
from abc import ABCMeta, abstractmethod
import concurrent.futures
try:
import requests
_session: requests.Session = requests.Session()
def get_json(url: str) -> Optional[Dict[str, Any]]:
response = _session.get(url)
if not (200 <= response.status_code < 300):
return None
return response.json()
except ImportError:
from urllib import request
def get_json(url: str) -> Optional[Dict[str, Any]]:
response = request.urlopen(url)
if not (200 <= response.status < 300):
return None
return json.loads(response.read())
if os.name == "posix":
import sys
import tty
import termios
def getch() -> str:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
else:
print("Sorry, but only posix systems are supported for now")
exit(1)
PERSISTENT_CACHE: bool = True # Everything is lost upon restart if False
HN_API_BASE_URL: str = "https://hacker-news.firebaseio.com/v0"
SAVE_FILE: str = "hnjobs.json"
##############
# Hacker news API is described here: https://github.com/HackerNews/API
##############
class ItemType(str, Enum):
JOB = "job"
STORY = "story"
COMMENT = "comment"
POLL = "poll"
POLLOPT = "pollopt"
@dataclass(kw_only=True, slots=False)
class HNItem(object):
id: int
type: ItemType
time: int # Unix timestamp
title: Optional[str] = None
text: Optional[str] = None # In HTML
parent: Optional[int] = None
kids: Sequence[int] = tuple()
descendants: Optional[int] = None
deleted: Optional[bool] = None
by: Optional[str] = None
url: Optional[str] = None
dead: Optional[bool] = None
score: Optional[int] = None
poll: Optional[int] = None
parts: Optional[List[int]] = None
def get_item_no_cache(id_: int) -> Optional[HNItem]:
dict_item = get_json(HN_API_BASE_URL + f"/item/{id_}.json")
if dict_item is None:
return None
return HNItem(**dict_item)
_item_cache: Dict[int, HNItem] = {}
def get_item_cached(id_: int) -> Optional[HNItem]:
if (item := _item_cache.get(id_, None)) is not None:
return item
item = get_item_no_cache(id_)
if item is not None:
_item_cache[id_] = item
return item
get_item = get_item_cached
class CustomHTMLParser(HTMLParser):
"""An HTML Parser that interprets <br> and <p> tags and replaces
them with line breaks"""
__slots__ = ("parts",)
parts: List[str]
def reset(self) -> None:
self.parts = []
super().reset()
def get_text(self) -> str:
return "".join(self.parts)
def handle_starttag(self, tag: str, attrs: List[Tuple[str, Any]]) -> None:
match tag:
case "br":
self.parts.append("\n")
case "p":
self.parts.append("\n\n")
def handle_endtag(self, tag: str) -> None:
pass
def handle_data(self, data: str) -> None:
self.parts.append(data)
def html_to_text(html: Optional[str]) -> str:
if html is None:
return ""
parser = CustomHTMLParser()
parser.feed(html)
parser.close()
return parser.get_text()
def display_item(item: HNItem) -> None:
os.system("clear")
if item.title is not None:
print(f"{item.title}\n")
print(html_to_text(item.text))
def get_all_kids(base_item: HNItem) -> Generator[HNItem, None, None]:
for id_ in base_item.kids:
if (item := get_item(id_)) is not None:
yield item
def command(arg: Union[Callable, str]) -> Callable:
"""A decorator that annotates a Callable with a __shortcut__ attribute
which is a 1-character str desribing the key used to run the command"""
if callable(arg):
key = arg.__name__[0]
elif isinstance(arg, str):
if len(arg) != 1:
raise Exception("Command shortcuts must be a single character")
key = arg
def annotate_func(func: Callable) -> Callable:
func.__shortcut__ = key # type: ignore
return func
if callable(arg):
return annotate_func(arg)
return annotate_func
class UserInterface(object, metaclass=ABCMeta):
"""Abstract class that expands subclasses and transforms them into
functional (but not necessarily user-friendly) terminal-based interfaces"""
__slots__ = ("_run",)
# str describing tooltips (shortcuts) for user interface commands
_tooltips_line: ClassVar[str]
# Assigns shortcuts to command functions
_tooltips_dict: ClassVar[Dict[str, Callable[[Any], None]]]
_run: bool
def __init__(self):
super().__init__()
self._run = True
@staticmethod
def register_command(
dct: Dict[str, Callable], shortcut: str, command: Callable
) -> None:
if shortcut in dct:
shortcut = shortcut.swapcase()
if shortcut in dct:
raise Exception(
f"Cannot register command {command} with shortcut {shortcut}"
)
dct[shortcut] = command
@classmethod
def __init_subclass__(cls, /, **kwargs) -> None:
super().__init_subclass__(**kwargs)
ttd: Dict[str, Callable[[Any], None]] = {}
for attr_name in dir(cls):
try:
attr = getattr(cls, attr_name)
except AttributeError:
continue
if (sc := getattr(attr, "__shortcut__", None)) is not None:
cls.register_command(ttd, sc, attr)
cls._tooltips_dict = ttd
tooltips: List[str] = []
for k, v in ttd.items():
name = v.__name__
if k.lower() == name[0].lower():
name = name[1:]
tooltips.append(f"({k}){name}")
cls._tooltips_line = " ".join(tooltips)
def wait_command(self) -> None:
while True:
ch = getch()
if ch not in self._tooltips_dict:
continue
break
self._tooltips_dict[ch](self)
@abstractmethod
def update_display(self) -> str:
raise NotImplementedError
@classmethod
def print_tooltips(cls) -> None:
print(f"{cls._tooltips_line}\n\n")
def refresh(self) -> None:
os.system("clear")
self.print_tooltips()
print(self.update_display())
def loop(self) -> None:
while self._run:
self.refresh()
self.wait_command()
def stop(self) -> None:
self._run = False
_item_user_tags: Dict[int, List[str]] = defaultdict(list)
_item_user_ratings: Dict[int, int] = {}
class MainInterface(UserInterface):
__slots__ = ("display",)
def __init__(self):
super().__init__()
self.display = ""
def update_display(self) -> str:
return "Main interface\n\n" + self.display
def display_now(self, s: str) -> None:
self.display = s
self.refresh()
@command
def quit(self) -> None:
self.stop()
@command
def update_jobs(self) -> None:
self.display_now("Please enter WhoIsHiring link or item id: ")
i = input()
try:
i = i.split("id=")[-1]
i = i.split("&")[0]
id_ = int(i, 10)
except Exception:
self.display_now("Bad link or id")
return
self.display_now("Fetching...")
# Do not use item cache for whoishiring root post, so we can get new
# posts if there are some
item = get_item_no_cache(id_)
if not item:
self.display_now("Could not fetch HN post!\n")
return
if item.by != "whoishiring":
self.display_now("This does not seem to be a WhoIsHiring post!\n")
return
self.display_now(
f"There are {len(item.kids)} comments here, fetch them? [y/n]"
)
while True:
c = getch()
if c.lower() == "n":
return
if c.lower() == "y":
break
self.display += "\nplease enter y or n"
self.refresh()
total = len(item.kids)
self.display_now(f"fetching {total} items...")
n = 0
with concurrent.futures.ThreadPoolExecutor(
max_workers=min(32, os.cpu_count() * 5)) as executor:
for future in executor.map(get_item, item.kids):
n += 1
self.display_now(f"{n}/{total} comments fetched")
self.display += "\ndone."
@command
def select_some_items(self) -> None:
SelectorInterface().loop()
class InvalidFilterOrSorter(Exception):
pass
FILTER_FUNCS = {
"tag": lambda tag: lambda item: tag in _item_user_tags[item.id],
"rated": lambda _: lambda item: item.id in _item_user_ratings,
"contains": lambda s: lambda item:
item.text and (s.lower() in item.text.lower()),
}
def filter_from_str(s: str) -> Callable:
inverted = False
if s.startswith("!"):
inverted = True
s = s[1:]
parts = s.split(":")
if len(parts) == 1:
filter_name, arg = parts[0], None
elif len(parts) == 2:
filter_name, arg = parts
else:
raise InvalidFilterOrSorter("Too many ':'")
try:
func = FILTER_FUNCS[filter_name](arg)
if inverted:
return lambda x: not func(x)
return func
except Exception as e:
raise InvalidFilterOrSorter(e)
SORTER_FUNCS = {
"tag": lambda tag: lambda item:
0 if tag in _item_user_tags[item.id] else 1,
"recent": lambda _: lambda item: -item.time,
# It is strange to compare int with floats, but inf is quite useful here...
"rating": lambda _: lambda item:
-_item_user_ratings.get(item.id, float("-inf")),
"contains": lambda s: lambda item:
0 if (item.text and (s.lower() in item.text.lower())) else 1,
}
def sorter_from_str(s: str) -> Callable:
inverted = False
if s.startswith("!"):
inverted = True
s = s[1:]
parts = s.split(":")
if len(parts) == 1:
sorter_name, arg = parts[0], None
elif len(parts) == 2:
sorter_name, arg = parts
else:
raise InvalidFilterOrSorter("Too many ':'")
try:
func = SORTER_FUNCS[sorter_name](arg)
if inverted:
return lambda x: -func(x)
return func
except Exception as e:
raise InvalidFilterOrSorter(e)
class SelectorInterface(UserInterface):
__slots__ = "display", "filters", "sorters"
display: str
help: ClassVar[str] = (
"Filter/sorter format: (!)<name>(:value)\n"
"'!' inverts a filter/sorter\n"
f"Available filters: {', '.join(FILTER_FUNCS.keys())}\n"
f"Available sorters: {', '.join(SORTER_FUNCS.keys())}\n"
)
filters: List[str]
sorters: List[str]
def _summary(self) -> None:
self.display = (
f"{self.help}\n"
f"Current filters: {self.filters}\n"
f"Current sorters: {self.sorters}\n"
)
self.refresh()
def __init__(self):
super().__init__()
self.filters = []
self.sorters = []
self._summary()
def update_display(self) -> str:
return self.display
def display_now(self, s: str) -> None:
self.display = s
self.refresh()
@command("f")
def update_filters(self) -> None:
new_filters_str = input("\nEnter new filters separated with ','\n")
if new_filters_str == "":
new_filters = []
else:
new_filters = new_filters_str.replace(" ", "").split(",")
for f in new_filters:
try:
filter_from_str(f)
except InvalidFilterOrSorter as e:
self.display += f"Filter {f} is invalid: {e}"
return
self.filters = new_filters
self._summary()
@command("s")
def update_sorters(self) -> None:
new_sorters_str = input("\nEnter new sorters separated with ','\n")
if new_sorters_str == "":
new_sorters = []
else:
new_sorters = new_sorters_str.replace(" ", "").split(",")
for s in new_sorters:
try:
sorter_from_str(s)
except InvalidFilterOrSorter as e:
self.display += f"Sorter {s} is invalid: {e}"
return
self.sorters = new_sorters
self._summary()
def _get_selected(self) -> List[HNItem]:
items: Iterable[HNItem] = filter(
lambda item: item.type == ItemType.COMMENT, _item_cache.values()
)
for f in self.filters:
items = filter(filter_from_str(f), items)
filtered_items: List[HNItem] = list(items)
for s in self.sorters[::-1]:
filtered_items.sort(key=sorter_from_str(s))
return filtered_items
@command
def review_selected(self) -> None:
self.stop()
ReviewInterface(self._get_selected()).loop()
@command("R")
def quick_review_selected(self) -> None:
self.stop()
QuickReviewInterface(self._get_selected()).loop()
@command
def tag_selected(self) -> None:
tag = input("\nEnter the tag to add to matching items: ")
for item in self._get_selected():
tags = _item_user_tags[item.id]
if tag not in tags:
tags.append(tag)
@command
def quit(self) -> None:
self.stop()
class ReviewInterface(UserInterface):
__slots__ = "current_index", "items"
items: List[HNItem]
current_index: int
def __init__(self, items: List[HNItem]):
self.items = items
if not items:
self.stop()
return
self.current_index = 0
super().__init__()
@property
def current_item(self) -> HNItem:
return self.items[self.current_index]
def update_display(self) -> str:
item = self.current_item
return (
f"Item {self.current_index + 1}/{len(self.items)}\n"
f"Rating: {_item_user_ratings.get(item.id, '???')}\n"
f"Tags: {_item_user_tags[item.id]}\n"
"===============================================================\n"
f"{html_to_text(item.text)}"
)
@command
def next(self) -> None:
self.current_index += 1
self.current_index = min(len(self.items) - 1, self.current_index)
@command
def previous(self) -> None:
self.current_index -= 1
self.current_index = max(0, self.current_index)
@command("t")
def add_tags(self) -> None:
tags = _item_user_tags[self.current_item.id]
new_tags = input("Enter new tags separated by ',':\n").split("'")
for tag in new_tags:
if tag not in tags:
tags.append(tag)
@command
def rate(self):
try:
rating = int(input("Enter new rating:\n"), 10)
_item_user_ratings[self.current_item.id] = rating
except ValueError:
return
@command
def quit(self) -> None:
self.stop()
class QuickReviewInterface(UserInterface):
__slots__ = "current_index", "items"
items: List[HNItem]
current_index: int
def __init__(self, items: List[HNItem]):
self.items = items
if not items:
self.stop()
return
self.current_index = 0
super().__init__()
@property
def current_item(self) -> HNItem:
return self.items[self.current_index]
def update_display(self) -> str:
item = self.current_item
return (
f"Item {self.current_index + 1}/{len(self.items)}\n"
f"Rating: {_item_user_ratings.get(item.id, '???')}\n"
f"Tags: {_item_user_tags[item.id]}\n"
"===============================================================\n"
f"{html_to_text(item.text)}"
)
@command
def next(self) -> None:
self.current_index += 1
self.current_index = min(len(self.items) - 1, self.current_index)
@command
def previous(self) -> None:
self.current_index -= 1
self.current_index = max(0, self.current_index)
@command("t")
def add_tags(self) -> None:
tags = _item_user_tags[self.current_item.id]
new_tags = input("Enter new tags separated by ',':\n").split("'")
for tag in new_tags:
if tag not in tags:
tags.append(tag)
@command("j")
def no_0(self) -> None:
_item_user_ratings[self.current_item.id] = 0
self.next()
@command("k")
def maybe_no_4(self) -> None:
_item_user_ratings[self.current_item.id] = 4
self.next()
@command("l")
def maybe_yes_7(self) -> None:
_item_user_ratings[self.current_item.id] = 7
self.next()
@command("m")
def yes_10(self) -> None:
_item_user_ratings[self.current_item.id] = 10
self.next()
@command
def quit(self) -> None:
self.stop()
def save() -> None:
to_save = {
"tags": _item_user_tags,
"ratings": _item_user_ratings,
}
if PERSISTENT_CACHE:
to_save["cache"] = dict(
(k, v.__dict__)
for k, v in _item_cache.items()
)
json.dump(to_save, open(SAVE_FILE, "w"))
def load() -> None:
global _item_user_tags
global _item_user_ratings
global _item_cache
loaded: dict = json.load(open(SAVE_FILE, "r"))
_item_user_tags = defaultdict(
list, ((int(k), v) for k, v in loaded["tags"].items())
)
_item_user_ratings = dict(
(int(k), v)
for k, v in loaded["ratings"].items()
)
_item_cache = dict(
(int(k, 10), HNItem(**v))
for k, v in loaded.get("cache", {}).items()
)
def main() -> None:
interface = MainInterface()
interface.loop()
if __name__ == "__main__":
try:
load()
except FileNotFoundError:
pass
try:
main()
except KeyboardInterrupt:
pass
finally:
save()