Skip to content

Commit 7b02227

Browse files
committed
修复已知问题
1 parent 06ba396 commit 7b02227

File tree

10 files changed

+76
-39
lines changed

10 files changed

+76
-39
lines changed

docs/TikTokDownloader文档.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ https://www.douyin.com/note/123456789
376376
<td align="center">每次从服务器接收的数据块大小, 单位字节; 默认值:<code>2621440</code>(2.5 MB)</td>
377377
</tr>
378378
<tr>
379+
<td align="center">timeout</td>
380+
<td align="center">int</td>
381+
<td align="center">请求数据的超时限制, 单位秒; 默认值:<code>10</code></td>
382+
</tr>
383+
<tr>
379384
<td align="center">max_retry</td>
380385
<td align="center">int</td>
381386
<td align="center">发送请求获取数据发生异常时重试的最大次数<br>设置为 <code>0</code> 代表关闭重试, 默认值: <code>10</code></td>
@@ -483,6 +488,7 @@ https://www.douyin.com/note/123456789
483488
"download": true,
484489
"max_size": 104857600,
485490
"chunk": 10485760,
491+
"timeout": 5,
486492
"max_retry": 10,
487493
"max_pages": 2,
488494
"default_mode": "6 2 1",

src/application/main_complete.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__(self, parameter: "Parameter", database: "Database", ):
162162
("批量下载账号作品(抖音)", self.account_acquisition_interactive,),
163163
("批量下载链接作品(抖音)", self.detail_interactive,),
164164
("获取直播推流地址(抖音)", self.live_interactive,),
165-
("采集作品评论数据(抖音)", self.comment_interactive,),
165+
# ("采集作品评论数据(抖音)", self.comment_interactive,),
166166
("批量下载合集作品(抖音)", self.mix_interactive,),
167167
# ("采集账号详细数据(抖音)", self.disable_function,),
168168
# ("采集搜索结果数据(抖音)", self.disable_function,),
@@ -486,17 +486,20 @@ async def _batch_process_detail(self,
486486
addition: str = None,
487487
tiktok=False,
488488
title: str = None,
489+
collect_id: str = None,
489490
):
490491
self.logger.info("开始提取作品数据")
491492
id_, name, mid, title, mark, data = self.extractor.preprocessing_data(
492-
data, id_, mark, post, mix, tiktok, title, )
493+
data, id_, mark, post, mix, tiktok, title, collect_id=collect_id, )
493494
self.__display_extracted_information(
494495
mix, id_, name, mid, title, mark, )
495496
addition = addition or ("合集作品" if mix else "发布作品" if post else "喜欢作品")
496497
old_mark = f"{m["MARK"]}_{addition}" if (
497498
m := await self.cache.has_cache(
498499
mid if mix else id_)) else None
499-
async with logger(root, name=f"{'MID' if mix else 'UID'}{mid if mix else id_}_{mark}_{addition}", old=old_mark,
500+
async with logger(root,
501+
name=f"{self._generate_prefix(mix=mix, collect=bool(collect_id))}{self._generate_id(id_, mid, mix=mix, collect=bool(collect_id))}_{mark}_{addition}",
502+
old=old_mark,
500503
console=self.console, **params) as recorder:
501504
data = await self.extractor.run(
502505
data,
@@ -512,16 +515,35 @@ async def _batch_process_detail(self,
512515
return data
513516
await self.cache.update_cache(
514517
self.parameter.folder_mode,
515-
"MID" if mix else "UID",
516-
mid if mix else id_,
518+
self._generate_prefix(mix=mix, collect=bool(collect_id)),
519+
self._generate_id(id_, mid, mix=mix, collect=bool(collect_id)),
517520
mark,
518521
title if mix else name,
519522
addition,
520523
)
521524
await self.download_account_detail(
522-
data, id_, name, mark, mid, title, addition, tiktok, )
525+
data, id_, name, mark, self._generate_id(id_, mid, mix=mix, collect=bool(collect_id)), title, addition,
526+
tiktok, collect=bool(collect_id), )
523527
return True
524528

529+
@staticmethod
530+
def _generate_prefix(user=True, mix=False, collect=False):
531+
if collect:
532+
return "CID"
533+
if mix:
534+
return "MID"
535+
if user:
536+
return "UID"
537+
538+
@staticmethod
539+
def _generate_id(id_, mid, user=True, mix=False, collect=False):
540+
if collect:
541+
return mid
542+
if mix:
543+
return mid
544+
if user:
545+
return id_
546+
525547
def __display_extracted_information(
526548
self,
527549
mix: bool,
@@ -544,6 +566,7 @@ async def download_account_detail(
544566
title: str = None,
545567
addition: str = None,
546568
tiktok: bool = False,
569+
collect=False,
547570
):
548571
await self.downloader.run(
549572
data,
@@ -555,6 +578,7 @@ async def download_account_detail(
555578
addition=addition,
556579
mid=mid,
557580
title=title,
581+
collect=collect,
558582
)
559583

560584
async def detail_interactive(self, select="", *args, **kwargs):
@@ -906,7 +930,8 @@ async def mix_inquire_collection(self) -> list[str]:
906930
return self.input_download_index(data)
907931

908932
def input_download_index(self, data: list[dict]) -> list[str]:
909-
return self.__input_download_index(data)[-1]
933+
_, id_ = self.__input_download_index(data)
934+
return id_
910935

911936
def __input_download_index(self,
912937
data: list[dict],
@@ -917,15 +942,14 @@ def __input_download_index(self,
917942
self.console.print(f"{text}列表:")
918943
for i, j in enumerate(data, start=1):
919944
self.console.print(f"{i}. {j[key]}")
920-
index = self.console.input(f"请输入需要下载的{text}序号(输入 ALL 下载全部{text}):")
945+
index = self.console.input(f"请输入需要下载的{text}序号(多个序号使用空格分隔,输入 ALL 下载全部{text}):")
921946
try:
922947
if index.upper() == "ALL":
923948
return zip(*[(d[key], d["id"]) for d in data])
924-
if 0 <= (i := int(index) - 1) <= len(data):
925-
return [data[i][key]], [data[i]["id"]]
926-
self.console.print(f"{text}序号输入错误!", style=WARNING)
927-
raise ValueError
949+
index = {int(i) for i in index.split()}
950+
return zip(*[(d[key], d["id"]) for i, d in enumerate(data, start=1) if i in index])
928951
except ValueError:
952+
self.console.print(f"{text}序号输入错误!", style=WARNING)
929953
return []
930954

931955
async def mix_txt(self, root, params, logger):
@@ -1331,7 +1355,10 @@ async def collection_interactive(self, *args, **kwargs):
13311355
@check_cookie_state(tiktok=False)
13321356
async def collects_interactive(self, *args, **kwargs):
13331357
if sec_user_id := await self.__check_owner_url():
1334-
names, ids = await self.__get_collects_list()
1358+
try:
1359+
names, ids = await self.__get_collects_list()
1360+
except ValueError:
1361+
names, ids = [], []
13351362
root, params, logger = self.record.run(self.parameter)
13361363
start = time()
13371364
for i, j in zip(names, ids):
@@ -1465,6 +1492,7 @@ async def _deal_collects_data(
14651492
addition="收藏夹作品",
14661493
tiktok=tiktok,
14671494
title=name,
1495+
collect_id=id_,
14681496
)
14691497

14701498
async def hashtag_interactive(

src/config/parameter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ def __check_chunk(self, chunk: int) -> int:
383383
self.logger.info(f"chunk 参数已设置为 {chunk}", False)
384384
return chunk
385385
self.logger.warning(
386-
f"chunk 参数 {chunk} 设置错误,程序将使用默认值:{1024 * 1024}", )
387-
return 1024 * 1024
386+
f"chunk 参数 {chunk} 设置错误,程序将使用默认值:{int(1024 * 1024 * 2.5)}", )
387+
return int(1024 * 1024 * 2.5)
388388

389389
def __check_max_retry(self, max_retry: int) -> int:
390390
if isinstance(max_retry, int) and max_retry >= 0:

src/config/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class Settings:
8787
"twc_tiktok": "",
8888
"download": True,
8989
"max_size": 0,
90-
"chunk": 1024 * 1024, # 每次从服务器接收的数据块大小
90+
"chunk": int(1024 * 1024 * 2.5), # 每次从服务器接收的数据块大小
91+
"timeout": 10,
9192
"max_retry": 5, # 重试最大次数
9293
"max_pages": 0,
9394
"default_mode": "",

src/downloader/download.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,18 @@ async def run_batch(
136136
addition="发布作品",
137137
mid: str = None,
138138
title: str = None,
139+
collect=False,
139140
):
140141
# assert addition in {"喜欢作品", "收藏作品", "发布作品", "合集作品"}, ValueError
141142
mix = addition == "合集作品"
142143
root = self.storage_folder(
143-
mid if mix else id_,
144+
mid if (mix or collect) else id_,
144145
title if mix else name,
145146
not mix,
146147
mark,
147148
addition,
148-
mix)
149+
mix,
150+
collect=collect)
149151
await self.batch_processing(data, root, tiktok=tiktok, )
150152

151153
async def run_general(self, data: list[dict], tiktok: bool):
@@ -547,11 +549,14 @@ def storage_folder(
547549
mark: str = None,
548550
addition: str = None,
549551
mix=False,
550-
folder_name: str = None) -> Path:
551-
if batch and all((id_, name, addition)):
552+
folder_name: str = None,
553+
collect=False, ) -> Path:
554+
if collect and all((id_, name, addition)):
555+
folder_name = f"CID{id_}_{mark or name}_{addition}"
556+
elif batch and all((id_, name, addition)):
552557
folder_name = f"UID{id_}_{mark or name}_{addition}"
553558
elif mix and all((id_, name, addition)):
554-
folder_name = f"MIX{id_}_{mark or name}_{addition}"
559+
folder_name = f"MID{id_}_{mark or name}_{addition}"
555560
else:
556561
folder_name = folder_name or self.folder_name
557562
folder = self.root.joinpath(folder_name)

src/extract/extractor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ def preprocessing_data(self,
526526
mix=False,
527527
tiktok=False,
528528
title=None,
529+
collect_id=None,
529530
) -> tuple:
530531
if tiktok:
531532
params = {
@@ -552,6 +553,7 @@ def preprocessing_data(self,
552553
post,
553554
mix,
554555
**params,
556+
collect_id=collect_id,
555557
)
556558

557559
def __preprocessing_data(self,
@@ -566,6 +568,7 @@ def __preprocessing_data(self,
566568
nickname="",
567569
mix_name="",
568570
title="",
571+
collect_id=None
569572
) -> tuple:
570573
for item in data:
571574
item = self.generate_data_object(item)
@@ -586,7 +589,7 @@ def __preprocessing_data(self,
586589
default="无效合集标题")
587590
mark = self.cleaner.filter_name(
588591
mark, inquire=False, default=title if mix else name)
589-
return id_, name.strip(), mid, title.strip(
592+
return id_, name.strip(), collect_id or mid, title.strip(
590593
), mark.strip(), data[:None if post else -1]
591594

592595
def __platform_classify_detail(

src/interface/account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ async def favorite_mode(self):
154154
if self.sec_user_id != (s := info.get("sec_uid")):
155155
self.log.error(
156156
f"sec_user_id {self.sec_user_id}{s} 不一致")
157-
self.__generate_temp_data()
157+
self._generate_temp_data()
158158
else:
159159
self.response.append({"author": info})
160160

161-
def __generate_temp_data(self, ):
161+
def _generate_temp_data(self, ):
162162
temp_data = timestamp()
163163
self.log.warning(f"获取账号昵称失败,本次运行将临时使用 {temp_data} 作为账号昵称和 UID")
164164
temp_dict = {

src/interface/account_tiktok.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ async def favorite_mode(self):
128128
if self.sec_user_id != (s := info.get("sec_uid")):
129129
self.log.error(
130130
f"sec_user_id {self.sec_user_id}{s} 不一致")
131-
self.__generate_temp_data()
131+
self._generate_temp_data()
132132
else:
133133
self.response.append({"author": info})
134134

src/interface/template.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import TYPE_CHECKING
55
from typing import Type
66
from typing import Union
7-
from urllib.parse import quote
87

98
from httpx import AsyncClient
109
from rich.progress import (
@@ -21,6 +20,8 @@
2120
from src.tools import TikTokDownloaderError
2221
from src.tools import capture_error_request
2322

23+
# from urllib.parse import quote
24+
2425
# from urllib.parse import urlencode
2526

2627
if TYPE_CHECKING:
@@ -313,16 +314,9 @@ def __record_request_messages(
313314

314315
def deal_url_params(self, params: dict, method="GET", **kwargs, ):
315316
if params:
316-
params["a_bogus"] = quote(
317-
self.ab.get_value(
318-
params,
319-
method,
320-
),
321-
safe="",
322-
)
323-
# params["a_bogus"] = quote(
324-
# self.ab.generate_abogus(
325-
# urlencode(params), method, )[1], safe="", )
317+
params["a_bogus"] = self.ab.get_value(params, method, )
318+
# params["a_bogus"] = self.ab.generate_abogus(
319+
# urlencode(params), method, )[1]
326320

327321
def summary_works(self, ) -> None:
328322
self.log.info(f"共获取到 {len(self.response)}{self.text}")
@@ -430,6 +424,6 @@ async def request_data(self,
430424

431425
def deal_url_params(self, params: dict, number=8, **kwargs, ):
432426
if params:
433-
params["X-Bogus"] = quote(self.xb.get_x_bogus(
427+
params["X-Bogus"] = self.xb.get_x_bogus(
434428
params, number, self.headers.get(
435-
"User-Agent", USERAGENT)), safe="", )
429+
"User-Agent", USERAGENT))

src/link/extractor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def extract_info(pattern, urls: str, index=1) -> list[str]:
117117

118118
class ExtractorTikTok(Extractor):
119119
SEC_UID = compile(r'"secUid":"([a-zA-Z0-9_-]+)"')
120-
ROOD_ID = compile(r'room_id=(\d+)"')
120+
ROOD_ID = compile(r'"roomId":"(\d+)"')
121121
MIX_ID = compile(r'"playlistId":"(\d{19})"')
122122

123123
account_link = compile(r"\S*?(https://www\.tiktok\.com/@[^\s/]+)\S*?")

0 commit comments

Comments
 (0)