Skip to content

Commit

Permalink
improve date validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruz Núñez authored and Cruz Núñez committed Dec 19, 2024
1 parent 0cebebe commit 548441c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions city_scrapers/spiders/cinoh_Civil_Service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import datetime
from datetime import datetime

import scrapy
from city_scrapers_core.constants import COMMISSION
from city_scrapers_core.items import Meeting
from city_scrapers_core.spiders import CityScrapersSpider
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta


class CinohCivilServiceSpider(CityScrapersSpider):
Expand All @@ -30,18 +31,23 @@ def parse(self, response):
Parse JSON response.
"""

year = str(datetime.datetime.today().year)
lower_limit = datetime.now() - relativedelta(months=6)

data = response.json()

for item in data:
numb = item.get("numberdate")

# skip iteration if meeting is not for current year
if numb is None or year not in numb:
# skip if no date or meeting is too old
if numb is None:
continue

# if we are in current year then parse meeting
# skip if date is too old
meeting_date = parse(numb)
if meeting_date < lower_limit:
continue

# if date is valid then parse meeting
meeting = Meeting(
title=item["name"],
description="",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cinoh_Civil_Service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def test_count():
assert len(parsed_items) == 21
assert len(parsed_items) == 12


def test_title():
Expand Down

0 comments on commit 548441c

Please sign in to comment.