-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
53 lines (43 loc) · 1.56 KB
/
Copy pathutils.py
File metadata and controls
53 lines (43 loc) · 1.56 KB
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
# utils.py
import re
from datetime import datetime, timedelta
def normalize(s: str) -> str:
return (s or "").strip().lower()
def parse_posted_date(text: str) -> str:
if not text:
return ""
text = text.lower().strip()
now = datetime.now()
if "h ago" in text or "m ago" in text:
return now.strftime("%Y-%m-%d")
m = re.match(r"(\d+)\s*d ago", text)
if m:
days = int(m.group(1))
return (now - timedelta(days=days)).strftime("%Y-%m-%d")
m = re.match(r"(\d+)\s*mo ago", text)
if m:
months = int(m.group(1))
return (now - timedelta(days=months * 30)).strftime("%Y-%m-%d")
m = re.match(r"(\d+)\+?\s*d.*ago", text)
if m:
days = int(m.group(1))
return (now - timedelta(days=days)).strftime("%Y-%m-%d")
try:
dt = datetime.strptime(text, "%d %b %Y")
return dt.strftime("%Y-%m-%d")
except:
return now.strftime("%Y-%m-%d")
def pass_jobtype_filter(job_type_text: str, chosen_job_types: list[str]) -> bool:
if not chosen_job_types: # ALL
return True
jt = normalize(job_type_text)
return any(ch in jt for ch in chosen_job_types)
def pass_state_filter(location_text: str, state_code: str, state_name: str, chosen_states: list[str]) -> bool:
if not chosen_states: # ALL
return True
sc = normalize(state_code)
sn = normalize(state_name)
loc = normalize(location_text)
return any((s == sc) or (s in sn) or (s in loc) for s in chosen_states)
def now_ts() -> str:
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")