-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgist.py
93 lines (82 loc) · 3.04 KB
/
gist.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
import os
from loguru import logger
from dotenv import load_dotenv
from bilibili_api import Credential,user,sync
from github.InputFileContent import InputFileContent
from github import Github
ENV_VAR_GIST_ID = "GIST_ID"
ENV_VAR_GITHUB_TOKEN = "GH_TOKEN"
ENV_VAR_BILI_SESSDATA = "BILI_SESSDATA"
ENV_VAR_BILI_UID = "BILI_UID"
REQUIRED_ENVS = [
ENV_VAR_GIST_ID,
ENV_VAR_GITHUB_TOKEN,
ENV_VAR_BILI_SESSDATA,
ENV_VAR_BILI_UID
]
async def get_bili_video_list():
global vist
vist = await u.get_videos(ps=2)
return vist
async def get_bili_relation_info():
relate = await u.get_relation_info()
return relate
async def get_bili_user_info():
info= await u.get_user_info()
return info
def update_gist(title: str, content: str) -> bool:
access_token = os.environ[ENV_VAR_GITHUB_TOKEN]
gist_id = os.environ[ENV_VAR_GIST_ID]
gist = Github(access_token).get_gist(gist_id)
# Shouldn't necessarily work, keeping for case of single file made in hurry to get gist id.
old_title = list(gist.files.keys())[0]
gist.edit(title, {old_title: InputFileContent(content, title)})
logger.info(f"\n{title}\n{content}")
def getneededinfo(info: str,need: str):
return info[need]
def getvideoinfo(num: int,need: str):
return vist["list"]["vlist"][num][need]
def getvideodate(num: int):
import datetime
from pytz import timezone
tzc = timezone('Asia/Shanghai')
date= getvideoinfo(num,"created")
date_time = datetime.datetime.fromtimestamp(date,tz=tzc)
formated_date = date_time.strftime("%Y年%m月%d日 %H:%M:%S")
return formated_date
@logger.catch
def main():
uid = os.environ[ENV_VAR_BILI_UID]
sessdata= os.environ[ENV_VAR_BILI_SESSDATA]
global u
logger.info("Trying to get some info...")
cedential = Credential(sessdata=sessdata)
u= user.User(uid,credential=cedential)
i = sync(get_bili_user_info())
follows = sync(get_bili_relation_info())
sync(get_bili_video_list())
username= getneededinfo(i,"name")
follower= getneededinfo(follows,"follower")
following = getneededinfo(follows,"following")
title1 = getvideoinfo(0,"title")
date1 = getvideodate(0)
view1 = getvideoinfo(0,"play")
comment1 = getvideoinfo(0,"comment")
title2 = getvideoinfo(1,"title")
date2 = getvideodate(1)
view2 = getvideoinfo(1,"play")
comment2 = getvideoinfo(1,"comment")
base_info = f"👤粉丝数: {follower} 关注数: {following} \n"
video_info = f"{title1} \n -🕒:{date1} ▶︎:{view1} 💬:{comment1} \n {title2} \n -🕒:{date2} ▶︎:{view2} 💬:{comment2}"
contents = f"{base_info} ▶️最近更新视频: {video_info}"
logger.info("Updating gist...")
update_gist(f"📺bilibili@{username} ",contents)
if __name__== "__main__":
logger.add(f'./log.log',format='{time} {level} {function} - {message}')
load_dotenv(dotenv_path="./.env")
import time
logger.info("Starting jobs...")
s = time.perf_counter()
main()
elapsed = time.perf_counter() - s
logger.info(f"{__file__} executed in {elapsed:0.2f} seconds.")