修复删除已上传视频的逻辑,添加 追加上传功能
This commit is contained in:
parent
50216dc3e7
commit
cf8cbaae8c
170
bilibili.py
170
bilibili.py
@ -18,8 +18,10 @@ class VideoPart:
|
|||||||
self.title = title
|
self.title = title
|
||||||
self.desc = desc
|
self.desc = desc
|
||||||
|
|
||||||
|
|
||||||
class Bilibili:
|
class Bilibili:
|
||||||
def __init__(self, cookie=None):
|
def __init__(self, cookie=None):
|
||||||
|
self.files = []
|
||||||
self.videos = []
|
self.videos = []
|
||||||
self.session = requests.session()
|
self.session = requests.session()
|
||||||
if cookie:
|
if cookie:
|
||||||
@ -137,7 +139,6 @@ class Bilibili:
|
|||||||
)
|
)
|
||||||
json = r.json()
|
json = r.json()
|
||||||
|
|
||||||
|
|
||||||
if json['code'] is not 0:
|
if json['code'] is not 0:
|
||||||
return r.text
|
return r.text
|
||||||
|
|
||||||
@ -154,7 +155,6 @@ class Bilibili:
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def upload(self,
|
def upload(self,
|
||||||
parts,
|
parts,
|
||||||
title,
|
title,
|
||||||
@ -186,38 +186,23 @@ class Bilibili:
|
|||||||
:type no_reprint: int
|
:type no_reprint: int
|
||||||
"""
|
"""
|
||||||
self.preUpload(parts)
|
self.preUpload(parts)
|
||||||
self.finishUpload(title,tid,tag,desc,source,cover,no_reprint)
|
self.finishUpload(title, tid, tag, desc, source, cover, no_reprint)
|
||||||
|
|
||||||
def preUpload(self,parts):
|
def preUpload(self, parts):
|
||||||
"""
|
"""
|
||||||
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||||
:type parts: VideoPart or list<VideoPart>
|
:type parts: VideoPart or list<VideoPart>
|
||||||
:param title: video's title
|
|
||||||
:type title: str
|
|
||||||
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
|
||||||
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
|
||||||
:type tid: int
|
|
||||||
:param tag: video's tag
|
|
||||||
:type tag: list<str>
|
|
||||||
:param desc: video's description
|
|
||||||
:type desc: str
|
|
||||||
:param source: (optional) 转载地址
|
|
||||||
:type source: str
|
|
||||||
:param cover: (optional) cover's URL, use method *cover_up* to get
|
|
||||||
:type cover: str
|
|
||||||
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
|
||||||
:type no_reprint: int
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
if not isinstance(parts, list):
|
if not isinstance(parts, list):
|
||||||
parts = [parts]
|
parts = [parts]
|
||||||
|
|
||||||
videos = []
|
|
||||||
for part in parts:
|
for part in parts:
|
||||||
filepath = part.path
|
filepath = part.path
|
||||||
filename = os.path.basename(filepath)
|
filename = os.path.basename(filepath)
|
||||||
filesize = os.path.getsize(filepath)
|
filesize = os.path.getsize(filepath)
|
||||||
|
self.files.append(part)
|
||||||
r = self.session.get('https://member.bilibili.com/preupload?'
|
r = self.session.get('https://member.bilibili.com/preupload?'
|
||||||
'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0'
|
'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0'
|
||||||
.format(name=filename, size=filesize))
|
.format(name=filename, size=filesize))
|
||||||
@ -303,26 +288,142 @@ class Bilibili:
|
|||||||
cover='',
|
cover='',
|
||||||
no_reprint=1,
|
no_reprint=1,
|
||||||
):
|
):
|
||||||
|
"""
|
||||||
|
:param title: video's title
|
||||||
|
:type title: str
|
||||||
|
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||||
|
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||||
|
:type tid: int
|
||||||
|
:param tag: video's tag
|
||||||
|
:type tag: list<str>
|
||||||
|
:param desc: video's description
|
||||||
|
:type desc: str
|
||||||
|
:param source: (optional) 转载地址
|
||||||
|
:type source: str
|
||||||
|
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||||
|
:type cover: str
|
||||||
|
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||||
|
:type no_reprint: int
|
||||||
|
"""
|
||||||
|
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
copyright = 2 if source else 1
|
copyright = 2 if source else 1
|
||||||
r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
|
r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
|
||||||
json={
|
json={
|
||||||
"copyright" : copyright,
|
"copyright": copyright,
|
||||||
"source" : source,
|
"source": source,
|
||||||
"title" : title,
|
"title": title,
|
||||||
"tid" : tid,
|
"tid": tid,
|
||||||
"tag" : ','.join(tag),
|
"tag": ','.join(tag),
|
||||||
"no_reprint": no_reprint,
|
"no_reprint": no_reprint,
|
||||||
"desc" : desc,
|
"desc": desc,
|
||||||
"cover" : cover,
|
"cover": cover,
|
||||||
"mission_id": 0,
|
"mission_id": 0,
|
||||||
"order_id" : 0,
|
"order_id": 0,
|
||||||
"videos" : self.videos}
|
"videos": self.videos}
|
||||||
)
|
)
|
||||||
print(r.text)
|
print(r.text)
|
||||||
for _p in self.videos:
|
for _p in self.files:
|
||||||
os.remove(_p.path)
|
os.remove(_p.path)
|
||||||
|
|
||||||
|
def appendUpload(self,
|
||||||
|
aid,
|
||||||
|
parts,
|
||||||
|
title="",
|
||||||
|
tid="",
|
||||||
|
tag="",
|
||||||
|
desc="",
|
||||||
|
source='',
|
||||||
|
cover='',
|
||||||
|
no_reprint=1,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
:param aid: just aid
|
||||||
|
:type aid: int
|
||||||
|
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||||
|
:type parts: VideoPart or list<VideoPart>
|
||||||
|
:param title: video's title
|
||||||
|
:type title: str
|
||||||
|
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||||
|
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||||
|
:type tid: int
|
||||||
|
:param tag: video's tag
|
||||||
|
:type tag: list<str>
|
||||||
|
:param desc: video's description
|
||||||
|
:type desc: str
|
||||||
|
:param source: (optional) 转载地址
|
||||||
|
:type source: str
|
||||||
|
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||||
|
:type cover: str
|
||||||
|
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||||
|
:type no_reprint: int
|
||||||
|
"""
|
||||||
|
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
|
p = self.session.get("https://member.bilibili.com/x/web/archive/view?aid={}&history=".format(aid))
|
||||||
|
j = p.json()
|
||||||
|
if len(self.videos) == 0:
|
||||||
|
for i in j['data']['videos']:
|
||||||
|
self.videos.append({'filename': i['filename'],
|
||||||
|
'title': i["title"],
|
||||||
|
'desc': i["desc"]})
|
||||||
|
if (title == ""): title = j["data"]["archive"]['title']
|
||||||
|
if (tag == ""): tag = j["data"]["archive"]['tag']
|
||||||
|
if (no_reprint == ""): no_reprint = j["data"]["archive"]['no_reprint']
|
||||||
|
if (desc == ""): desc = j["data"]["archive"]['desc']
|
||||||
|
if (source == ""): source = j["data"]["archive"]['source']
|
||||||
|
if (tid == ""): tid = j["data"]["archive"]['tid']
|
||||||
|
self.preUpload(parts)
|
||||||
|
self.editUpload(aid, title, tid, tag, desc, source, cover, no_reprint)
|
||||||
|
|
||||||
|
def editUpload(self,
|
||||||
|
aid,
|
||||||
|
title,
|
||||||
|
tid,
|
||||||
|
tag,
|
||||||
|
desc,
|
||||||
|
source='',
|
||||||
|
cover='',
|
||||||
|
no_reprint=1,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
:param aid: just aid
|
||||||
|
:type aid: int
|
||||||
|
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||||
|
:type parts: VideoPart or list<VideoPart>
|
||||||
|
:param title: video's title
|
||||||
|
:type title: str
|
||||||
|
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||||
|
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||||
|
:type tid: int
|
||||||
|
:param tag: video's tag
|
||||||
|
:type tag: list<str>
|
||||||
|
:param desc: video's description
|
||||||
|
:type desc: str
|
||||||
|
:param source: (optional) 转载地址
|
||||||
|
:type source: str
|
||||||
|
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||||
|
:type cover: str
|
||||||
|
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||||
|
:type no_reprint: int
|
||||||
|
"""
|
||||||
|
copyright = 2 if source else 1
|
||||||
|
r = self.session.post('https://member.bilibili.com/x/vu/web/edit?csrf=' + self.csrf,
|
||||||
|
json={
|
||||||
|
"aid": aid,
|
||||||
|
"copyright": copyright,
|
||||||
|
"source": source,
|
||||||
|
"title": title,
|
||||||
|
"tid": tid,
|
||||||
|
"tag": ','.join(tag),
|
||||||
|
"no_reprint": no_reprint,
|
||||||
|
"desc": desc,
|
||||||
|
"cover": cover,
|
||||||
|
"mission_id": 0,
|
||||||
|
"order_id": 0,
|
||||||
|
"videos": self.videos}
|
||||||
|
)
|
||||||
|
print(r.text)
|
||||||
|
for _p in self.files:
|
||||||
|
os.remove(_p.path)
|
||||||
|
|
||||||
def addChannel(self, name, intro=''):
|
def addChannel(self, name, intro=''):
|
||||||
"""
|
"""
|
||||||
@ -335,10 +436,10 @@ class Bilibili:
|
|||||||
r = self.session.post(
|
r = self.session.post(
|
||||||
url='https://space.bilibili.com/ajax/channel/addChannel',
|
url='https://space.bilibili.com/ajax/channel/addChannel',
|
||||||
data={
|
data={
|
||||||
'name' : name,
|
'name': name,
|
||||||
'intro': intro,
|
'intro': intro,
|
||||||
'aids' : '',
|
'aids': '',
|
||||||
'csrf' : self.csrf,
|
'csrf': self.csrf,
|
||||||
},
|
},
|
||||||
# name=123&intro=123&aids=&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497077610768
|
# name=123&intro=123&aids=&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497077610768
|
||||||
|
|
||||||
@ -360,7 +461,7 @@ class Bilibili:
|
|||||||
url='https://space.bilibili.com/ajax/channel/addVideo',
|
url='https://space.bilibili.com/ajax/channel/addVideo',
|
||||||
data={
|
data={
|
||||||
'aids': '%2C'.join(aids),
|
'aids': '%2C'.join(aids),
|
||||||
'cid' : cid,
|
'cid': cid,
|
||||||
'csrf': self.csrf
|
'csrf': self.csrf
|
||||||
}
|
}
|
||||||
# aids=9953555%2C9872953&cid=15814&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497079332679
|
# aids=9953555%2C9872953&cid=15814&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497079332679
|
||||||
@ -389,4 +490,3 @@ class Bilibili:
|
|||||||
# print(r.text)
|
# print(r.text)
|
||||||
# {"code":0,"data":{"url":"http://i0.hdslb.com/bfs/archive/67db4a6eae398c309244e74f6e85ae8d813bd7c9.jpg"},"message":"","ttl":1}
|
# {"code":0,"data":{"url":"http://i0.hdslb.com/bfs/archive/67db4a6eae398c309244e74f6e85ae8d813bd7c9.jpg"},"message":"","ttl":1}
|
||||||
return r.json()['data']['url']
|
return r.json()['data']['url']
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user