Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新了API #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions BiliUtil/Space/channel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# coding=utf-8
from __future__ import annotations
import re
from urllib import parse
import BiliUtil.Util as Util
import BiliUtil.Video as Video

from .. import Util, Video


class Channel:
Expand All @@ -24,11 +25,10 @@ def set_by_url(self, url):
self.uid = str(uid)
self.cid = str(cid)

def get_album_list(self, cookie=None):
async def get_album_list(self, cookie=None):
# 检验必要的参数
if self.uid is None or self.cid is None:
raise Util.ParameterError('缺少获取频道列表的必要参数')

# 发送网络请求
http_request = {
'info_obj': Util.CHANNEL,
Expand All @@ -43,7 +43,7 @@ def get_album_list(self, cookie=None):
}
album_list = []
while True:
json_data = Util.http_get(**http_request)
json_data = await Util.http_get(**http_request)

# 修改对象信息
self.name = json_data['data']['list']['name']
Expand Down
112 changes: 77 additions & 35 deletions BiliUtil/Space/user.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
# coding=utf-8
from __future__ import annotations
import re
import copy
from urllib import parse
import BiliUtil.Util as Util
import BiliUtil.Space as Space
import BiliUtil.Video as Video
from typing import Optional, List, Union, Dict, Any
from .. import Util, Video
from .channel import Channel


class User:
def __init__(self, uid=None):
self.uid = str(uid)
self.name = None
self.birthday = None
self.title = None
self.face = None
self.time = None
self.level = None
self.sex = None
self.sign = None
self.vip = None

def set_user(self, uid):
def __init__(self, uid: Optional[Union[int, str]] = None) -> None:
self.uid: Optional[str] = str(uid) if uid is not None else None
self.name: Optional[str] = None
self.birthday: Optional[str] = None
self.title: Optional[str] = None
self.face: Optional[str] = None
self.time: Optional[str] = None # The datatype might need verification
self.level: Optional[str] = None # The datatype might need verification
self.sex: Optional[str] = None
self.sign: Optional[str] = None
self.vip: Optional[bool] = None

def set_user(self, uid: Union[int, str]) -> None:
self.uid = str(uid)

def set_by_url(self, url):
def set_by_url(self, url: str) -> None:
input_url = parse.urlparse(url)
uid = re.match('/([0-9]+)', input_url.path).group(1)
self.uid = str(uid)

def sync(self, cookie=None):
async def sync(self) -> Dict[str, Any]:
# 检验必要的参数
if self.uid is None:
raise Util.ParameterError('缺少获取用户信息的必要参数')

# 发送网络请求
http_request = {
'info_obj': Util.USER,
'params': {
'params': Util.enc_params({
'mid': str(self.uid),
'jsonp': 'jsonp'
},
'cookie': cookie
}),
'cookie': Util.get_cookie()
}
json_data = Util.http_get(**http_request)
json_data = await Util.http_get(**http_request)

# 修改对象信息
self.name = json_data['data']['name']
Expand All @@ -57,7 +56,7 @@ def sync(self, cookie=None):
# 返回用户信息
return copy.deepcopy(vars(self))

def get_channel_list(self, cookie=None):
async def get_channel_list(self, cookie: Optional[str] = None) -> List[Channel]:
if self.uid is None:
raise BaseException('缺少获取频道列表的必要参数')

Expand All @@ -71,37 +70,80 @@ def get_channel_list(self, cookie=None):
},
'cookie': cookie
}
json_data = Util.http_get(**http_request)
channel_list = list(Space.Channel(self.uid, ch['cid']) for ch in json_data['data']['list'])
json_data = await Util.http_get(**http_request)
channel_list = list(Channel(self.uid, ch['cid']) for ch in json_data['data']['list'])

# 返回频道列表
return channel_list

def get_album_list(self, cookie=None):
async def get_album_list(self, count: int = Util.FetchConfig.ALL) -> List[Video.Album]:
# 检验必要的参数
if self.uid is None:
raise Util.ParameterError('缺少获取视频列表的必要参数')

# 发送网络请求
http_request = {
'info_obj': Util.USER_VIDEO,
'params': {
'mid': str(self.uid),
'pagesize': 30,
'tid': 0,
'page': 1,
'pn': 1,
'order': 'pubdate'
},
'cookie': cookie
'cookie': Util.get_cookie()
}
album_list = []
while True:
json_data = Util.http_get(**http_request)
json_data = await Util.http_get(**http_request)

new_album_list = [Video.Album(av['aid']) for av in json_data['data']['list']['vlist']]
if count != Util.FetchConfig.ALL and len(album_list) + len(new_album_list) >= count:
album_list.extend(new_album_list[:count - len(album_list)])
break

# 循环获取列表
album_list.extend([Video.Album(av['aid']) for av in json_data['data']['vlist']])
if len(album_list) < int(json_data['data']['count']):
http_request['params']['page'] += 1
album_list.extend([Video.Album(av['aid']) for av in json_data['data']['list']['vlist']])

if len(album_list) < int(json_data['data']['page']['count']):
http_request['params']['pn'] += 1
else:
break

# 返回视频列表
return album_list

async def get_album_list_by_search(self, keyword: str = '', count: int = Util.FetchConfig.ALL, tid: int = 0) -> \
List[Video.Album]:
# 检验必要的参数
if self.uid is None:
raise Util.ParameterError('缺少获取视频列表的必要参数')
# 发送网络请求
http_request = {
'info_obj': Util.USER_VIDEO,
'params': Util.enc_params({
'keyword': keyword,
'mid': str(self.uid),
'pagesize': 30,
'tid': tid,
'pn': 1,
'order': 'pubdate'
}),
'cookie': Util.get_cookie()
}
album_list = []
while True:
json_data = await Util.http_get(**http_request)

new_album_list = [Video.Album(av['aid']) for av in json_data['data']['list']['vlist']]
if count != Util.FetchConfig.ALL and len(album_list) + len(new_album_list) >= count:
album_list.extend(new_album_list[:count - len(album_list)])
break

# 循环获取列表
album_list.extend([Video.Album(av['aid']) for av in json_data['data']['list']['vlist']])

if len(album_list) < int(json_data['data']['page']['count']):
http_request['params']['pn'] += 1
else:
break

Expand Down
5 changes: 5 additions & 0 deletions BiliUtil/Util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
from .tools import ffmpeg_merge
from .tools import ParameterError
from .tools import RunningError
from .tools import FetchConfig
from .tools import set_cookie
from .tools import get_cookie
from .tools import enc_params
from .tools import ConnectError
1 change: 0 additions & 1 deletion BiliUtil/Util/fake_useragent.json

This file was deleted.

Loading