rogerxavier
commited on
Commit
•
8b66c53
1
Parent(s):
3749294
Update api.py
Browse files
api.py
CHANGED
@@ -12,7 +12,8 @@ import subprocess
|
|
12 |
from fastapi import BackgroundTasks
|
13 |
import time
|
14 |
from bilibili_api import sync, video_uploader, Credential,user#bili上传部分和用户信息获取部分
|
15 |
-
import
|
|
|
16 |
import json #对bili_meta_data进行dumps ->str和loads—>dict
|
17 |
import bilibili_api
|
18 |
import random #随机获取cover
|
@@ -252,7 +253,14 @@ async def upload_with_random_cover(bili_meta_data:dict):
|
|
252 |
async def ev(data):
|
253 |
print(data)
|
254 |
|
255 |
-
await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
|
257 |
|
258 |
|
@@ -309,3 +317,100 @@ async def get_userInfo()->dict:
|
|
309 |
#查看账号信息
|
310 |
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
from fastapi import BackgroundTasks
|
13 |
import time
|
14 |
from bilibili_api import sync, video_uploader, Credential,user#bili上传部分和用户信息获取部分
|
15 |
+
from bilibili_api import comment#评论
|
16 |
+
import asyncio #保证后台任务的先后 ,同时适当sleep在评论的时候
|
17 |
import json #对bili_meta_data进行dumps ->str和loads—>dict
|
18 |
import bilibili_api
|
19 |
import random #随机获取cover
|
|
|
253 |
async def ev(data):
|
254 |
print(data)
|
255 |
|
256 |
+
if (await bili_status()) is True:
|
257 |
+
#账号登录状态才能发
|
258 |
+
print("当前账号登录状态:true,允许发送和评论")
|
259 |
+
await uploader.start()
|
260 |
+
await send_ad_to_comment("jm uc,简介下崽")
|
261 |
+
|
262 |
+
else:
|
263 |
+
print("当前账号未登录,停止操作")
|
264 |
|
265 |
|
266 |
|
|
|
317 |
#查看账号信息
|
318 |
|
319 |
|
320 |
+
|
321 |
+
async def send_ad_to_comment(ad_comment:str):
|
322 |
+
try:
|
323 |
+
await asyncio.sleep(900)#睡15分钟左右等待投稿完成
|
324 |
+
credential = Credential(sessdata=sessdata,
|
325 |
+
bili_jct=bili_jct,
|
326 |
+
buvid3=buvid3)
|
327 |
+
selfUserInfo = await user.get_self_info(credential=credential)
|
328 |
+
selfUserName = selfUserInfo[
|
329 |
+
"name"
|
330 |
+
]
|
331 |
+
selfUid = await user.name2uid_sync(selfUserName)
|
332 |
+
self_uid_int = selfUid['uid_list'][0]['uid']
|
333 |
+
print('当前uid是',self_uid_int)
|
334 |
+
selfUserClass = user.User(uid=self_uid_int)
|
335 |
+
selfVideos = await selfUserClass.get_media_list()
|
336 |
+
bvid_no_ad = []#记录没有写广告评论的video的bvid
|
337 |
+
for v in selfVideos['media_list']:
|
338 |
+
if (await has_ad(await get_comments(v['id']),selfUserName)) is False:
|
339 |
+
print("发现未广告评论的video",v,"予以记录")
|
340 |
+
bvid_no_ad.append(v['id'])
|
341 |
+
print("未广告的video bvid",bvid_no_ad)
|
342 |
+
for bvid in bvid_no_ad:
|
343 |
+
send_ad_result = await send_comment_ad(ad_comment=ad_comment,bvid=bvid,credential=credential)
|
344 |
+
await asyncio.sleep(10)
|
345 |
+
print(send_ad_result)
|
346 |
+
except Exception as e:
|
347 |
+
print("自动评论系统失败,原因是:",e)
|
348 |
+
|
349 |
+
|
350 |
+
|
351 |
+
|
352 |
+
async def has_ad(comments:list,selfUserName:str)->bool:
|
353 |
+
# 打印评论
|
354 |
+
for cmt in comments:
|
355 |
+
# print(f"{cmt['member']['uname']}: {cmt['content']['message']}")
|
356 |
+
if cmt['member']['uname'] is selfUserName:
|
357 |
+
return True
|
358 |
+
await asyncio.sleep(10)
|
359 |
+
all_sub_comment = await get_sub_comments(bvid=cmt['oid'],commentID=cmt['rpid'])
|
360 |
+
#用str进行模糊匹配.不然直接in dict不能判断子项
|
361 |
+
if selfUserName in str(all_sub_comment):
|
362 |
+
return True
|
363 |
+
return False
|
364 |
+
|
365 |
+
|
366 |
+
|
367 |
+
|
368 |
+
#根据video bvid获取评论list
|
369 |
+
async def get_comments(bvid:int)->list:
|
370 |
+
# 存储评论
|
371 |
+
comments = []
|
372 |
+
# 页码
|
373 |
+
page = 1
|
374 |
+
# 当前已获取数量
|
375 |
+
count = 0
|
376 |
+
while True:
|
377 |
+
# 获取评论
|
378 |
+
c = await comment.get_comments(bvid, comment.CommentResourceType.VIDEO, page)
|
379 |
+
# 存储评论
|
380 |
+
comments.extend(c['replies'])
|
381 |
+
# 增加已获取数量
|
382 |
+
count += c['page']['size']
|
383 |
+
# 增加页码
|
384 |
+
page += 1
|
385 |
+
await asyncio.sleep(10)
|
386 |
+
|
387 |
+
if count >= c['page']['count']:
|
388 |
+
# 当前已获取数量已达到评论总数,跳出循环
|
389 |
+
break
|
390 |
+
|
391 |
+
|
392 |
+
return comments #
|
393 |
+
|
394 |
+
|
395 |
+
#根据video bvid获取评论子评论list
|
396 |
+
async def get_sub_comments(bvid:int,commentID:int,credential=None)->dict:
|
397 |
+
cmt = comment.Comment(
|
398 |
+
oid=bvid,
|
399 |
+
type_=comment.CommentResourceType.VIDEO,
|
400 |
+
rpid=commentID,
|
401 |
+
credential=credential,
|
402 |
+
)
|
403 |
+
return await cmt.get_sub_comments()
|
404 |
+
|
405 |
+
|
406 |
+
|
407 |
+
#评论
|
408 |
+
async def send_comment_ad(ad_comment:str,bvid:int,credential):
|
409 |
+
print("测试只回复视频而不是子评论")
|
410 |
+
result = await comment.send_comment(
|
411 |
+
ad_comment,
|
412 |
+
oid=bvid,
|
413 |
+
type_=comment.CommentResourceType.VIDEO,
|
414 |
+
credential=credential,
|
415 |
+
)
|
416 |
+
return result
|