Commit 8c57e09

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-02-26 18:00:16
fix: more flexible comments provider settings
1 parent da95f78
Changed files (4)
src/preview/douyin.py
@@ -42,8 +42,8 @@ async def preview_douyin(
         url (str, optional): The douyin or tiktok link.
         db_key (str, optional): The cache key.
         platform(str, optional): The platform name. Defaults to "douyin".
-        douyin_provider (str, optional): The douyin extractor: "free" or "tikhub".
-        douyin_comments_provider (str, optional): The douyin comments extractor: "free" or "tikhub".
+        douyin_provider (str, optional): The douyin extractor: "free", "tikhub" or "free-tikhub".
+        douyin_comments_provider (str, optional): The douyin comments extractor: "free", "tikhub" or "free-tikhub".
         fallback (bool, optional): Fallback to other bots. Defaults to True.
     """
     if kwargs.get("show_progress") and "progress" not in kwargs:
@@ -58,16 +58,16 @@ async def preview_douyin(
     logger.info(f"{platform} link preview for {url}")
     succ = False
     data = {}
-    if douyin_provider == "free":  # try free first
+    if "free" in douyin_provider:  # try free first
         api_url = f"{API.TIKHUB_FREE}/api/hybrid/video_data?url={url}"
-        headers = {"accept": "application/json"} if douyin_provider == "tikhub" else {}
+        headers = {"accept": "application/json"}
         try:
             resp = await hx_req(api_url, headers=headers, check_keys=["data"], check_kv={"code": 200}, max_retry=0, timeout=3)
             data = resp["data"]
             succ = True
         except Exception:
             logger.warning(f"{platform} API [free] failed")
-    if not succ:  # try tikhub
+    if not succ and "tikhub" in douyin_provider:  # try tikhub
         api_url = f"{API.TIKHUB}/api/v1/hybrid/video_data?url={url}"
         headers = {"authorization": f"Bearer {TOKEN.TIKHUB}", "accept": "application/json"}
         try:
@@ -133,7 +133,7 @@ async def get_comments(aweme_id: str = "", platform: str = "douyin", douyin_comm
     }
     succ = False
     data = []
-    if douyin_comments_provider == "free":  # try free first
+    if "free" in douyin_comments_provider:  # try free first
         api_url = api_urls.get(f"{platform}_free")
         headers = {"accept": "application/json"}
         try:
@@ -142,7 +142,7 @@ async def get_comments(aweme_id: str = "", platform: str = "douyin", douyin_comm
             succ = True
         except Exception:
             logger.warning(f"{platform} comments API [free] failed")
-    if not succ:  # try tikhub
+    if not succ and "tikhub" in douyin_comments_provider:  # try tikhub
         api_url = api_urls.get(f"{platform}_tikhub")
         headers = {"authorization": f"Bearer {TOKEN.TIKHUB}", "accept": "application/json"}
         try:
src/preview/twitter.py
@@ -42,7 +42,7 @@ async def preview_twitter(
         url (str, optional): The twitter link.
         db_key (str, optional): The cache key.
         platform (str): The domain of the link: twitter, x, fxtwitter, fixupx
-        twitter_provider (str): The extractor to use: fxtwitter or tikhub. Defaults to "tikhub".
+        twitter_provider (str): The extractor to use: fxtwitter or tikhub.
         twitter_comments_provider (str, optional): The twitter comments extractor: "tikhub" or "false".
         fallback (bool, optional): Fallback to other bots. Defaults to True.
 
@@ -64,7 +64,7 @@ async def preview_twitter(
     master_info = {}
     this_info = {}
     quote_info = {}
-    if twitter_provider == "tikhub":  # try tikhub first
+    if "tikhub" in twitter_provider:  # try tikhub first
         try:
             this_info = await get_tweet_info_via_tikhub(url=url, **kwargs)
             if not this_info:
@@ -79,7 +79,7 @@ async def preview_twitter(
         except Exception as e:
             logger.warning(f"Twitter API [tikhub] failed: {e}")
 
-    if not succ:  # try fxtwitter
+    if not succ and "fxtwitter" in twitter_provider:  # try fxtwitter
         try:
             this_info = await get_tweet_info_via_fxtwitter(url=url)
             if not this_info:
src/preview/ytdlp.py
@@ -460,7 +460,7 @@ async def get_bilibili_comments(bvid: str | None, provider: str = PROVIDER.BILIB
 
     succ = False
     data = []
-    if provider == "free":
+    if "free" in provider:
         try:
             api = f"{API.TIKHUB_FREE}/api/bilibili/web/fetch_video_comments?bv_id={bvid}"
             headers = {"accept": "application/json"}
@@ -469,7 +469,7 @@ async def get_bilibili_comments(bvid: str | None, provider: str = PROVIDER.BILIB
             succ = True
         except Exception:
             logger.warning("Bilibili comments API [free] failed")
-    if not succ:  # try tikhub
+    if not succ and "tikhub" in provider:  # try tikhub
         api_url = f"{API.TIKHUB}/api/v1/bilibili/web/fetch_video_comments?bv_id={bvid}"
         headers = {"authorization": f"Bearer {TOKEN.TIKHUB}", "accept": "application/json"}
         try:
src/config.py
@@ -90,13 +90,13 @@ class API:
 
 
 class PROVIDER:  # default API provider
-    DOUYIN = os.getenv("DOUYIN_PROVIDER", "free").lower()  # free or tikhub
-    DOUYIN_COMMENTS = os.getenv("DOUYIN_COMMENTS_PROVIDER", "free").lower()  # free or tikhub or a false value (0, false, none, null, etc.)
-    TWITTER = os.getenv("TWITTER_PROVIDER", "tikhub").lower()  # tikhub or fxtwitter
+    DOUYIN = os.getenv("DOUYIN_PROVIDER", "free-tikhub").lower()  # free or tikhub
+    DOUYIN_COMMENTS = os.getenv("DOUYIN_COMMENTS_PROVIDER", "free-tikhub").lower()  # free or tikhub or a false value (0, false, none, null, etc.)
+    TWITTER = os.getenv("TWITTER_PROVIDER", "tikhub-fxtwitter").lower()  # tikhub or fxtwitter
     TWITTER_COMMENTS = os.getenv("TWITTER_COMMENTS_PROVIDER", "tikhub").lower()  # tikhub or a false value (0, false, none, null, etc.)
     INSTAGRAM_COMMENTS = os.getenv("INSTAGRAM_COMMENTS_PROVIDER", "tikhub").lower()  # tikhub or a false value (0, false, none, null, etc.)
     WEIBO_COMMENTS = os.getenv("WEIBO_COMMENTS_PROVIDER", "free").lower()  # free or a false value (0, false, none, null, etc.)
-    BILIBILI_COMMENTS = os.getenv("BILIBILI_COMMENTS_PROVIDER", "free").lower()  # free or tikhub or a false value (0, false, none, null, etc.)
+    BILIBILI_COMMENTS = os.getenv("BILIBILI_COMMENTS_PROVIDER", "free-tikhub").lower()  # free or tikhub or a false value (0, false, none, null, etc.)
     YOUTUBE_COMMENTS = os.getenv("YOUTUBE_COMMENTS_PROVIDER", "free").lower()  # free or a false value (0, false, none, null, etc.)