如何修复返回编码的 Youtube API 结果标题 [英] How to fix Youtube API results title that are returned encoded

查看:21
本文介绍了如何修复返回编码的 Youtube API 结果标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 youtube-search 1.1.4 来查找视频.问题是我得到的结果标题用 &' 而不是 & 编码>' 等等.

I'm using youtube-search 1.1.4 to find videos. The problem is that i get the results titles encoded with & or ' instead of just & and ' and more.

我从下面的示例代码中得到的一个结果示例(再次,有意在字符之间添加空格):title: "Post Malone - "Wow."(官方音乐视频)"

example of one result i got from the example code below (again, added spaces between characters intentionally): title: "Post Malone - "Wow." (Official Music Video)"

尝试通过 decodeURI 、decodeURIComponent 或 unescape 解决这个问题,但没有帮助.直接调用 youtube api 并得到相同的结果.我错过了什么?

Tried solving this by decodeURI ,decodeURIComponent or unescape which didn't help. Used a direct call for youtube api and got the same results. What am i missing?

var youtubeSearch = require("youtube-search")

var opts = {
  maxResults  : 15,
  key         : 'MY_API_KEY',
  part        : 'snippet',
  type        : 'video',
};

youtubeSearch('post malone', opts, function(err, results) {
  if(err) return console.log(err);

  console.dir(results);
});

推荐答案

在 google's issue tracker 上找到相关工单后:issuetracker.google.com/u/1/issues/128673539 并获得了谷歌的回应是这是预期的行为,他们不会修复它,我只是使用了用户 3limin4t0r 建议并解码了使用 he.js 库返回值标题,这是解决此问题的空闲方法,但我无意等待谷歌做出决定...

After finding a related ticket on google's issue tracker: issuetracker.google.com/u/1/issues/128673539 and got a response from google that this is the expected behaviour and they won't fix it, i just used user 3limin4t0r suggestion and decoded the return value title using he.js library, it's the idle way to solve this but i had no intention to wait for google to come around from their decision...

所以,我的解决方案是这样的:

so, my solution goes like that:

var youtubeSearch = require("youtube-search")
let he            = require('he');

var opts = {
  maxResults  : 15,
  key         : 'MY_API_KEY',
  part        : 'snippet',
  type        : 'video',
};

youtubeSearch('post malone', opts, function(err, results) {
  if(err) return console.log(err);
  results = results.map(item => {
        item.snippet.title = he.decode(item.snippet.title);
        return item;
   });
  console.dir(results);
});

这篇关于如何修复返回编码的 Youtube API 结果标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆