freesound.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. function freesound(packages) {
  2. const {axios, cheerio} = packages;
  3. async function search(query, page, type) {
  4. if(type === 'music') {// 我们能搜索的只有音乐,因此判断下类型
  5. // 获取网站的html
  6. const rawHtml = (await axios.get('https://freesound.org/search', {
  7. q: query,
  8. page
  9. })).data
  10. // 接下来解析html
  11. const $ = cheerio.load(rawHtml);
  12. // 存储搜索结果
  13. const searchResults = [];
  14. // 找到所有的音频元素
  15. const audioElements = $('.sample_player_small');
  16. // 解析每一个音频元素
  17. audioElements.each((index, element) => {
  18. // id
  19. const id = $(element).attr('id');
  20. // 音频名
  21. const title = $(element).find('.sound_filename').children('a').attr('title');
  22. // 封面
  23. const artwork = $(element).find('.background').css('background-url');
  24. // 作者
  25. const artist = $(element).find('.sample_information').children('.user').text();
  26. // 专辑名,这里就随便写个了,不写也没事
  27. const album = '来自FreeSound的音频';
  28. // 源mp3
  29. const url = $(element).find('.metadata').children('.mp3_file').attr('href');
  30. // 接下来拼装成musicItem格式,并拼接结果中
  31. searchResults.push({
  32. id,
  33. title,
  34. artist,
  35. artwork,
  36. album,
  37. yourName: url
  38. })
  39. });
  40. // 总页码
  41. const totalPage = parseInt($('.pagination').children('.last-page').text().trim());
  42. // 最后,按照search方法的返回结果格式返回就好了
  43. return {
  44. isEnd: page >= totalPage, // 当当前页码比总页码更多时,搜索结束
  45. data: searchResults // 本页的搜索结果
  46. }
  47. }
  48. }
  49. function getMediaSource(musicItem) {
  50. return {
  51. url: musicItem.yourName
  52. }
  53. }
  54. return {
  55. platform: 'FreeSound', // 插件名
  56. version: '0.0.0', // 版本号
  57. srcUrl: 'https://gitee.com/maotoumao/MusicFreePlugins/raw/master/freesound.js', // 更新链接;当在app更新插件后会自动用此地址的插件覆盖
  58. cacheControl: 'no-store', // 我们可以直接解析出musicItem的结构,因此选取no-store就好了,当然也可以不写这个字段
  59. search, // 在这里写上search方法,此时插件就会出现在搜索结果页了
  60. getMediaSource, // 由于搜索结果中没有了url字段,需要指定如何获取音源
  61. }
  62. }