mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-09-15 14:17:54 +08:00
rfc2822_to_timestamp 先用 %z 解析出带偏移的 datetime,再用 replace(tzinfo=timezone.utc) 处理。replace 只是覆盖 tzinfo 而不做换算, +0800 的时间被当成 UTC,算出的时间戳比真实时间晚 8 小时。 改用 astimezone(timezone.utc) 做真正的时区换算,与紧邻上方的 rfc2822_to_china_datetime 保持一致。 影响 store/weibo 落库的 create_time 字段(帖子与评论各一处)。同一条记录里 create_time 与 create_date_time 会相差 8 小时,跨日内容的日期还会整体偏移 一天。 补两条离线单测:一条断言 +0800 输入对应的时间戳,一条断言 create_time 与 create_date_time 指向同一时刻。两条测试在修复前均失败。
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (c) 2025 relakkes@gmail.com
|
|
#
|
|
# This file is part of MediaCrawler project.
|
|
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/test/test_utils.py
|
|
# GitHub: https://github.com/NanmiCoder
|
|
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
|
|
#
|
|
|
|
# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则:
|
|
# 1. 不得用于任何商业用途。
|
|
# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。
|
|
# 3. 不得进行大规模爬取或对平台造成运营干扰。
|
|
# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。
|
|
# 5. 不得用于任何非法或不当的用途。
|
|
#
|
|
# 详细许可条款请参阅项目根目录下的LICENSE文件。
|
|
# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from tools import utils
|
|
|
|
|
|
def test_convert_cookies():
|
|
xhs_cookies = "a1=x000101360; webId=1190c4d3cxxxx125xxx; "
|
|
cookie_dict = utils.convert_str_cookie_to_dict(xhs_cookies)
|
|
assert cookie_dict.get("webId") == "1190c4d3cxxxx125xxx"
|
|
assert cookie_dict.get("a1") == "x000101360"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_convert_browser_context_cookies_uses_url_filter():
|
|
browser_context = AsyncMock()
|
|
browser_context.cookies.return_value = [{"name": "sessionid", "value": "abc"}]
|
|
|
|
cookie_str, cookie_dict = await utils.convert_browser_context_cookies(
|
|
browser_context,
|
|
urls=["https://www.douyin.com"],
|
|
)
|
|
|
|
browser_context.cookies.assert_awaited_once_with(urls=["https://www.douyin.com"])
|
|
assert cookie_str == "sessionid=abc"
|
|
assert cookie_dict == {"sessionid": "abc"}
|
|
|
|
|
|
def test_rfc2822_to_timestamp_converts_utc_offset():
|
|
# 2023-12-23 17:12:54 +0800 == 2023-12-23 09:12:54 UTC == 1703322774
|
|
assert utils.rfc2822_to_timestamp("Sat Dec 23 17:12:54 +0800 2023") == 1703322774
|
|
|
|
|
|
def test_rfc2822_to_timestamp_agrees_with_china_datetime():
|
|
rfc2822_time = "Sat Dec 23 17:12:54 +0800 2023"
|
|
|
|
assert utils.rfc2822_to_timestamp(rfc2822_time) == int(
|
|
utils.rfc2822_to_china_datetime(rfc2822_time).timestamp()
|
|
)
|