-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_data.py
117 lines (100 loc) · 3.89 KB
/
main_data.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding = utf-8 -*-
# @Python : 3.8
import re
import sys
import time
from loguru import logger
def get_data(path):
with open(path + "data.csv", "r", encoding="utf-8-sig") as fl:
lines = [l.replace("\n", "").split(",") for l in fl.readlines()]
return lines
def time_md(_time: str):
if len(_time) > 10:
timeStamp = int(int(_time) / 1000)
else:
timeStamp = int(_time)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("## %Y年%m月%d日 %H:%M:%S", timeArray)
# ## 2020年11月23日 20:16:20
# print(otherStyleTime)
return otherStyleTime
def title_md(_time: str, type) -> str:
return f"{_time} Type:{type}\r\n"
def pic_md(pic_str: str) -> str:
pics = pic_str.replace("'", "").replace("[", "").replace("]", "").split("&")
# print("pics urls: ", pics)
if len(pics) == 0:
return None
text = ""
for pic in pics:
name = pic.split("/")[-1]
if "?" in name:
name = name.split("?")[0]
if name == "":
continue
text += f"![{name}]({img_path + name}) "
return text
def content_md(text, pic_content) -> str:
return f"{text}\r\n{pic_content}\r\n"
def id_url_md(id, type) -> str:
if type == "img" or type == "text" or type == "decorate" or type == "reprint":
url = f"https://t.bilibili.com/{id}"
text = f"[点击直达动态]({url})"
elif type == 'av':
url = f"https://b23.tv/av{id}"
text = f"[点击直达视频]({url})"
elif type == "cv":
url = f"https://www.bilibili.com/read/cv{id}"
text = f"[点击直达专栏]({url})"
elif type == "au":
url = f"https://www.bilibili.com/audio/au{id}"
text = f"[点击直达音乐]({url})"
else:
text = ""
logger.error(type, " ", id) # exit()
return text + "\r\n"
if __name__ == '__main__':
UID = 351609538
if len(sys.argv) != 1:
UID = sys.argv[1]
logger.add(sink=f"./data/log_data_{UID}.log", format="{level} - {time} - {message}", level="DEBUG",
encoding="utf-8")
logger.info("日志记录开始")
logger.success(f"* 已获取参数 <目标UID:{UID}>")
else:
logger.add(sink=f"./data/log_data_{UID}.log", format="{level} - {time} - {message}", level="DEBUG",
encoding="utf-8")
logger.info("日志记录开始")
logger.warning(f"* 未获取到外置参数,使用内置参数 <目标UID:{UID}>")
logger.info('* Usage: python main_data.py <UID>')
# example1: python main_get.py 1111,22222,333333 0 #uids:[1111,22222,333333] download_img?:False''')
time.sleep(3)
base_path = f"./data/{UID}/"
img_path = "./img/"
md_path = base_path + "data.md"
with open(md_path, "w", encoding="utf8") as f:
f.write(f"# UID:{UID} 动态留档\r\n动态页面:[点此直达](https://space.bilibili.com/{UID}/dynamic)\r\n")
datas = get_data(base_path)
for data in datas:
# print(data)
time_ = time_md(data[0])
title = title_md(time_, data[4])
logger.info(title.replace("\r\n", ""))
pic_data = pic_md(data[2])
content = data[1].replace("\\n", "\n") + "\r\n"
find_tag = re.compile("#.+?#")
tags = re.findall(find_tag, content)
for tag in tags:
content = content.replace(tag, f'Tag:{tag.replace("#", "")} ')
if pic_data is None:
content = content
else:
content = content_md(content, pic_data)
click_url = id_url_md(data[3], data[4])
# print(click_url)
# print(content)
with open(md_path, "a+", encoding="utf8") as f:
f.write("\r\n" + title)
f.write(click_url)
f.write(content)
logger.success("Done!")