-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny.py
More file actions
110 lines (91 loc) · 3.66 KB
/
tiny.py
File metadata and controls
110 lines (91 loc) · 3.66 KB
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
import os
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import re
import requests
import time
import sys
"""
给所有下载的文件夹添加tag文件, 记录更多信息便于辨认
为b站下载重新命名
"""
# 得到bilibili视频名称和分p名称
def bilibili_namer(bili_url):
"""
Find out the real video name
"""
request_timeout = 60
title = ""
subtitle=""
if "www.bilibili.com/video/av" in bili_url:
start_time = time.time()
while True:
try:
r = requests.get(bili_url)
break
except requests.exceptions.ConnectionError:
if time.time() - start_time > request_timeout:
print("bilibili_namer timeout: unable to connect after %s s" % request_timeout)
return [title,subtitle]
else:
time.sleep(1)
soup = BeautifulSoup(r.text,"lxml")
title = soup.findAll('div',{'class':'v-title'})[0].text
if soup.findAll('option'):
if re.search(r'.*/video/av(\d+)(/)?$', bili_url):
bili_url = (bili_url + '/index_1.html' if re.search(r'av(\d+)$', bili_url)
else bili_url + 'index_1.html')
p_str = urlparse(bili_url).path # get /video/av/index.html
search = soup.findAll('option',{"value":p_str})
if search:
subtitle = soup.findAll('option',{"value":p_str})[0].text[2:]
else:
print("%s out of range. 无效链接" % p_str)
return [title,subtitle]
return [title,subtitle]
else:
print("Not valid bilibili video link")
return [title,subtitle]
# 给所有已下载的b站视频添加info log,记录名称、分p名称、网址
def log_all(inputfile):
input_file = open(inputfile,'r')
URLs = []
URLs.extend([x for x in input_file.read().splitlines() if x])
for uri in URLs:
print(uri)
rtitle,rsub_title = bilibili_namer(uri)
newtitle = rtitle + ' ' + rsub_title if rsub_title else rtitle
old_dir = os.getcwd() + os.path.sep + "downloaded" + os.path.sep + rtitle
if os.path.isdir(old_dir):
log_path = old_dir + os.path.sep + "info.log"
if not os.path.isfile(log_path):
_log = open(log_path, 'w')
_log.write(rtitle + '\n')
_log.write(rsub_title + '\n')
_log.write(uri)
_log.close()
# 给已下载b站视频重新命名: "视频名称" <---> "视频名称 分P名称"
def rename_all(inputfile):
input_file = open(inputfile,'r')
URLs = []
URLs.extend([x for x in input_file.read().splitlines() if x])
for uri in URLs:
print(uri)
rtitle,rsub_title = bilibili_namer(uri)
newtitle = rtitle + ' ' + rsub_title if rsub_title else rtitle
old_dir = os.getcwd() + os.path.sep + "downloaded" + os.path.sep + rtitle
new_dir = os.getcwd() + os.path.sep + "downloaded" + os.path.sep + newtitle
if os.path.isdir(old_dir):
old_file = old_dir + os.path.sep + rtitle + '.ogg'
new_file = old_dir + os.path.sep + newtitle + '.ogg'
if os.path.isfile(new_file):
print("renaming %s > %s" % (old_file.split('/')[-1],new_file.split('/')[-1]))
#input()
os.rename(old_file,new_file)
print("renaming folder %s > %s" % (old_dir.split('/')[-1], new_dir.split('/')[-1]))
#input()
os.rename(old_dir, new_dir)
if __name__ == "__main__":
list_file = "listaa"
# rename_all(list_file)
log_all(list_file)