forked from freelawproject/judge-pics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
78 lines (63 loc) · 2.05 KB
/
search.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
import json
import os
import re
from enum import Enum
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Literal, Optional, List, Union
import climage
import requests
from fuzzywuzzy import fuzz
ROOT = os.path.dirname(os.path.abspath(__file__))
with Path(ROOT, "data", "people.json").open() as f:
judges = json.load(f)
class ImageSizes(Enum):
SMALL = 128
MEDIUM = 256
LARGE = 512
ORIGINAL = "orig"
SIZES = Literal[
ImageSizes.SMALL, ImageSizes.MEDIUM, ImageSizes.LARGE, ImageSizes.ORIGINAL
]
def query(search_str: str, size: SIZES = ImageSizes.MEDIUM) -> Optional[List]:
"""Find a judge by name"""
paths = [j["path"] for j in judges]
xlist = []
for path in paths:
matching_path = re.sub(r"[\d-]+", " ", path).strip()
m = fuzz.token_sort_ratio(matching_path, search_str.lower())
xlist.append((path, m))
if m > 95:
return [f"https://portraits.free.law/v2/{size}/{path}.jpeg"]
xlist.sort(key=lambda y: -y[1])
if len(xlist) == 0:
return None
return [
f"https://portraits.free.law/v2/{size}/{x[0]}.jpeg"
for x in xlist
if x[1] > 10
]
def portrait(
person: Union[str, int], size: SIZES = ImageSizes.ORIGINAL
) -> Optional[str]:
"""Get URL for portait on free.law"""
if type(person) == int:
paths = [x for x in judges if x["person"] == person]
if len(paths) > 0:
return f"https://portraits.free.law/v2/{size.value}/{paths[0]['path']}.jpeg"
else:
return None
else:
matches = query(person, size.value)
if matches:
return matches[0]
return None
def show(person: int, size: ImageSizes = None) -> str:
"""Get the image as ANSI escape codes so you can print it out"""
url = portrait(person, size)
r = requests.get(url, timeout=10)
with NamedTemporaryFile(suffix=".jpeg") as tmp:
with open(tmp.name, "wb") as f:
f.write(r.content)
output = climage.convert(tmp.name, width=40)
return output