parsehub 2.3.14__tar.gz → 2.3.16__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {parsehub-2.3.14/src/parsehub.egg-info → parsehub-2.3.16}/PKG-INFO +2 -2
- {parsehub-2.3.14 → parsehub-2.3.16}/pyproject.toml +2 -2
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/instagram.py +51 -21
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/xhs.py +1 -22
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/zuiyou.py +2 -1
- parsehub-2.3.16/src/parsehub/provider_api/instagram.py +356 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/xhs.py +6 -9
- {parsehub-2.3.14 → parsehub-2.3.16/src/parsehub.egg-info}/PKG-INFO +2 -2
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub.egg-info/requires.txt +1 -1
- {parsehub-2.3.14 → parsehub-2.3.16}/test/test_core_offline.py +1 -2
- parsehub-2.3.14/src/parsehub/provider_api/instagram.py +0 -433
- {parsehub-2.3.14 → parsehub-2.3.16}/LICENSE +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/README.md +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/setup.cfg +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/cli.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/cli_config.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/config/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/config/config.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/errors.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/base/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/base/base.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/base/ytdlp.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/bangumi.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/bilibili.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/coolapk.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/douban.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/douyin.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/facebook.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/kuaishou.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/medium.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/pipix.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/snapchat.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/threads.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/tieba.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/tiktok.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/toutiao.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/twitter.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/weibo.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/weixin.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/wxchannels.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/xiaoheihe.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/youtube.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/parsers/parser/zhihu.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/bangumi.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/bilibili.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/coolapk.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/douban.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/douyin.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/kuaishou.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/medium.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/pipix.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/threads.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/tieba.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/tiktok.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/toutiao.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/twitter.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/weibo.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/weixin.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/wxchannels.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/xiaoheihe.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/zhihu.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/provider_api/zuiyou.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/__init__.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/callback.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/media_file.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/media_ref.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/platform.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/post.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/types/result.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/utils/downloader.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/utils/fallback.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/utils/helpers.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub/utils/media_info.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub.egg-info/SOURCES.txt +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub.egg-info/dependency_links.txt +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub.egg-info/entry_points.txt +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/src/parsehub.egg-info/top_level.txt +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/test/test_bilibili_offline.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/test/test_cli.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/test/test_cli_config.py +0 -0
- {parsehub-2.3.14 → parsehub-2.3.16}/test/test_downloader.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: parsehub
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.16
|
|
4
4
|
Summary: 轻量、异步、开箱即用的社交媒体聚合解析库
|
|
5
5
|
Author-email: 梓澪 <zilingmio@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -22,7 +22,7 @@ Requires-Dist: pydub>=0.25.1
|
|
|
22
22
|
Requires-Dist: python-dotenv>=1.0.1
|
|
23
23
|
Requires-Dist: tenacity>=8.5.0
|
|
24
24
|
Requires-Dist: urlextract>=1.9.0
|
|
25
|
-
Requires-Dist: yt-dlp[default]>=2026.10.
|
|
25
|
+
Requires-Dist: yt-dlp[default]>=2026.10.26.220202.dev0
|
|
26
26
|
Requires-Dist: lxml>=5.3.0
|
|
27
27
|
Requires-Dist: pydantic>=1.10.19
|
|
28
28
|
Requires-Dist: markdownify>=1.1.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "parsehub"
|
|
3
|
-
version = "2.3.
|
|
3
|
+
version = "2.3.16"
|
|
4
4
|
description = "轻量、异步、开箱即用的社交媒体聚合解析库"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12.0"
|
|
@@ -25,7 +25,7 @@ dependencies = [
|
|
|
25
25
|
"python-dotenv>=1.0.1",
|
|
26
26
|
"tenacity>=8.5.0",
|
|
27
27
|
"urlextract>=1.9.0",
|
|
28
|
-
"yt-dlp[default]>=2026.10.
|
|
28
|
+
"yt-dlp[default]>=2026.10.26.220202.dev0",
|
|
29
29
|
"lxml>=5.3.0",
|
|
30
30
|
"pydantic>=1.10.19",
|
|
31
31
|
"markdownify>=1.1.0",
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import re
|
|
2
2
|
|
|
3
|
+
from loguru import logger
|
|
4
|
+
|
|
3
5
|
from ...provider_api.instagram import (
|
|
4
6
|
InstagramAPI,
|
|
5
7
|
InstagramAPIError,
|
|
@@ -19,6 +21,7 @@ from ...types import (
|
|
|
19
21
|
)
|
|
20
22
|
from ...utils.helpers import SecretCookie
|
|
21
23
|
from ..base.base import BaseParser, FallbackStage
|
|
24
|
+
from ..base.ytdlp import YtParser, YtVideoParseResult
|
|
22
25
|
|
|
23
26
|
|
|
24
27
|
class InstagramParser(BaseParser):
|
|
@@ -34,29 +37,56 @@ class InstagramParser(BaseParser):
|
|
|
34
37
|
|
|
35
38
|
post = await self._parse(shortcode)
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
match post.typename:
|
|
40
|
+
match post.type:
|
|
40
41
|
case InstagramMediaType.SIDECAR:
|
|
41
42
|
media = [
|
|
42
|
-
VideoRef(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
VideoRef(
|
|
44
|
+
url=item.video_url,
|
|
45
|
+
thumb_url=item.display_url,
|
|
46
|
+
duration=int(item.duration),
|
|
47
|
+
width=item.width,
|
|
48
|
+
height=item.height,
|
|
49
|
+
)
|
|
50
|
+
if item.is_video
|
|
51
|
+
else ImageRef(url=item.display_url, width=item.width, height=item.height)
|
|
52
|
+
for item in post.media
|
|
46
53
|
]
|
|
47
54
|
return MultimediaParseResult(media=media, title=post.title, content=post.caption)
|
|
48
55
|
case InstagramMediaType.IMAGE:
|
|
56
|
+
item = post.media[0]
|
|
49
57
|
return ImageParseResult(
|
|
50
|
-
photo=[ImageRef(url=
|
|
58
|
+
photo=[ImageRef(url=item.display_url, width=item.width, height=item.height)],
|
|
59
|
+
title=post.title,
|
|
60
|
+
content=post.caption,
|
|
51
61
|
)
|
|
52
62
|
case InstagramMediaType.VIDEO:
|
|
63
|
+
item = post.media[0]
|
|
64
|
+
try:
|
|
65
|
+
info = await InstagramYtParse(proxy=self.proxy)._parse(f"https://www.instagram.com/p/{shortcode}/")
|
|
66
|
+
except ParseError as e:
|
|
67
|
+
logger.warning(f"Instagram yt-dlp 解析失败, 使用直链视频: {e}")
|
|
68
|
+
else:
|
|
69
|
+
return YtVideoParseResult(
|
|
70
|
+
dl=info,
|
|
71
|
+
title=post.title,
|
|
72
|
+
content=post.caption,
|
|
73
|
+
video=VideoRef(
|
|
74
|
+
url=info.url,
|
|
75
|
+
thumb_url=item.display_url or info.thumbnail,
|
|
76
|
+
width=info.width,
|
|
77
|
+
height=info.height,
|
|
78
|
+
duration=int(info.duration or item.duration),
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
if not item.video_url:
|
|
82
|
+
raise ParseError("未获取到视频地址")
|
|
53
83
|
return VideoParseResult(
|
|
54
84
|
video=VideoRef(
|
|
55
|
-
url=
|
|
56
|
-
thumb_url=
|
|
57
|
-
duration=int(
|
|
58
|
-
width=width,
|
|
59
|
-
height=height,
|
|
85
|
+
url=item.video_url,
|
|
86
|
+
thumb_url=item.display_url,
|
|
87
|
+
duration=int(item.duration),
|
|
88
|
+
width=item.width,
|
|
89
|
+
height=item.height,
|
|
60
90
|
),
|
|
61
91
|
title=post.title,
|
|
62
92
|
content=post.caption,
|
|
@@ -80,14 +110,10 @@ class InstagramParser(BaseParser):
|
|
|
80
110
|
return await api.get_post(shortcode)
|
|
81
111
|
except ContentUnavailableError:
|
|
82
112
|
raise
|
|
83
|
-
except
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
text = f"无法获取帖子内容: {e}"
|
|
88
|
-
else:
|
|
89
|
-
text = f"无法获取帖子内容(可能为私人内容): {e}"
|
|
90
|
-
raise ParseError(text) from e
|
|
113
|
+
except InstagramRestrictedError as e:
|
|
114
|
+
raise ParseError(str(e)) from e
|
|
115
|
+
except InstagramAPIError as e:
|
|
116
|
+
raise ParseError(f"无法获取帖子内容: {e}") from e
|
|
91
117
|
|
|
92
118
|
@staticmethod
|
|
93
119
|
def get_short_code(url: str) -> str | None:
|
|
@@ -96,4 +122,8 @@ class InstagramParser(BaseParser):
|
|
|
96
122
|
return shortcode.group(2).split("/")[0] if shortcode else None
|
|
97
123
|
|
|
98
124
|
|
|
125
|
+
class InstagramYtParse(YtParser, register=False):
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
|
|
99
129
|
__all__ = ["InstagramParser"]
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import re
|
|
2
2
|
from typing import Union
|
|
3
3
|
|
|
4
|
-
import httpx
|
|
5
|
-
|
|
6
4
|
from ...provider_api.xhs import MOBILE_HEADERS, PC_HEADERS, XHSAPI, XHSMedia, XHSMediaType, XHSPost, XHSPostType
|
|
7
5
|
from ...types import (
|
|
8
6
|
ContentUnavailableError,
|
|
@@ -81,12 +79,8 @@ class XHSParser(BaseParser):
|
|
|
81
79
|
LivePhotoRef(url=i.thumb_url or "", video_url=i.url, width=i.width, height=i.height)
|
|
82
80
|
)
|
|
83
81
|
else:
|
|
84
|
-
# 小红书图片格式: "png" | "webp" | "jpeg" | "heic" | "avif"
|
|
85
|
-
ext = await self.get_ext_by_url(i.url)
|
|
86
|
-
if ext not in ["png", "webp", "jpeg", "heic", "avif"]:
|
|
87
|
-
ext = "jpeg"
|
|
88
82
|
photos.append(
|
|
89
|
-
ImageRef(url=i.url, ext=
|
|
83
|
+
ImageRef(url=i.url, ext="jpg", thumb_url=i.thumb_url, width=i.width, height=i.height)
|
|
90
84
|
)
|
|
91
85
|
|
|
92
86
|
return ImageParseResult(
|
|
@@ -97,21 +91,6 @@ class XHSParser(BaseParser):
|
|
|
97
91
|
case _:
|
|
98
92
|
raise ParseError("不支持的类型")
|
|
99
93
|
|
|
100
|
-
async def get_ext_by_url(self, url: str) -> str:
|
|
101
|
-
async with httpx.AsyncClient(proxy=self.proxy) as client:
|
|
102
|
-
try:
|
|
103
|
-
response = await client.head(url, follow_redirects=True)
|
|
104
|
-
except Exception:
|
|
105
|
-
return ""
|
|
106
|
-
|
|
107
|
-
if content_type := response.headers.get("content-type"):
|
|
108
|
-
media_type = content_type.split(";")[0].strip()
|
|
109
|
-
if "/" in media_type:
|
|
110
|
-
extension = media_type.split("/")[-1]
|
|
111
|
-
return str(extension)
|
|
112
|
-
|
|
113
|
-
return ""
|
|
114
|
-
|
|
115
94
|
@staticmethod
|
|
116
95
|
def hashtag_handler(desc: str | None) -> str:
|
|
117
96
|
if not desc:
|
|
@@ -6,7 +6,8 @@ from ..base.base import BaseParser
|
|
|
6
6
|
class ZuiYouParser(BaseParser):
|
|
7
7
|
__platform__ = Platform.ZUIYOU
|
|
8
8
|
__supported_type__ = ["视频", "图文"]
|
|
9
|
-
__match__ = r"^(http(s)?://)share.xiaochuankeji.cn/hybrid/share/post\?pid=\d+"
|
|
9
|
+
__match__ = r"^(http(s)?://)(share.xiaochuankeji.cn/hybrid/share/post\?pid=\d+|s.izuiyou.com/s/\w+)"
|
|
10
|
+
__redirect_keywords__ = ["s.izuiyou.com/s/"]
|
|
10
11
|
__reserved_parameters__ = ["pid"]
|
|
11
12
|
|
|
12
13
|
async def _do_parse(self, raw_url: str) -> MultimediaParseResult:
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from enum import StrEnum
|
|
7
|
+
from typing import Any
|
|
8
|
+
from urllib.parse import urlparse
|
|
9
|
+
|
|
10
|
+
from curl_cffi import requests
|
|
11
|
+
|
|
12
|
+
from ..types import ContentUnavailableError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class InstagramAPIError(RuntimeError):
|
|
16
|
+
"""Instagram 接口请求或响应解析失败。"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class InstagramRestrictedError(InstagramAPIError):
|
|
20
|
+
"""Instagram 内容受限。"""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class InstagramMediaType(StrEnum):
|
|
24
|
+
IMAGE = "GraphImage"
|
|
25
|
+
VIDEO = "GraphVideo"
|
|
26
|
+
SIDECAR = "GraphSidecar"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(slots=True, kw_only=True)
|
|
30
|
+
class InstagramMedia:
|
|
31
|
+
is_video: bool
|
|
32
|
+
display_url: str
|
|
33
|
+
video_url: str = ""
|
|
34
|
+
width: int = 0
|
|
35
|
+
height: int = 0
|
|
36
|
+
duration: float = 0
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(slots=True, kw_only=True)
|
|
40
|
+
class InstagramPost:
|
|
41
|
+
shortcode: str
|
|
42
|
+
type: InstagramMediaType
|
|
43
|
+
caption: str = ""
|
|
44
|
+
title: str = ""
|
|
45
|
+
media: list[InstagramMedia]
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def from_v1(cls, item: dict[str, Any]) -> InstagramPost:
|
|
49
|
+
media_type = _v1_type(item)
|
|
50
|
+
if media_type is InstagramMediaType.SIDECAR:
|
|
51
|
+
media_items = item.get("carousel_media") or []
|
|
52
|
+
if not media_items:
|
|
53
|
+
raise InstagramAPIError("未返回帖子数据")
|
|
54
|
+
else:
|
|
55
|
+
media_items = [item]
|
|
56
|
+
if not all(isinstance(media_item, dict) for media_item in media_items):
|
|
57
|
+
raise InstagramAPIError("Instagram 帖子数据格式无效")
|
|
58
|
+
return cls(
|
|
59
|
+
shortcode=str(item.get("code") or item.get("shortcode") or ""),
|
|
60
|
+
type=media_type,
|
|
61
|
+
caption=_caption_text(item.get("caption")),
|
|
62
|
+
title=str(item.get("title") or ""),
|
|
63
|
+
media=[_v1_media(media_item) for media_item in media_items],
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def from_graph(cls, node: dict[str, Any]) -> InstagramPost:
|
|
68
|
+
media_type = _graph_type(node)
|
|
69
|
+
if media_type is InstagramMediaType.SIDECAR:
|
|
70
|
+
edges = (node.get("edge_sidecar_to_children") or {}).get("edges") or []
|
|
71
|
+
if not edges:
|
|
72
|
+
raise InstagramAPIError("未返回帖子数据")
|
|
73
|
+
media = [_graph_media(edge.get("node") or {}) for edge in edges]
|
|
74
|
+
else:
|
|
75
|
+
media = [_graph_media(node)]
|
|
76
|
+
|
|
77
|
+
caption_edges = (node.get("edge_media_to_caption") or {}).get("edges") or []
|
|
78
|
+
caption = caption_edges[0].get("node", {}).get("text", "") if caption_edges else ""
|
|
79
|
+
return cls(
|
|
80
|
+
shortcode=str(node.get("shortcode") or node.get("code") or ""),
|
|
81
|
+
type=media_type,
|
|
82
|
+
caption=str(caption or node.get("caption") or ""),
|
|
83
|
+
title=str(node.get("title") or ""),
|
|
84
|
+
media=media,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class InstagramAPI:
|
|
89
|
+
HOME_URL = "https://www.instagram.com/"
|
|
90
|
+
GRAPHQL_URL = "https://www.instagram.com/api/graphql"
|
|
91
|
+
EMBED_URL = "https://www.instagram.com/{kind}/{shortcode}/embed/captioned/"
|
|
92
|
+
MEDIA_INFO_URL = "https://www.instagram.com/api/v1/media/{media_id}/info/"
|
|
93
|
+
RULING_URL = "https://www.instagram.com/api/v1/web/get_ruling_for_content/"
|
|
94
|
+
APP_ID = "936619743392459"
|
|
95
|
+
ASBD_ID = "359341"
|
|
96
|
+
DOC_ID = "27130156389949648"
|
|
97
|
+
MOBILE_USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15"
|
|
98
|
+
|
|
99
|
+
def __init__(
|
|
100
|
+
self,
|
|
101
|
+
*,
|
|
102
|
+
proxy: str | None = None,
|
|
103
|
+
cookie: dict[str, str] | None = None,
|
|
104
|
+
timeout: float = 30,
|
|
105
|
+
):
|
|
106
|
+
self.proxy = proxy
|
|
107
|
+
self.cookie = cookie or {}
|
|
108
|
+
self.timeout = timeout
|
|
109
|
+
|
|
110
|
+
async def get_post(self, shortcode: str) -> InstagramPost:
|
|
111
|
+
try:
|
|
112
|
+
async with requests.AsyncSession(
|
|
113
|
+
impersonate="chrome",
|
|
114
|
+
proxy=self.proxy,
|
|
115
|
+
timeout=self.timeout,
|
|
116
|
+
cookies=self.cookie or None,
|
|
117
|
+
) as session:
|
|
118
|
+
if self.cookie:
|
|
119
|
+
return await self._get_cookie_post(session, shortcode)
|
|
120
|
+
return await self._get_logged_out_post(session, shortcode)
|
|
121
|
+
except requests.RequestsError as exc:
|
|
122
|
+
raise InstagramAPIError(f"请求 Instagram 失败: {exc}") from exc
|
|
123
|
+
|
|
124
|
+
async def _get_logged_out_post(self, session: requests.AsyncSession, shortcode: str) -> InstagramPost:
|
|
125
|
+
detail = "未返回帖子数据"
|
|
126
|
+
media_id = str(self.shortcode_to_media_id(shortcode))
|
|
127
|
+
|
|
128
|
+
try:
|
|
129
|
+
item = await self._fetch_graphql(session, shortcode, media_id)
|
|
130
|
+
if item is not None:
|
|
131
|
+
return InstagramPost.from_v1(item)
|
|
132
|
+
except InstagramAPIError as exc:
|
|
133
|
+
detail = str(exc)
|
|
134
|
+
except requests.RequestsError as exc:
|
|
135
|
+
detail = f"请求 Instagram 失败: {exc}"
|
|
136
|
+
|
|
137
|
+
try:
|
|
138
|
+
node = await self._fetch_embed(session, shortcode)
|
|
139
|
+
if node is not None:
|
|
140
|
+
return InstagramPost.from_graph(node)
|
|
141
|
+
except InstagramAPIError as exc:
|
|
142
|
+
detail = str(exc)
|
|
143
|
+
except requests.RequestsError as exc:
|
|
144
|
+
detail = f"请求 Instagram Embed 失败: {exc}"
|
|
145
|
+
|
|
146
|
+
await self._check_ruling(session, media_id)
|
|
147
|
+
raise InstagramAPIError(detail)
|
|
148
|
+
|
|
149
|
+
async def _fetch_graphql(
|
|
150
|
+
self, session: requests.AsyncSession, shortcode: str, media_id: str
|
|
151
|
+
) -> dict[str, Any] | None:
|
|
152
|
+
response = await session.get(self.HOME_URL)
|
|
153
|
+
if response.status_code != 200:
|
|
154
|
+
raise InstagramAPIError(f"HTTP {response.status_code}: {response.text[:300]}")
|
|
155
|
+
|
|
156
|
+
match = re.search(r'\["LSD",\[\],\{"token":"([^"]+)"', response.text)
|
|
157
|
+
if not match:
|
|
158
|
+
raise InstagramAPIError(f"未能获取 Instagram LSD token: {response.text[:300]}")
|
|
159
|
+
lsd = match.group(1)
|
|
160
|
+
csrf = session.cookies.get("csrftoken", "")
|
|
161
|
+
headers = {
|
|
162
|
+
"X-IG-App-ID": self.APP_ID,
|
|
163
|
+
"X-ASBD-ID": self.ASBD_ID,
|
|
164
|
+
"X-IG-WWW-Claim": "0",
|
|
165
|
+
"Origin": self.HOME_URL.rstrip("/"),
|
|
166
|
+
"Accept": "*/*",
|
|
167
|
+
"X-FB-Friendly-Name": "PolarisLoggedOutDesktopWWWPostRootContentQuery",
|
|
168
|
+
"X-CSRFToken": csrf,
|
|
169
|
+
"X-FB-LSD": lsd,
|
|
170
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
171
|
+
"Referer": f"https://www.instagram.com/p/{shortcode}/",
|
|
172
|
+
}
|
|
173
|
+
data = {
|
|
174
|
+
"lsd": lsd,
|
|
175
|
+
"fb_api_caller_class": "RelayModern",
|
|
176
|
+
"fb_api_req_friendly_name": "PolarisLoggedOutDesktopWWWPostRootContentQuery",
|
|
177
|
+
"server_timestamps": "true",
|
|
178
|
+
"variables": json.dumps({"media_id": media_id}, separators=(",", ":")),
|
|
179
|
+
"doc_id": self.DOC_ID,
|
|
180
|
+
}
|
|
181
|
+
response = await session.post(self.GRAPHQL_URL, headers=headers, data=data)
|
|
182
|
+
if response.status_code != 200:
|
|
183
|
+
raise InstagramAPIError(f"HTTP {response.status_code}: {response.text[:300]}")
|
|
184
|
+
payload = _response_json(response)
|
|
185
|
+
graphql_data = payload.get("data")
|
|
186
|
+
media = graphql_data.get("xig_polaris_media") if isinstance(graphql_data, dict) else None
|
|
187
|
+
item = media.get("if_not_gated_logged_out") if isinstance(media, dict) else None
|
|
188
|
+
if isinstance(item, dict):
|
|
189
|
+
return item
|
|
190
|
+
errors = payload.get("errors")
|
|
191
|
+
if errors:
|
|
192
|
+
raise InstagramAPIError(str(errors[0])[:300])
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
async def _fetch_embed(self, session: requests.AsyncSession, shortcode: str) -> dict[str, Any] | None:
|
|
196
|
+
for kind in ("p", "reel"):
|
|
197
|
+
response = await session.get(
|
|
198
|
+
self.EMBED_URL.format(kind=kind, shortcode=shortcode),
|
|
199
|
+
headers={"Accept": "text/html", "User-Agent": self.MOBILE_USER_AGENT},
|
|
200
|
+
allow_redirects=False,
|
|
201
|
+
)
|
|
202
|
+
if response.status_code != 200:
|
|
203
|
+
continue
|
|
204
|
+
node = self._extract_embed_media(response.text)
|
|
205
|
+
if node is not None:
|
|
206
|
+
return node
|
|
207
|
+
return None
|
|
208
|
+
|
|
209
|
+
@staticmethod
|
|
210
|
+
def _extract_embed_media(html: str) -> dict[str, Any] | None:
|
|
211
|
+
match = re.search(r'"init",\[\],\[(.*?)\]\],', html, re.DOTALL)
|
|
212
|
+
if not match:
|
|
213
|
+
return None
|
|
214
|
+
try:
|
|
215
|
+
payload = json.loads(f"[{match.group(1)}]")
|
|
216
|
+
context = payload[0].get("contextJSON")
|
|
217
|
+
context = json.loads(context) if isinstance(context, str) else context
|
|
218
|
+
except (IndexError, AttributeError, json.JSONDecodeError, TypeError):
|
|
219
|
+
return None
|
|
220
|
+
if not isinstance(context, dict):
|
|
221
|
+
return None
|
|
222
|
+
node = (context.get("gql_data") or {}).get("shortcode_media")
|
|
223
|
+
return node if isinstance(node, dict) else None
|
|
224
|
+
|
|
225
|
+
async def _check_ruling(self, session: requests.AsyncSession, media_id: str) -> None:
|
|
226
|
+
try:
|
|
227
|
+
response = await session.get(
|
|
228
|
+
self.RULING_URL,
|
|
229
|
+
params={"content_type": "MEDIA", "target_id": media_id},
|
|
230
|
+
headers={"X-IG-App-ID": self.APP_ID, "Accept-Language": "zh-CN,zh;q=0.9"},
|
|
231
|
+
)
|
|
232
|
+
body = _response_json(response)
|
|
233
|
+
except (InstagramAPIError, requests.RequestsError):
|
|
234
|
+
return
|
|
235
|
+
|
|
236
|
+
if body.get("message") == "Media cannot be found":
|
|
237
|
+
raise ContentUnavailableError("帖子不存在或已被删除")
|
|
238
|
+
if title := body.get("title"):
|
|
239
|
+
description = body.get("description")
|
|
240
|
+
raise InstagramRestrictedError(f"{title}: {description}" if description else str(title))
|
|
241
|
+
|
|
242
|
+
async def _get_cookie_post(self, session: requests.AsyncSession, shortcode: str) -> InstagramPost:
|
|
243
|
+
media_id = self.shortcode_to_media_id(shortcode)
|
|
244
|
+
response = await session.get(
|
|
245
|
+
self.MEDIA_INFO_URL.format(media_id=media_id),
|
|
246
|
+
headers={
|
|
247
|
+
"X-IG-App-ID": self.APP_ID,
|
|
248
|
+
"X-ASBD-ID": self.ASBD_ID,
|
|
249
|
+
"X-IG-WWW-Claim": "0",
|
|
250
|
+
"X-CSRFToken": self.cookie.get("csrftoken", ""),
|
|
251
|
+
"Origin": self.HOME_URL.rstrip("/"),
|
|
252
|
+
"Referer": f"https://www.instagram.com/p/{shortcode}/",
|
|
253
|
+
},
|
|
254
|
+
allow_redirects=False,
|
|
255
|
+
)
|
|
256
|
+
location = response.headers.get("Location", "")
|
|
257
|
+
redirect_path = urlparse(location).path
|
|
258
|
+
if response.status_code in (301, 302, 303, 307, 308) and (
|
|
259
|
+
redirect_path.startswith("/accounts/login") or redirect_path == "/"
|
|
260
|
+
):
|
|
261
|
+
raise InstagramAPIError("Cookie 已失效")
|
|
262
|
+
if response.status_code != 200:
|
|
263
|
+
raise InstagramAPIError(f"HTTP {response.status_code}: {response.text[:300]}")
|
|
264
|
+
|
|
265
|
+
payload = _response_json(response)
|
|
266
|
+
items = payload.get("items") or []
|
|
267
|
+
if not items or not isinstance(items[0], dict):
|
|
268
|
+
raise InstagramAPIError("未返回帖子数据")
|
|
269
|
+
return InstagramPost.from_v1(items[0])
|
|
270
|
+
|
|
271
|
+
@staticmethod
|
|
272
|
+
def shortcode_to_media_id(shortcode: str) -> int:
|
|
273
|
+
if len(shortcode) > 28:
|
|
274
|
+
shortcode = shortcode[:-28]
|
|
275
|
+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
276
|
+
media_id = 0
|
|
277
|
+
for char in shortcode:
|
|
278
|
+
media_id = media_id * 64 + alphabet.index(char)
|
|
279
|
+
return media_id
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _response_json(response: Any) -> dict[str, Any]:
|
|
283
|
+
try:
|
|
284
|
+
payload = response.json()
|
|
285
|
+
except (ValueError, requests.RequestsError) as exc:
|
|
286
|
+
raise InstagramAPIError(f"Instagram 返回无效 JSON: {response.text[:300]}") from exc
|
|
287
|
+
if not isinstance(payload, dict):
|
|
288
|
+
raise InstagramAPIError("Instagram 返回的数据格式无效")
|
|
289
|
+
return payload
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _caption_text(caption: Any) -> str:
|
|
293
|
+
if isinstance(caption, dict):
|
|
294
|
+
caption = caption.get("text")
|
|
295
|
+
return str(caption or "")
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _v1_type(item: dict[str, Any]) -> InstagramMediaType:
|
|
299
|
+
media_types = {
|
|
300
|
+
1: InstagramMediaType.IMAGE,
|
|
301
|
+
2: InstagramMediaType.VIDEO,
|
|
302
|
+
8: InstagramMediaType.SIDECAR,
|
|
303
|
+
}
|
|
304
|
+
media_type = item.get("media_type")
|
|
305
|
+
if isinstance(media_type, int) and media_type in media_types:
|
|
306
|
+
return media_types[media_type]
|
|
307
|
+
raise InstagramAPIError(f"不支持的 Instagram 媒体类型: {media_type}")
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _v1_media(item: dict[str, Any]) -> InstagramMedia:
|
|
311
|
+
candidates = (item.get("image_versions2") or {}).get("candidates") or []
|
|
312
|
+
candidate = candidates[0] if candidates and isinstance(candidates[0], dict) else {}
|
|
313
|
+
video_versions = item.get("video_versions") or []
|
|
314
|
+
video = video_versions[0] if video_versions and isinstance(video_versions[0], dict) else {}
|
|
315
|
+
video_url = str(video.get("url") or "")
|
|
316
|
+
return InstagramMedia(
|
|
317
|
+
is_video=bool(video_url),
|
|
318
|
+
display_url=str(candidate.get("url") or item.get("display_url") or ""),
|
|
319
|
+
video_url=video_url,
|
|
320
|
+
width=int(item.get("original_width") or candidate.get("width") or 0),
|
|
321
|
+
height=int(item.get("original_height") or candidate.get("height") or 0),
|
|
322
|
+
duration=float(item.get("video_duration") or 0),
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _graph_type(node: dict[str, Any]) -> InstagramMediaType:
|
|
327
|
+
typename = node.get("__typename")
|
|
328
|
+
if not isinstance(typename, str):
|
|
329
|
+
raise InstagramAPIError(f"不支持的 Instagram 媒体类型: {typename}")
|
|
330
|
+
try:
|
|
331
|
+
return InstagramMediaType(typename)
|
|
332
|
+
except ValueError as exc:
|
|
333
|
+
raise InstagramAPIError(f"不支持的 Instagram 媒体类型: {typename}") from exc
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _graph_media(node: dict[str, Any]) -> InstagramMedia:
|
|
337
|
+
video_url = str(node.get("video_url") or "")
|
|
338
|
+
dimensions = node.get("dimensions") or {}
|
|
339
|
+
return InstagramMedia(
|
|
340
|
+
is_video=bool(video_url),
|
|
341
|
+
display_url=str(node.get("display_url") or node.get("display_src") or ""),
|
|
342
|
+
video_url=video_url,
|
|
343
|
+
width=int(dimensions.get("width") or 0),
|
|
344
|
+
height=int(dimensions.get("height") or 0),
|
|
345
|
+
duration=float(node.get("video_duration") or 0),
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
__all__ = [
|
|
350
|
+
"InstagramAPI",
|
|
351
|
+
"InstagramAPIError",
|
|
352
|
+
"InstagramMedia",
|
|
353
|
+
"InstagramMediaType",
|
|
354
|
+
"InstagramPost",
|
|
355
|
+
"InstagramRestrictedError",
|
|
356
|
+
]
|
|
@@ -8,6 +8,7 @@ import re
|
|
|
8
8
|
from dataclasses import dataclass
|
|
9
9
|
from enum import Enum
|
|
10
10
|
from typing import Any, cast
|
|
11
|
+
from urllib.parse import urlsplit
|
|
11
12
|
|
|
12
13
|
import httpx
|
|
13
14
|
from bs4 import BeautifulSoup
|
|
@@ -87,6 +88,7 @@ class XHSAPI:
|
|
|
87
88
|
script = scripts[0].text
|
|
88
89
|
json_data = script.replace("window.__INITIAL_STATE__=", "")
|
|
89
90
|
json_data = re.sub(r"\bundefined\b", "null", json_data) # 清理js对象
|
|
91
|
+
json_data = re.sub(r"new Set\((\[[^\]]*\])\)", r"\1", json_data)
|
|
90
92
|
data = json.loads(json_data)
|
|
91
93
|
return cast(dict[str, Any], data)
|
|
92
94
|
|
|
@@ -192,18 +194,13 @@ class XHSAPI:
|
|
|
192
194
|
|
|
193
195
|
@staticmethod
|
|
194
196
|
def get_trace_id(img_url: str) -> str:
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
match = re.search(r"/([^/!]+)(?:!.*)?$", img_url)
|
|
200
|
-
if match:
|
|
201
|
-
return match.group(1)
|
|
202
|
-
return img_url
|
|
197
|
+
path = urlsplit(img_url).path.split("!", 1)[0]
|
|
198
|
+
match = re.search(r"/[a-f0-9]{32}/(.+)", path)
|
|
199
|
+
return match.group(1) if match else path.lstrip("/")
|
|
203
200
|
|
|
204
201
|
def get_raw_image_url(self, ime_url: str) -> str:
|
|
205
202
|
"""拼接无水印图片链接"""
|
|
206
|
-
return f"
|
|
203
|
+
return f"https://sns-img-hw.xhscdn.com/{self.get_trace_id(ime_url)}?imageView2/format/jpg"
|
|
207
204
|
|
|
208
205
|
|
|
209
206
|
class XHSMediaType(Enum):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: parsehub
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.16
|
|
4
4
|
Summary: 轻量、异步、开箱即用的社交媒体聚合解析库
|
|
5
5
|
Author-email: 梓澪 <zilingmio@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -22,7 +22,7 @@ Requires-Dist: pydub>=0.25.1
|
|
|
22
22
|
Requires-Dist: python-dotenv>=1.0.1
|
|
23
23
|
Requires-Dist: tenacity>=8.5.0
|
|
24
24
|
Requires-Dist: urlextract>=1.9.0
|
|
25
|
-
Requires-Dist: yt-dlp[default]>=2026.10.
|
|
25
|
+
Requires-Dist: yt-dlp[default]>=2026.10.26.220202.dev0
|
|
26
26
|
Requires-Dist: lxml>=5.3.0
|
|
27
27
|
Requires-Dist: pydantic>=1.10.19
|
|
28
28
|
Requires-Dist: markdownify>=1.1.0
|
|
@@ -640,8 +640,7 @@ class TestPlatformUrlMatching(unittest.TestCase):
|
|
|
640
640
|
],
|
|
641
641
|
Platform.XIAOHEIHE: [
|
|
642
642
|
"https://www.xiaoheihe.cn/app/bbs/link/174972336",
|
|
643
|
-
"https://
|
|
644
|
-
"https://api.xiaoheihe.cn/v3/bbs/app/link?link_id=174972336",
|
|
643
|
+
"https://api.xiaoheihe.cn/v3/bbs/app/api/web/share?h_camp=link&h_src=YXBwX3NoYXJl&link_id=57b172affdbc",
|
|
645
644
|
],
|
|
646
645
|
Platform.YOUTUBE: [
|
|
647
646
|
"https://www.youtube.com/watch?v=1h_uc3K4Cpg",
|