2017-02-15 87 views
1

好的,所以我在Discord中爲我的服務器製作了一個bot,而我想實現的是一個youtube命令。
我一直在尋找一切,看着Youtube的API中,所有我能找到的是他們對什麼似乎像一個瀏覽器用Javascript關鍵字搜索返回Youtube視頻網址

我使用到的NodeJS運行它關閉我的筆記本電腦的搜索,我的機器人運行關閉discord.js
我有一個類似的命令,做一個MAL和一個城市詞典搜索,但我什麼都沒發現,也不知道如何做與YouTube相同

我曾經有一個命令蟒蛇機器人,因爲這一點,我見過其他Discord機器人也能做到這一點,所以我知道這顯然是可能的

基本上我說的是我需要噸Ø能夠從搜索項的字符串搜索並返回一個YouTube視頻URL(第一搜索結果),所以使用看起來像

>>youtube Tunak Tunak Tun 

將返回 https://www.youtube.com/watch?v=vTIIMJ9tUc8,這對於第一個搜索結果關鍵字

編輯:
我發現蟒蛇命令,將做到這一點,但有親近的技能,也沒有信心去嘗試將此翻譯成JavaScript

elif prefix and cmd=="youtube" and len(args) > 0: 
     try: 
      yword=args.replace(" ","_") 
      ydata= urlreq.urlopen("http://gdata.youtube.com/feeds/api/videos?vq="+yword+"&racy=include&orderby=relevance&max-results=1") 
      yread= str(ydata.read()) 
      if "<openSearch:totalResults>0</openSearch:totalResults>" in yread: 
       room.message("I got nothin' for ya by the name of "+args) 
      else: 
       trash , yclean=yread.split("<media:player url='http://www.youtube.com/watch?v=",1) 
       yclean , trash=yclean.split("&amp;",1) 
       room.message("http://http://www.youtube.com/watch?v="+yclean,True) 
     except: 
      room.message("Somethin ain't right") 

EDIT2(阿波羅長度):好的!我發現了讓我更加接近的東西! https://www.npmjs.com/package/youtube-search
我在我的機器人得到了命令,現在是這樣的:

if (commandIs("yt" , message)){ 
    search(args.join(' ').substring(4), opts, function(err, results) { 
    if(err) return console.log(err); 
    message.channel.sendMessage(results); 
    console.log(results); 
    }); 
} 

所以現在當我進入>>yt Tunak Tunak Tun我得到

[ { id: 'vTIIMJ9tUc8', 
link: 'https://www.youtube.com/watch?v=vTIIMJ9tUc8', 
kind: 'youtube#video', 
publishedAt: '2014-03-21T07:00:01.000Z', 
channelId: 'UC3MLnJtqc_phABBriLRhtgQ', 
channelTitle: 'SonyMusicIndiaVEVO', 
title: 'Daler Mehndi - Tunak Tunak Tun Video', 
description: 'Presenting \'Tunak Tunak Tun\' music video sung by the talented Daler Mehndi Song Name - Tunak Tunak Tun Album - Tunak Tunak Tun Singer - Daler Mehndi ...', 
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ] 
在控制檯

[object Object]的不和諧渠道。 http://i.imgur.com/Vorpn0f.png

所以,現在的問題是我在我到達的鏈接,但我不能讓它返回JUST的鏈接,我不知道如何把它拉出來那些亂七八糟的。

+0

嘗試console.log(results.link) – jonofan

+0

@jonofan undefined – Paraxo

回答

1

這聽起來像你的結果對象是JSON字符串。這基本上意味着它是一個javascript對象的字符串表示。您可以使用JSON.parse()將此解析爲對象。

var objResults = JSON.parse(results); 
console.log(objResults); 
console.log(objResults.link); 

編輯

沒有注意到,你的結果實際上是一個數組。你只需要像這樣訪問它:console.log(results[0].link)。應該不需要JSON.parse()

+0

因此,我最終得到http://pastebin.com/yUr2Jy3。 我甚至都不知道現在是什麼滋味。 這是怎麼和我在哪裏進入你的片段http://pastebin.com/qBiRhWXj – Paraxo

+0

@Paraxo第一個pastebin鏈接被破壞,所以看不到你的錯誤,只能看你的代碼。 – jonofan

+0

@Paraxo已經更新了這個答案,你實際上不需要解析它。只需像數組一樣訪問它。 – jonofan

0

好的,這裏有另一種方法,使用google javascript API爲我工作。再次,SO片段不運行它,所以I'll link you to the fiddle.

這種方法需要你setup a google API key,然後enable youtube API access.

我已經移除撥弄我的谷歌API密鑰,所以你需要設置那個。如果你想先測試,我可以PM你。

var apiKey = null //put your API key here 
 

 
function search() { 
 
\t var searchTerm = $('#txtSearch').val() 
 
    
 
    gapi.client.init({ 
 
    'apiKey': apiKey, 
 
    'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'] 
 
    }).then(function() { 
 
    return gapi.client.youtube.search.list({ 
 
     q: searchTerm, 
 
     part: 'snippet' 
 
    }); 
 
    }).then(function(response) { 
 
    \t var searchResult = response.result; 
 
    $('#search-results').append(JSON.stringify(searchResult, null, 4)) 
 
    \t console.log(searchResult.items[0]) 
 
    var firstVideo = searchResult.items[0] 
 
    firstVideo.url = `https://youtube.com/watch?v=${firstVideo.id.videoId}` 
 
    $('#first-video').text(firstVideo.url).attr('href', firstVideo.url) 
 
    $('#first-video-title').text(firstVideo.snippet.title) 
 
    $('#first-video-description').text(firstVideo.snippet.description) 
 
    }); 
 

 
} 
 

 

 
$('#btnSearch').on('click', function() { 
 
    \t $('#first-video-title').text("") 
 
    if (!apiKey) { 
 
     $('#first-video-title').text("You need to set an apiKey!") 
 
     return; 
 
    } 
 
    \t gapi.load('client', search) 
 
    });
#search-results { white-space: pre; font-family: monospace; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src='https://apis.google.com/js/api.js'></script> 
 

 
<div id="container"> 
 
    <input id="txtSearch" type="text" /> 
 
    <button id="btnSearch"> 
 
    Search! 
 
    </button> 
 
    <br /> 
 
    <p id='first-video-title'> </p> 
 
    <p id='first-video-description'></p> 
 
    <a target="_blank" id="first-video"></a> 
 
    <div id='search-results'> 
 
    
 
    </div> 
 
</div>

+0

對於任何遇到此問題並需要瀏覽器中的某些內容的人,這實際上工作得很好。 – Paraxo