{"record":{"id":"0a82421468c1c735","repo":"NanmiCoder/MediaCrawler","slug":"wrong-time-range-please-check-your-start-and-end","errorCode":null,"errorMessage":"Wrong time range, please check your start and end argument, to ensure that the start cannot exceed end","messagePattern":"Wrong time range, please check your start and end argument, to ensure that the start cannot exceed end","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"media_platform/bilibili/core.py","lineNumber":175,"sourceCode":"        ---\n        :param start: Publish date start time, YYYY-MM-DD\n        :param end: Publish date end time, YYYY-MM-DD\n\n        Note\n        ---\n        - Search time range is from start to end, including both start and end\n        - 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\n            - For example, searching only 2024-01-05 content, pubtime_begin_s = 1704384000, pubtime_end_s = 1704470399\n              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)\n        - 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\n            - For example, searching 2024-01-05 - 2024-01-06 content, pubtime_begin_s = 1704384000, pubtime_end_s = 1704556799\n              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)\n        \"\"\"\n        # Convert start and end to datetime objects\n        start_day: datetime = datetime.strptime(start, \"%Y-%m-%d\")\n        end_day: datetime = datetime.strptime(end, \"%Y-%m-%d\")\n        if start_day > end_day:\n            raise ValueError(\"Wrong time range, please check your start and end argument, to ensure that the start cannot exceed end\")\n        elif start_day == end_day:  # Searching content from the same day\n            end_day = (start_day + timedelta(days=1) - timedelta(seconds=1))  # Set end_day to start_day + 1 day - 1 second\n        else:  # Searching from start to end\n            end_day = (end_day + timedelta(days=1) - timedelta(seconds=1))  # Set end_day to end_day + 1 day - 1 second\n        # Convert back to timestamps\n        return str(int(start_day.timestamp())), str(int(end_day.timestamp()))\n\n    async def search_by_keywords(self):\n        \"\"\"\n        search bilibili video with keywords in normal mode\n        :return:\n        \"\"\"\n        utils.logger.info(\"[BilibiliCrawler.search_by_keywords] Begin search bilibli keywords\")\n        bili_limit_count = 20  # bilibili limit page fixed value\n        if config.CRAWLER_MAX_NOTES_COUNT < bili_limit_count:\n            config.CRAWLER_MAX_NOTES_COUNT = bili_limit_count\n        start_page = config.START_PAGE  # start page number\n        for keyword in config.KEYWORDS.split(\",\"):","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/NanmiCoder/MediaCrawler/blob/d6f7c5bb906b6dac40ddf343ef9e26438a3de092/media_platform/bilibili/core.py#L157-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Swap the arguments so start <= end, using the exact YYYY-MM-DD format both expect.","Validate/normalize the pair (auto-swap or reject) in the API layer before calling the crawler.","Add a date-picker constraint in the UI that forbids end before start."],"exampleFix":"# before\nawait crawler.search_by_date(start='2024-06-02', end='2024-01-05')\n\n# after\nawait crawler.search_by_date(start='2024-01-05', end='2024-06-02')","handlingStrategy":"validation","validationCode":"from datetime import datetime\ns, e = datetime.strptime(start, '%Y-%m-%d'), datetime.strptime(end, '%Y-%m-%d')\nif s > e:\n    start, end = end, start  # or raise your own clear error","typeGuard":"def is_valid_date_range(start: str, end: str) -> bool:\n    try:\n        return datetime.strptime(start, '%Y-%m-%d') <= datetime.strptime(end, '%Y-%m-%d')\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    await bilibili_crawler.search(start, end)\nexcept ValueError as e:\n    if 'time range' in str(e):\n        raise ValueError('start date must be on or before end date') from e","preventionTips":["Always format dates as YYYY-MM-DD","Sort/validate the date pair before passing it to the crawler","Constrain date pickers so end < start is unselectable"],"tags":["bilibili","validation","date-range","valueerror"],"backgroundTag":null,"analyzedSha":"d6f7c5bb906b6dac40ddf343ef9e26438a3de092","analyzedAt":"2026-08-15T01:39:07.505Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}