NanmiCoder/MediaCrawler · error · ValueError

Wrong time range, please check your start and end argument,

Error message

Wrong time range, please check your start and end argument, to ensure that the start cannot exceed end

What it means

ValueError from BilibiliCrawler's date-range conversion (media_platform/bilibili/core.py) when the 'start' date parses later than the 'end' date. The method parses both with '%Y-%m-%d' and explicitly refuses inverted ranges before converting them to bilibili's pubtime_begin_s/pubtime_end_s epoch-second bounds.

Source

Thrown at media_platform/bilibili/core.py:175

        ---
        :param start: Publish date start time, YYYY-MM-DD
        :param end: Publish date end time, YYYY-MM-DD

        Note
        ---
        - Search time range is from start to end, including both start and end
        - To search content from the same day, to include search content from that day, pubtime_end_s should be pubtime_begin_s plus one day minus one second, i.e., the last second of start day
            - For example, searching only 2024-01-05 content, pubtime_begin_s = 1704384000, pubtime_end_s = 1704470399
              Converted to readable datetime objects: pubtime_begin_s = datetime.datetime(2024, 1, 5, 0, 0), pubtime_end_s = datetime.datetime(2024, 1, 5, 23, 59, 59)
        - To search content from start to end, to include search content from end day, pubtime_end_s should be pubtime_end_s plus one day minus one second, i.e., the last second of end day
            - For example, searching 2024-01-05 - 2024-01-06 content, pubtime_begin_s = 1704384000, pubtime_end_s = 1704556799
              Converted to readable datetime objects: pubtime_begin_s = datetime.datetime(2024, 1, 5, 0, 0), pubtime_end_s = datetime.datetime(2024, 1, 6, 23, 59, 59)
        """
        # Convert start and end to datetime objects
        start_day: datetime = datetime.strptime(start, "%Y-%m-%d")
        end_day: datetime = datetime.strptime(end, "%Y-%m-%d")
        if start_day > end_day:
            raise ValueError("Wrong time range, please check your start and end argument, to ensure that the start cannot exceed end")
        elif start_day == end_day:  # Searching content from the same day
            end_day = (start_day + timedelta(days=1) - timedelta(seconds=1))  # Set end_day to start_day + 1 day - 1 second
        else:  # Searching from start to end
            end_day = (end_day + timedelta(days=1) - timedelta(seconds=1))  # Set end_day to end_day + 1 day - 1 second
        # Convert back to timestamps
        return str(int(start_day.timestamp())), str(int(end_day.timestamp()))

    async def search_by_keywords(self):
        """
        search bilibili video with keywords in normal mode
        :return:
        """
        utils.logger.info("[BilibiliCrawler.search_by_keywords] Begin search bilibli keywords")
        bili_limit_count = 20  # bilibili limit page fixed value
        if config.CRAWLER_MAX_NOTES_COUNT < bili_limit_count:
            config.CRAWLER_MAX_NOTES_COUNT = bili_limit_count
        start_page = config.START_PAGE  # start page number
        for keyword in config.KEYWORDS.split(","):

View on GitHub (pinned to d6f7c5bb90)

Solutions

  1. Swap the arguments so start <= end, using the exact YYYY-MM-DD format both expect.
  2. Validate/normalize the pair (auto-swap or reject) in the API layer before calling the crawler.
  3. Add a date-picker constraint in the UI that forbids end before start.

Example fix

# before
await crawler.search_by_date(start='2024-06-02', end='2024-01-05')

# after
await crawler.search_by_date(start='2024-01-05', end='2024-06-02')
Defensive patterns

Strategy: validation

Validate before calling

from datetime import datetime
s, e = datetime.strptime(start, '%Y-%m-%d'), datetime.strptime(end, '%Y-%m-%d')
if s > e:
    start, end = end, start  # or raise your own clear error

Type guard

def is_valid_date_range(start: str, end: str) -> bool:
    try:
        return datetime.strptime(start, '%Y-%m-%d') <= datetime.strptime(end, '%Y-%m-%d')
    except ValueError:
        return False

Try / catch

try:
    await bilibili_crawler.search(start, end)
except ValueError as e:
    if 'time range' in str(e):
        raise ValueError('start date must be on or before end date') from e

Prevention

When it happens

Trigger: Passing CrawlerStartRequest/start-end config where start='2024-06-02' and end='2024-01-05'; copy-paste swapping the two fields; building the strings from a date picker that returns end first.

Common situations: Config files with START_DAY/END_DAY (or equivalent) edited manually in the wrong order; timezone confusion when generating midnight timestamps that end up crossing dates; UIs that submit a range picker's value as (end, start).

Related errors


AI-assisted analysis of NanmiCoder/MediaCrawler@d6f7c5bb90 (2026-08-15). Data as JSON: /api/errors/0a82421468c1c735. Report an issue: GitHub.