/
client.py
2465 lines (1881 loc) · 80.7 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import logging
import socket
from typing import TYPE_CHECKING, List, Literal, Optional, Union
import aiohttp
import yarl
from .api import (
add_bawu,
add_bawu_blacklist,
add_blacklist_old,
add_post,
agree,
agree_vimage,
block,
del_bawu,
del_bawu_blacklist,
del_blacklist_old,
del_post,
del_posts,
del_thread,
del_threads,
dislike_forum,
follow_forum,
follow_user,
get_ats,
get_bawu_blacklist,
get_bawu_info,
get_bawu_perm,
get_bawu_postlogs,
get_bawu_userlogs,
get_blacklist,
get_blacklist_old,
get_blocks,
get_cid,
get_comments,
get_dislike_forums,
get_fans,
get_fid,
get_follow_forums,
get_follows,
get_forum,
get_forum_detail,
get_god_threads,
get_group_msg,
get_images,
get_member_users,
get_posts,
get_rank_forums,
get_rank_users,
get_recom_status,
get_recovers,
get_replys,
get_self_follow_forums,
get_selfinfo_initNickname,
get_selfinfo_moindex,
get_square_forums,
get_statistics,
get_tab_map,
get_threads,
get_uinfo_getuserinfo_app,
get_uinfo_getUserInfo_web,
get_uinfo_panel,
get_uinfo_user_json,
get_unblock_appeals,
get_user_contents,
good,
handle_unblock_appeals,
init_z_id,
login,
move,
profile,
recommend,
recover,
remove_fan,
search_exact,
send_msg,
set_bawu_perm,
set_blacklist,
set_msg_readed,
set_nickname_old,
set_profile,
set_thread_privacy,
sign_forum,
sign_growth,
sync,
tieba_uid2user_info,
top,
unblock,
undislike_forum,
unfollow_forum,
unfollow_user,
ungood,
)
from .api._classdef import UserInfo
from .config import ProxyConfig, TimeoutConfig
from .const import MAIN_VERSION
from .core import Account, HttpCore, NetCore, WsCore
from .enums import (
BawuPermType,
BawuSearchType,
BawuType,
BlacklistType,
Gender,
GroupType,
PostSortType,
RankForumType,
ReqUInfo,
SearchType,
ThreadSortType,
WsStatus,
)
from .exception import BoolResponse, IntResponse, StrResponse
from .helper.cache import ForumInfoCache
from .helper.utils import handle_exception, is_portrait, is_user_name
from .logging import get_logger as LOG
from .typing import TypeUserInfo
if TYPE_CHECKING:
import datetime
def _try_websocket(func):
async def awrapper(self: "Client", *args, **kwargs):
if self._try_ws:
await self.init_websocket()
return await func(self, *args, **kwargs)
awrapper.__name__ = func.__name__
return awrapper
def _force_websocket(func):
async def awrapper(self: "Client", *args, **kwargs):
await self.init_websocket()
return await func(self, *args, **kwargs)
awrapper.__name__ = func.__name__
return awrapper
class Client:
"""
贴吧客户端
Args:
BDUSS (str, optional): BDUSS. Defaults to ''.
STOKEN (str, optional): STOKEN. Defaults to ''.
account (Account, optional): Account实例 该字段会覆盖前两个参数. Defaults to None.
try_ws (bool, optional): 尝试使用websocket接口. Defaults to False.
proxy (bool | ProxyConfig, optional): True则使用环境变量代理 False则禁用代理 输入ProxyConfig实例以手动配置代理. Defaults to False.
timeout (TimeoutConfig, optional): 超时配置. Defaults to TimeoutConfig().
loop (asyncio.AbstractEventLoop, optional): 事件循环. Defaults to None.
"""
__slots__ = [
'_connector',
'_http_core',
'_ws_core',
'_try_ws',
'_user',
]
def __init__(
self,
BDUSS: str = '',
STOKEN: str = '',
*,
account: Optional[Account] = None,
try_ws: bool = False,
proxy: Union[bool, ProxyConfig] = False,
timeout: TimeoutConfig = TimeoutConfig,
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> None:
if loop is None:
loop = asyncio.get_running_loop()
if not isinstance(timeout, TimeoutConfig):
timeout = TimeoutConfig()
connector = aiohttp.TCPConnector(
ttl_dns_cache=timeout.dns_ttl,
family=socket.AF_INET,
keepalive_timeout=timeout.http_keepalive,
limit=0,
ssl=False,
loop=loop,
)
self._connector = connector
if proxy is True:
proxy = ProxyConfig.from_env()
elif not proxy:
proxy = ProxyConfig()
if not isinstance(account, Account):
account = Account(BDUSS, STOKEN)
net_core = NetCore(connector, proxy, timeout)
self._http_core = HttpCore(account, net_core, loop)
self._ws_core = WsCore(account, net_core, loop)
self._try_ws = try_ws
self._user = UserInfo()
async def __aenter__(self) -> "Client":
return self
async def __aexit__(self, exc_type=None, exc_val=None, exc_tb=None) -> None:
await self._ws_core.close()
await self._connector.close()
def __hash__(self) -> int:
return hash(self.account)
def __eq__(self, obj: "Client") -> bool:
return self.account == obj.account
@property
def account(self) -> Account:
return self._http_core.account
@account.setter
def account(self, new_account: Account) -> None:
self._http_core.set_account(new_account)
self._ws_core.set_account(new_account)
@handle_exception(BoolResponse)
async def init_websocket(self) -> BoolResponse:
"""
初始化websocket
Returns:
BoolResponse: True无须执行 False失败
"""
if self._ws_core.status == WsStatus.CLOSED:
try:
await self._ws_core.connect()
await self.__upload_sec_key()
except BaseException:
self._ws_core._status = WsStatus.CLOSED
raise
return BoolResponse()
async def __upload_sec_key(self) -> None:
from .api import init_websocket
from .core.websocket import MsgIDPair
groups = await init_websocket.request(self._ws_core)
mid_manager = self._ws_core.mid_manager
for group in groups:
if group.group_type == GroupType.PRIVATE_MSG:
mid_manager.priv_gid = group.group_id
mid_manager.gid2mid = {g.group_id: MsgIDPair(g.last_msg_id, g.last_msg_id) for g in groups}
self._ws_core._status = WsStatus.OPEN
async def __init_tbs(self) -> None:
if self.account.tbs:
return
await self.__login()
@handle_exception(UserInfo)
async def get_self_info(self, require: ReqUInfo = ReqUInfo.ALL) -> UserInfo:
"""
获取本账号信息
Args:
require (ReqUInfo): 指示需要获取的字段
Returns:
TypeUserInfo: 用户信息
"""
if not self._user.user_id:
if require & ReqUInfo.BASIC:
await self.__login()
if not self._user.tieba_uid:
if require == ReqUInfo.ALL:
user = await self._get_uinfo_profile(self._user.user_id)
self._user |= user
elif require & (ReqUInfo.TIEBA_UID | ReqUInfo.NICK_NAME):
await self.__get_selfinfo_initNickname()
return self._user
async def __login(self) -> None:
user, tbs = await login.request(self._http_core)
self._user |= user
self.account.tbs = tbs
async def __init_client_id(self) -> None:
if self.account.client_id:
return
await self.__sync()
async def __init_sample_id(self) -> None:
if self.account.sample_id:
return
await self.__sync()
async def __sync(self) -> None:
client_id, sample_id = await sync.request(self._http_core)
self.account.client_id = client_id
self.account.sample_id = sample_id
async def __init_z_id(self) -> None:
if self.account.z_id:
return
z_id = await init_z_id.request(self._http_core)
self.account.z_id = z_id
@handle_exception(get_forum.Forum)
async def get_forum(self, fname_or_fid: Union[str, int]) -> get_forum.Forum:
"""
通过forum_id获取贴吧信息
此接口较`get_forum_detail`更强大
Args:
fname_or_fid (str | int): 目标贴吧名或fid 优先贴吧名
Returns:
Forum: 贴吧信息
"""
fname = fname_or_fid if isinstance(fname_or_fid, str) else await self.__get_fname(fname_or_fid)
return await get_forum.request(self._http_core, fname)
@handle_exception(get_forum_detail.Forum_detail)
@_try_websocket
async def get_forum_detail(self, fname_or_fid: Union[str, int]) -> get_forum_detail.Forum_detail:
"""
通过forum_id获取贴吧信息
Args:
fname_or_fid (str | int): 目标贴吧名或fid 优先fid
Returns:
Forum_detail: 贴吧信息
"""
fid = fname_or_fid if isinstance(fname_or_fid, int) else await self.__get_fid(fname_or_fid)
if self._ws_core.status == WsStatus.OPEN:
return await get_forum_detail.request_ws(self._ws_core, fid)
return await get_forum_detail.request_http(self._http_core, fid)
async def __get_fid(self, fname: str) -> int:
if fid := ForumInfoCache.get_fid(fname):
return fid
fid = await get_fid.request(self._http_core, fname)
ForumInfoCache.add_forum(fname, fid)
return fid
@handle_exception(IntResponse)
async def get_fid(self, fname: str) -> IntResponse:
"""
通过贴吧名获取forum_id
Args:
fname (str): 贴吧名
Returns:
IntResponse: forum_id
"""
fid = await self.__get_fid(fname)
return IntResponse(fid)
async def __get_fname(self, fid: int) -> str:
if fname := ForumInfoCache.get_fname(fid):
return fname
fdetail = await self.get_forum_detail(fid)
fname = fdetail.fname
if fname:
ForumInfoCache.add_forum(fname, fid)
return fname
@handle_exception(StrResponse)
async def get_fname(self, fid: int) -> StrResponse:
"""
通过forum_id获取贴吧名
Args:
fid (int): forum_id
Returns:
StrResponse: 贴吧名
"""
fname = await self.__get_fname(fid)
return StrResponse(fname)
@handle_exception(get_threads.Threads)
@_try_websocket
async def get_threads(
self,
fname_or_fid: Union[str, int],
/,
pn: int = 1,
*,
rn: int = 30,
sort: ThreadSortType = ThreadSortType.REPLY,
is_good: bool = False,
) -> get_threads.Threads:
"""
获取首页帖子
Args:
fname_or_fid (str | int): 贴吧名或fid 优先贴吧名
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 30. Max to 100.
sort (ThreadSortType, optional): HOT热门排序 REPLY按回复时间 CREATE按发布时间 FOLLOW关注的人. Defaults to ThreadSortType.REPLY.
is_good (bool, optional): True则获取精品区帖子 False则获取普通区帖子. Defaults to False.
Returns:
Threads: 帖子列表
"""
fname = fname_or_fid if isinstance(fname_or_fid, str) else await self.__get_fname(fname_or_fid)
if self._ws_core.status == WsStatus.OPEN:
return await get_threads.request_ws(self._ws_core, fname, pn, rn, sort, is_good)
return await get_threads.request_http(self._http_core, fname, pn, rn, sort, is_good)
@handle_exception(get_posts.Posts)
@_try_websocket
async def get_posts(
self,
tid: int,
/,
pn: int = 1,
*,
rn: int = 30,
sort: PostSortType = PostSortType.ASC,
only_thread_author: bool = False,
with_comments: bool = False,
comment_sort_by_agree: bool = True,
comment_rn: int = 4,
) -> get_posts.Posts:
"""
获取主题帖内回复
Args:
tid (int): 所在主题帖tid
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 30.
sort (PostSortType, optional): ASC时间顺序 DESC时间倒序 HOT热门序. Defaults to PostSortType.ASC.
only_thread_author (bool, optional): True则只看楼主 False则请求全部. Defaults to False.
with_comments (bool, optional): True则同时请求高赞楼中楼 False则返回的Post.comments字段为空. Defaults to False.
comment_sort_by_agree (bool, optional): True则楼中楼按点赞数顺序 False则楼中楼按时间顺序. Defaults to True.
comment_rn (int, optional): 请求的楼中楼数量. Defaults to 4. Max to 50.
Returns:
Posts: 回复列表
"""
if self._ws_core.status == WsStatus.OPEN:
return await get_posts.request_ws(
self._ws_core, tid, pn, rn, sort, only_thread_author, with_comments, comment_sort_by_agree, comment_rn
)
return await get_posts.request_http(
self._http_core, tid, pn, rn, sort, only_thread_author, with_comments, comment_sort_by_agree, comment_rn
)
@handle_exception(get_comments.Comments)
@_try_websocket
async def get_comments(
self, tid: int, pid: int, /, pn: int = 1, *, is_comment: bool = False
) -> get_comments.Comments:
"""
获取楼中楼回复
Args:
tid (int): 所在主题帖tid
pid (int): 所在楼层的pid或楼中楼的pid
pn (int, optional): 页码. Defaults to 1.
is_comment (bool, optional): pid是否指向楼中楼 若指向楼中楼则获取其附近的楼中楼列表. Defaults to False.
Returns:
Comments: 楼中楼列表
"""
if self._ws_core.status == WsStatus.OPEN:
return await get_comments.request_ws(self._ws_core, tid, pid, pn, is_comment)
return await get_comments.request_http(self._http_core, tid, pid, pn, is_comment)
@handle_exception(search_exact.ExactSearches)
async def search_exact(
self,
fname_or_fid: Union[str, int],
query: str,
/,
pn: int = 1,
*,
rn: int = 30,
search_type: SearchType = SearchType.ALL,
only_thread: bool = False,
) -> search_exact.ExactSearches:
"""
贴吧搜索
Args:
fname_or_fid (str | int): 查询的贴吧名或fid 优先贴吧名
query (str): 查询文本
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 30.
search_type (SearchType, optional): 查询模式 默认查询全部. Defaults to SearchType.ALL.
only_thread (bool, optional): 是否仅查询主题帖. Defaults to False.
Returns:
ExactSearches: 搜索结果列表
"""
fname = fname_or_fid if isinstance(fname_or_fid, str) else await self.__get_fname(fname_or_fid)
return await search_exact.request(self._http_core, fname, query, pn, rn, search_type, only_thread)
@handle_exception(profile.UserInfo_pf)
@_try_websocket
async def _get_uinfo_profile(self, uid_or_portrait: Union[str, int]) -> profile.UserInfo_pf:
"""
接口 https://tiebac.baidu.com/c/u/user/profile
Args:
uid_or_portrait (str | int): 用户id user_id / portrait
Returns:
UserInfo_pf: 包含最全面的用户信息
"""
if self._ws_core.status == WsStatus.OPEN:
return await profile.get_uinfo_profile.request_ws(self._ws_core, uid_or_portrait)
return await profile.get_uinfo_profile.request_http(self._http_core, uid_or_portrait)
@handle_exception(get_uinfo_getuserinfo_app.UserInfo_guinfo_app)
@_try_websocket
async def _get_uinfo_getuserinfo(self, user_id: int) -> get_uinfo_getuserinfo_app.UserInfo_guinfo_app:
"""
接口 https://tiebac.baidu.com/c/u/user/getuserinfo
Args:
user_id (int): 用户id user_id
Returns:
UserInfo_guinfo_app: 包含 user_id / portrait / user_name / nick_name_old / 性别 /
是否大神 / 是否超级会员
"""
if self._ws_core.status == WsStatus.OPEN:
user = await get_uinfo_getuserinfo_app.request_ws(self._ws_core, user_id)
else:
user = await get_uinfo_getuserinfo_app.request_http(self._http_core, user_id)
if (user_id := user.user_id) < 0:
user.user_id = 0xFFFFFFFF + user_id
return user
@handle_exception(get_uinfo_getUserInfo_web.UserInfo_guinfo_web)
async def _get_uinfo_getUserInfo(self, user_id: int) -> get_uinfo_getUserInfo_web.UserInfo_guinfo_web:
"""
接口 http://tieba.baidu.com/im/pcmsg/query/getUserInfo
Args:
user_id (int): 用户id user_id
Returns:
UserInfo_guinfo_web: 包含 user_id / portrait / user_name / nick_name_new
Note:
该接口需要BDUSS
"""
user = await get_uinfo_getUserInfo_web.request(self._http_core, user_id)
user.user_id = user_id
return user
@handle_exception(get_uinfo_user_json.UserInfo_json)
async def _get_uinfo_user_json(self, user_name: str) -> get_uinfo_user_json.UserInfo_json:
"""
接口 http://tieba.baidu.com/i/sys/user_json
Args:
user_name (str): 用户id user_name
Returns:
UserInfo_json: 包含 user_id / portrait / user_name
"""
user = await get_uinfo_user_json.request(self._http_core, user_name)
user.user_name = user_name
return user
@handle_exception(get_uinfo_panel.UserInfo_panel)
async def _get_uinfo_panel(self, name_or_portrait: str) -> get_uinfo_panel.UserInfo_panel:
"""
接口 https://tieba.baidu.com/home/get/panel
Args:
name_or_portrait (str): 用户id user_name / portrait
Returns:
UserInfo_panel: 包含较全面的用户信息
Note:
从2022.08.30开始服务端不再返回user_id字段 请谨慎使用\n
该接口可判断用户是否被屏蔽\n
该接口rps阈值较低
"""
return await get_uinfo_panel.request(self._http_core, name_or_portrait)
async def get_user_info(self, id_: Union[str, int], /, require: ReqUInfo = ReqUInfo.ALL) -> TypeUserInfo:
"""
获取用户信息
Args:
id_ (str | int): 用户id user_id / portrait / user_name
require (ReqUInfo): 指示需要获取的字段
Returns:
TypeUserInfo: 用户信息
"""
if not id_:
LOG().warning("Null input")
return UserInfo()
if isinstance(id_, int):
if (require | ReqUInfo.BASIC) == ReqUInfo.BASIC:
# 仅有BASIC需求
return await self._get_uinfo_getuserinfo(id_)
else:
return await self._get_uinfo_profile(id_)
elif is_portrait(id_):
if (require | ReqUInfo.BASIC) == ReqUInfo.BASIC:
# 仅有BASIC需求
if not require & ReqUInfo.USER_ID:
# 无USER_ID需求
return await self._get_uinfo_panel(id_)
return await self._get_uinfo_profile(id_)
else:
if (require | ReqUInfo.BASIC) == ReqUInfo.BASIC:
return await self._get_uinfo_user_json(id_)
elif require & ReqUInfo.NICK_NAME and not require & ReqUInfo.USER_ID:
# 有NICK_NAME需求但无USER_ID需求
return await self._get_uinfo_panel(id_)
else:
user = await self._get_uinfo_user_json(id_)
return await self._get_uinfo_profile(user.portrait)
@handle_exception(tieba_uid2user_info.UserInfo_TUid)
@_try_websocket
async def tieba_uid2user_info(self, tieba_uid: int) -> tieba_uid2user_info.UserInfo_TUid:
"""
通过tieba_uid获取用户信息
Args:
tieba_uid (int): 用户id tieba_uid
Returns:
UserInfo_TUid: 包含较全面的用户信息
Note:
请注意tieba_uid与旧版user_id的区别
"""
if self._ws_core.status == WsStatus.OPEN:
return await tieba_uid2user_info.request_ws(self._ws_core, tieba_uid)
return await tieba_uid2user_info.request_http(self._http_core, tieba_uid)
@handle_exception(profile.Homepage)
@_try_websocket
async def get_homepage(self, id_: Union[str, int], /, pn: int = 1) -> profile.Homepage:
"""
获取用户个人页信息
Args:
id_ (str | int): 用户id user_id / user_name / portrait 优先user_id
pn (int, optional): 页码. Defaults to 1.
Returns:
Homepage: 用户个人页信息
"""
if not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
if self._ws_core.status == WsStatus.OPEN:
return await profile.get_homepage.request_ws(self._ws_core, user_id, pn)
return await profile.get_homepage.request_http(self._http_core, user_id, pn)
@handle_exception(get_follows.Follows)
async def get_follows(self, id_: Union[str, int, None] = None, /, pn: int = 1) -> get_follows.Follows:
"""
获取关注列表
Args:
pn (int, optional): 页码. Defaults to 1.
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
Returns:
Follows: 关注列表
"""
if id_ is None:
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
elif not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
return await get_follows.request(self._http_core, user_id, pn)
@handle_exception(get_fans.Fans)
async def get_fans(self, id_: Union[str, int, None] = None, /, pn: int = 1) -> get_fans.Fans:
"""
获取粉丝列表
Args:
pn (int, optional): 页码. Defaults to 1.
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
Returns:
Fans: 粉丝列表
"""
if id_ is None:
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
elif not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
return await get_fans.request(self._http_core, user_id, pn)
@handle_exception(get_blacklist.BlacklistUsers)
async def get_blacklist(self) -> get_blacklist.BlacklistUsers:
"""
获取新版用户黑名单列表
Returns:
BlacklistUsers: 新版用户黑名单列表
"""
return await get_blacklist.request(self._http_core)
@handle_exception(get_blacklist_old.BlacklistOldUsers)
@_try_websocket
async def get_blacklist_old(self, pn: int = 1, /, *, rn: int = 10) -> get_blacklist_old.BlacklistOldUsers:
"""
获取旧版用户黑名单列表
Args:
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 10. Max to Inf.
Returns:
BlacklistOldUsers: 旧版用户黑名单列表
"""
if self._ws_core.status == WsStatus.OPEN:
return await get_blacklist_old.request_ws(self._ws_core, pn, rn)
return await get_blacklist_old.request_http(self._http_core, pn, rn)
@handle_exception(get_follow_forums.FollowForums)
async def get_follow_forums(
self, id_: Union[str, int], /, pn: int = 1, *, rn: int = 50
) -> get_follow_forums.FollowForums:
"""
获取用户关注贴吧列表
Args:
id_ (str | int): 用户id user_id / user_name / portrait 优先user_id
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 50. Max to Inf.
Returns:
FollowForums: 用户关注贴吧列表
"""
if not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
return await get_follow_forums.request(self._http_core, user_id, pn, rn)
@handle_exception(get_self_follow_forums.SelfFollowForums)
async def get_self_follow_forums(self, pn: int = 1) -> get_self_follow_forums.SelfFollowForums:
"""
获取本账号关注贴吧列表
Args:
pn (int, optional): 页码. Defaults to 1.
Returns:
SelfFollowForums: 本账号关注贴吧列表
Note:
本接口需要STOKEN
"""
return await get_self_follow_forums.request(self._http_core, pn)
@handle_exception(get_dislike_forums.DislikeForums)
@_try_websocket
async def get_dislike_forums(self, pn: int = 1, /, *, rn: int = 20) -> get_dislike_forums.DislikeForums:
"""
获取首页推荐屏蔽的贴吧列表
Args:
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 20. Max to 20.
Returns:
DislikeForums: 首页推荐屏蔽的贴吧列表
"""
if self._ws_core.status == WsStatus.OPEN:
return await get_dislike_forums.request_ws(self._ws_core, pn, rn)
return await get_dislike_forums.request_http(self._http_core, pn, rn)
async def __get_user_posts(self, id_: Union[str, int], pn: int, rn: int):
if not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
UPOST_VERSION = "8.9.8.5"
return await get_user_contents.get_posts.request_http(self._http_core, user_id, pn, rn, UPOST_VERSION)
@_try_websocket
async def __get_self_posts(self, pn: int, rn: int):
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_posts.request_ws(self._ws_core, user_id, pn, rn, MAIN_VERSION)
return await get_user_contents.get_posts.request_http(self._http_core, user_id, pn, rn, MAIN_VERSION)
@handle_exception(get_user_contents.UserPostss)
async def get_user_posts(
self, id_: Union[str, int, None] = None, pn: int = 1, *, rn: int = 20
) -> get_user_contents.UserPostss:
"""
获取用户发布的回复列表
Args:
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
pn (int, optional): 页码. Defaults to 1.
rn (int, optional): 请求的条目数. Defaults to 20. Max to 50.
Returns:
UserPostss: 回复列表
"""
if id_ is None:
return await self.__get_self_posts(pn, rn)
else:
return await self.__get_user_posts(id_, pn, rn)
@handle_exception(get_user_contents.UserThreads)
@_try_websocket
async def get_user_threads(
self, id_: Union[str, int, None] = None, pn: int = 1, *, public_only: bool = False
) -> get_user_contents.UserThreads:
"""
获取用户发布的主题帖列表
Args:
id_ (str | int | None): 用户id user_id / user_name / portrait 优先user_id
默认为None即获取本账号信息. Defaults to None.
pn (int, optional): 页码. Defaults to 1.
public_only (bool, optional): 是否仅获取公开主题帖 该选项在获取他人主题帖时无效. Defaults to False.
Returns:
UserThreads: 主题帖列表
"""
if id_ is None:
user = await self.get_self_info(ReqUInfo.USER_ID)
user_id = user.user_id
elif not isinstance(id_, int):
user = await self.get_user_info(id_, ReqUInfo.USER_ID)
user_id = user.user_id
else:
user_id = id_
if self._ws_core.status == WsStatus.OPEN:
return await get_user_contents.get_threads.request_ws(self._ws_core, user_id, pn, public_only)
return await get_user_contents.get_threads.request_http(self._http_core, user_id, pn, public_only)
@handle_exception(get_replys.Replys)
@_try_websocket
async def get_replys(self, pn: int = 1) -> get_replys.Replys:
"""
获取回复信息
Args:
pn (int, optional): 页码. Defaults to 1.
Returns:
Replys: 回复列表
"""
if self._ws_core.status == WsStatus.OPEN:
return await get_replys.request_ws(self._ws_core, pn)
return await get_replys.request_http(self._http_core, pn)
@handle_exception(get_ats.Ats)
async def get_ats(self, pn: int = 1) -> get_ats.Ats:
"""
获取@信息
Args:
pn (int, optional): 页码. Defaults to 1.
Returns:
Ats: at列表
"""
return await get_ats.request(self._http_core, pn)
@handle_exception(get_images.ImageBytes)
async def get_image_bytes(self, img_url: str) -> get_images.ImageBytes:
"""
从链接获取静态图像的原始字节流
Args:
img_url (str): 图像链接
Returns:
ImageBytes: 未解码的原始字节流
"""
return await get_images.request_bytes(self._http_core, yarl.URL(img_url))
@handle_exception(get_images.Image)
async def get_image(self, img_url: str) -> get_images.Image:
"""
从链接获取静态图像
Args:
img_url (str): 图像链接
Returns:
Image: 图像
"""
return await get_images.request(self._http_core, yarl.URL(img_url))
@handle_exception(get_images.Image)
async def hash2image(self, raw_hash: str, /, size: Literal['s', 'm', 'l'] = 's') -> get_images.Image:
"""
通过百度图库hash获取静态图像