'How to return values only within a specific date range?

I have a program that scrapes through an API and gets the required values from the fields. There is a field called published_date one act json object. I want to publish only the values for the last 2 months from current date.

try:
    price = str(price).replace(',', '')
    price = Decimal(price)

    if date < end:

        if not math.isnan(price):
            report_item = PriceItem(
            source=SOURCE,
            source_url=crawled_url,
            original_index_id=original_index_id,
            index_specification=index_specification,
            published_date=date,
            price=price.quantize(Decimal('1.00'))
        )



            yield report_item
except DecimalException as ex:
    self.logger.error(f"Non decimal price of {price} "
                      f"found in {original_index_id}", exc_info=ex)

The published date is extracted:

                     for report_date in REPORT_DATE_TYPES:
                        if report_date in result:
                            date = result[report_date].split(' ')[0]
                            date = datetime.strptime(date, '%m/%d/%Y')
MAX_REPORT_MONTHS = 3
current_date = datetime.now()
current_date_str = current_date.strftime('%m/%d/%Y')
start = datetime.strptime(current_date_str, '%m/%d/%Y')

last_date = current_date - relativedelta(months=MAX_REPORT_MONTHS)
last_date_str = last_date.strftime('%m/%d/%Y')
end = datetime.strptime(last_date_str, '%m/%d/%Y')

The above I say last date string and current date string.

Extract of the api:

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source