-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
58 lines (45 loc) · 1.71 KB
/
Copy pathvalidate.py
File metadata and controls
58 lines (45 loc) · 1.71 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
54
55
56
57
58
from datetime import datetime, timedelta
import requests
import schedule
import time
import json
from colorama import init, Fore, Style
from sqlalchemy.orm import Session
from database import Job, engine
from sqlalchemy import or_
init(autoreset=True)
# Loading values from config.json
with open("config.json", "r") as f:
config = json.load(f)
validation_interval = config["check_interval_days"]
def validate_jobs():
cutoff = datetime.utcnow() - timedelta(days=validation_interval)
with Session(engine) as session:
# Queries all stored jobs and returns the ones that haven't been checked or less than cutoff
stale_jobs = session.query(Job).filter(
Job.is_active == True,
or_(
Job.last_checked == None,
Job.last_checked < cutoff
)
).all()
closed_jobs = 0
for job in stale_jobs:
try:
response = requests.get(job.url, timeout=10)
if response.status_code == 404:
job.is_active = False
closed_jobs += 1
print(f"{Fore.RED} No Longer Exisits: {Style.BRIGHT} {job.url}")
except requests.exceptions.RequestException:
# Pass and don't do anything is there is a network error. Not sure if the job posting exists or not
pass
finally:
job.last_checked = datetime.utcnow()
session.commit()
print(f"{Fore.LIGHTMAGENTA_EX}Validated {len(stale_jobs)} jobs. {closed_jobs} are closed or no longer exist")
validate_jobs()
schedule.every(24).hours.do(validate_jobs)
while True:
schedule.run_pending()
time.sleep(60)