bilibili.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. function bilibili(packages) {
  2. const { axios, dayjs } = packages;
  3. const headers = {
  4. "user-agent":
  5. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
  6. accept: "*/*",
  7. "accept-encoding": "gzip, deflate, br",
  8. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  9. };
  10. let cookie;
  11. /** 获取cid */
  12. async function getCid(bvid, aid) {
  13. const params = bvid
  14. ? {
  15. bvid: bvid,
  16. }
  17. : {
  18. aid: aid,
  19. };
  20. const cidRes = (
  21. await axios.get("https://api.bilibili.com/x/web-interface/view?%s", {
  22. headers: headers,
  23. params: params,
  24. })
  25. ).data;
  26. return cidRes;
  27. }
  28. /** 格式化 */
  29. function durationToSec(duration) {
  30. if (typeof duration === "number") {
  31. return duration;
  32. }
  33. if (typeof duration === "string") {
  34. var dur = duration.split(":");
  35. return dur.reduce(function (prev, curr) {
  36. return 60 * prev + +curr;
  37. }, 0);
  38. }
  39. return 0;
  40. }
  41. const searchHeaders = {
  42. "user-agent":
  43. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
  44. accept: "application/json, text/plain, */*",
  45. "accept-encoding": "gzip, deflate, br",
  46. origin: "https://search.bilibili.com",
  47. "sec-fetch-site": "same-site",
  48. "sec-fetch-mode": "cors",
  49. "sec-fetch-dest": "empty",
  50. referer: "https://search.bilibili.com/",
  51. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  52. };
  53. async function getCookie() {
  54. if (!cookie) {
  55. cookie = await axios.get(
  56. "https://api.bilibili.com/x/frontend/finger/spi",
  57. { headers: searchHeaders }
  58. );
  59. }
  60. }
  61. const pageSize = 30;
  62. async function searchBase(keyword, page, searchType) {
  63. await getCookie();
  64. const params = {
  65. context: "",
  66. page: page,
  67. order: "",
  68. page_size: pageSize,
  69. keyword: keyword,
  70. duration: "",
  71. tids_1: "",
  72. tids_2: "",
  73. __refresh__: true,
  74. _extra: "",
  75. highlight: 1,
  76. single_column: 0,
  77. platform: 'pc',
  78. from_source: '',
  79. search_type: searchType,
  80. dynamic_offset: 0
  81. };
  82. const res = (
  83. await axios.get(
  84. "https://api.bilibili.com/x/web-interface/search/type",
  85. {
  86. headers: {
  87. ...searchHeaders,
  88. cookie: `buvid3=${cookie.b_3};buvid4=${cookie.b_4}`,
  89. },
  90. params: params,
  91. }
  92. )
  93. ).data;
  94. return res.data;
  95. }
  96. async function searchAlbum(keyword, page) {
  97. const resultData = await searchBase(keyword, page, 'video');
  98. const albums = resultData.result.map(function (result) {
  99. return {
  100. id:
  101. result["bvid"] !== null && result["bvid"] !== void 0
  102. ? result["bvid"]
  103. : result["aid"],
  104. aid: result.aid,
  105. bvid: result.bvid,
  106. artist: result.author,
  107. title:
  108. result.title === null || result.title === void 0
  109. ? void 0
  110. : result.title.replace(/(\<em(.*?)\>)|(\<\/em\>)/g, ""),
  111. album:
  112. result["bvid"] !== null && result["bvid"] !== void 0
  113. ? result["bvid"]
  114. : result["aid"],
  115. artwork: "http:".concat(result.pic),
  116. description: result.description,
  117. duration: durationToSec(result.duration),
  118. tags:
  119. result.tag === null || result.tag === void 0
  120. ? void 0
  121. : result.tag.split(","),
  122. date: dayjs.unix(result.pubdate).format("YYYY-MM-DD"),
  123. };
  124. })
  125. return {
  126. isEnd: resultData.numResults <= page * pageSize,
  127. data: albums
  128. }
  129. }
  130. async function searchArtist(keyword, page) {
  131. const resultData = await searchBase(keyword, page, 'bili_user');
  132. const artists = resultData.result.map(result => ({
  133. name: result.uname,
  134. id: result.mid,
  135. fans: result.fans,
  136. description: result.usign,
  137. avatar: result.upic,
  138. worksNum: result.videos
  139. }));
  140. return {
  141. isEnd: resultData.numResults <= page * pageSize,
  142. data: artists
  143. }
  144. }
  145. async function getArtistWorks(artistItem, page, type) {
  146. if (type !== 'album') {
  147. return;
  148. }
  149. const queryHeaders = {
  150. "user-agent":
  151. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
  152. accept: "application/json, text/plain, */*",
  153. "accept-encoding": "gzip, deflate, br",
  154. origin: "https://space.bilibili.com",
  155. "sec-fetch-site": "same-site",
  156. "sec-fetch-mode": "cors",
  157. "sec-fetch-dest": "empty",
  158. referer: `https://space.bilibili.com/${artistItem.id}/video`,
  159. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  160. };
  161. await getCookie();
  162. const params = {
  163. mid: artistItem.id,
  164. ps: 30,
  165. tid: 0,
  166. pn: page,
  167. order: 'pubdate',
  168. jsonp: 'jsonp'
  169. };
  170. const res = (
  171. await axios.get(
  172. "https://api.bilibili.com/x/space/arc/search",
  173. {
  174. headers: {
  175. ...queryHeaders,
  176. cookie: `buvid3=${cookie.b_3};buvid4=${cookie.b_4}`,
  177. },
  178. params: params,
  179. }
  180. )
  181. ).data;
  182. const resultData = res.data;
  183. const albums = resultData.list.vlist.map(function (result) {
  184. return {
  185. id:
  186. result["bvid"] !== null && result["bvid"] !== void 0
  187. ? result["bvid"]
  188. : result["aid"],
  189. aid: result.aid,
  190. bvid: result.bvid,
  191. artist: result.author,
  192. title:
  193. result.title === null || result.title === void 0
  194. ? void 0
  195. : result.title.replace(/(\<em(.*?)\>)|(\<\/em\>)/g, ""),
  196. album:
  197. result["bvid"] !== null && result["bvid"] !== void 0
  198. ? result["bvid"]
  199. : result["aid"],
  200. artwork: result.pic,
  201. description: result.description,
  202. duration: durationToSec(result.length),
  203. tags:
  204. result.tag === null || result.tag === void 0
  205. ? void 0
  206. : result.tag.split(","),
  207. date: dayjs.unix(result.created).format("YYYY-MM-DD"),
  208. }
  209. })
  210. return {
  211. isEnd: resultData.page.pn * resultData.page.ps >= resultData.page.count,
  212. data: albums
  213. }
  214. }
  215. return {
  216. platform: "bilibili",
  217. appVersion: ">=0.0",
  218. version: '0.0.1',
  219. defaultSearchType: 'album',
  220. cacheControl: 'no-cache',
  221. srcUrl: 'https://gitee.com/maotoumao/MusicFreePlugins/raw/master/bilibili.js',
  222. primaryKey: ['id', 'aid', 'bvid', 'cid'],
  223. async search(keyword, page, type) {
  224. if (type === 'album') {
  225. return await searchAlbum(keyword, page);
  226. }
  227. if (type === 'artist') {
  228. return await searchArtist(keyword, page);
  229. }
  230. },
  231. async getMediaSource(musicItem) {
  232. let cid = musicItem.cid;
  233. if (!cid) {
  234. cid = (await getCid(musicItem.bvid, musicItem.aid)).data.cid;
  235. }
  236. const _params = musicItem.bvid
  237. ? {
  238. bvid: musicItem.bvid,
  239. }
  240. : {
  241. aid: musicItem.aid,
  242. };
  243. const res = (
  244. await axios.get("https://api.bilibili.com/x/player/playurl", {
  245. headers: headers,
  246. params: { ..._params, cid: cid, fnval: 16 },
  247. })
  248. ).data;
  249. let url;
  250. if (res.data.dash) {
  251. url = res.data.dash.audio[0].baseUrl;
  252. } else {
  253. url = res.data.durl[0].url;
  254. }
  255. const hostUrl = url.substring(url.indexOf("/") + 2);
  256. const _headers = {
  257. "user-agent":
  258. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
  259. accept: "*/*",
  260. host: hostUrl.substring(0, hostUrl.indexOf("/")),
  261. "accept-encoding": "gzip, deflate, br",
  262. connection: "keep-alive",
  263. referer: "https://www.bilibili.com/video/".concat(
  264. (musicItem.bvid !== null && musicItem.bvid !== undefined
  265. ? musicItem.bvid
  266. : musicItem.aid) ?? ""
  267. ),
  268. };
  269. return {
  270. url: url,
  271. headers: _headers,
  272. };
  273. },
  274. async getAlbumInfo(albumItem) {
  275. const cidRes = await getCid(albumItem.bvid, albumItem.aid);
  276. const _ref2 =
  277. cidRes.data !== null && cidRes.data !== void 0 ? cidRes.data : {};
  278. const cid = _ref2.cid;
  279. const pages = _ref2.pages;
  280. let musicList;
  281. if (pages.length === 1) {
  282. musicList = [{ ...albumItem, cid: cid }];
  283. } else {
  284. musicList = pages.map(function (_) {
  285. return {
  286. ...albumItem,
  287. cid: _.cid,
  288. title: _.part,
  289. duration: durationToSec(_.duration),
  290. id: _.cid,
  291. };
  292. });
  293. }
  294. return {
  295. ...albumItem,
  296. musicList
  297. };
  298. },
  299. getArtistWorks
  300. };
  301. }