-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassFunctions.php
166 lines (157 loc) · 6.89 KB
/
classFunctions.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* author FengMo,SnowWarri0r
* link http://Mo66.cn, https://www.onesnowwarrior.cn
*/
class BilibiliAnimeInfo
{
public $title = array(); //标题
public $image_url = array(); //图片链接
public $total = array(); //总集数
public $progress = array(); //我的进度
public $evaluate = array(); //介绍
public $season_id = array(); //ID号,用于番剧跳转
public $sum; //番剧数目
private $cookie; //b站cookie
// 这个是处理我观看记录的函数
private function process($content)
{
$start = stripos($content, "第");
if ($start) {
$end = stripos($content, "话");
return substr($content, $start + 3, $end - $start - 3);
} else {
$start = stripos($content, "到");
if ($start) {
return substr($content, $start + 3);
} else {
$start = stripos($content, "完");
if ($start) {
return substr($content, $start + 3);
} else {
return "没有记录!";
}
}
}
}
private function getpage($uid)
{
$url = "https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=1&ps=15&vmid=$uid";
$ch = curl_init(); //初始化curl模块
curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //这个很关键就是把获取到的数据以文件流的方式返回,而不是直接输出
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//发送请求头
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36",
"Referer: https://www.bilibili.com/",
));
$info = json_decode(curl_exec($ch), true);
curl_close($ch); //关闭连接
return $info['data']['total'];
}
public function __construct($uid, $cookie)
{
if (!file_exists(__DIR__ . '/json/data.json') or time() - filectime(__DIR__ . '/json/data.json') >= 86400) { //文件不存在或文件已过期
# 文件不存在或已过期
if (!is_dir(__DIR__ . '/json')) {
# 判断文件夹是否存在
mkdir(__DIR__ . '/json');
}
$this->sum = $this->getpage($uid, $cookie);
for ($i = 1; $i <= ceil($this->sum / 15); $i++) {
$url = "https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=$i&ps=15&vmid=$uid";
$ch = curl_init(); //初始化curl模块
curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //这个很关键就是把获取到的数据以文件流的方式返回,而不是直接输出
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//发送请求头
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36",
"Referer: https://www.bilibili.com/",
"Cookie: $cookie",
));
$info = json_decode(curl_exec($ch), true);
curl_close($ch); //关闭连接
foreach ($info['data']['list'] as $data) {
if (!empty($data['cover'])) {
$data['cover'] = str_replace('http://', 'https://', $data['cover']);
}
array_push($this->title, $data['title']);
array_push($this->image_url, $data['cover']);
array_push($this->total, $data['new_ep']['title']);
array_push($this->progress, $this->process($data['progress']));
array_push($this->evaluate, $data['evaluate']);
array_push($this->season_id, $data['season_id']);
}
}
$file_json_str = array(
'title' => $this->title,
'image_url' => $this->image_url,
'total' => $this->total,
'progress' => $this->progress,
'evaluate' => $this->evaluate,
'season_id' => $this->season_id,
'sum' => $this->sum,
); //用来保存要写入文件的数据
$file_json_str['progress'] = mb_convert_encoding($file_json_str['progress'], "utf-8");
$file_json_str = json_encode($file_json_str, JSON_UNESCAPED_UNICODE);
$f = fopen(__DIR__ . '/json/data.json', 'w'); //打开文件
fwrite($f, $file_json_str); //写入
fclose($f); //关闭文件
} else {
# 文件可用,直接读即可
$f = fopen(__DIR__ . '/json/data.json', 'r');
$info = json_decode(fread($f, filesize(__DIR__ . '/json/data.json')), true);
# 如果缓存为空,还是得获取
if (count($info['title']) === 0) {
$this->sum = $this->getpage($uid, $cookie);
for ($i = 1; $i <= ceil($this->sum / 15); $i++) {
$url = "https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=$i&ps=15&vmid=$uid";
$ch = curl_init(); //初始化curl模块
curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //这个很关键就是把获取到的数据以文件流的方式返回,而不是直接输出
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//发送请求头
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36",
"Referer: https://www.bilibili.com/",
"Cookie: $cookie",
));
$info = json_decode(curl_exec($ch), true);
curl_close($ch); //关闭连接
foreach ($info['data']['list'] as $data) {
if (!empty($data['cover'])) {
$data['cover'] = str_replace('http://', 'https://', $data['cover']);
}
array_push($this->title, $data['title']);
array_push($this->image_url, $data['cover']);
array_push($this->total, $data['new_ep']['title']);
array_push($this->progress, $this->process($data['progress']));
array_push($this->evaluate, $data['evaluate']);
array_push($this->season_id, $data['season_id']);
}
}
$file_json_str = array(
'title' => $this->title,
'image_url' => $this->image_url,
'total' => $this->total,
'progress' => $this->progress,
'evaluate' => $this->evaluate,
'season_id' => $this->season_id,
'sum' => $this->sum,
); //用来保存要写入文件的数据
$file_json_str['progress'] = mb_convert_encoding($file_json_str['progress'], "utf-8");
$file_json_str = json_encode($file_json_str, JSON_UNESCAPED_UNICODE);
$f = fopen(__DIR__ . '/json/data.json', 'w'); //打开文件
fwrite($f, $file_json_str); //写入
fclose($f); //关闭文件
} else {
$this->title = $info['title'];
$this->image_url = $info['image_url'];
$this->total = $info['total'];
$this->progress = $info['progress'];
$this->evaluate = $info['evaluate'];
$this->season_id = $info['season_id'];
$this->sum = $info['sum'];
}
}
}
}