2017-02-15 5 views
1

申し訳ありません、私は自分のサーバー用にDiscordにボットを作っています。実装したいものはyoutubeコマンドです。
私はYoutube APIを全面的に検索してきました。ブラウザのようなものを探しているだけです。Javascriptを使ったキーワード検索からYoutubeのビデオURLを返します

私はnodejsを使ってノートパソコンを起動しています。 disord of offord.js
私はMALとUrban Dictionaryの検索と同様のコマンドを持っていますが、私は何も見つけておらず、youtubeと同じようにする方法がわかりません。

私は以前からpython botこれが原因で可能だったと私はそれを行うことができる他のディスコボットも見たので、明らかに可能であることを知っている

基本的に私は言いたいのですがOの使用は、そのための最初の検索結果である、 https://www.youtube.com/watch?v=vTIIMJ9tUc8を返します

>>youtube Tunak Tunak Tun 

ようになりますので、検索語の文字列からユーチ​​ューブ動画のURL(最初の検索結果を)を検索し、戻ることができキーワード(複数可)

編集:私はこれを行うだろうPythonコマンドを見つけたが、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(アポロにこれを翻訳しようとするスキルや自信を近付いてきた
長さのためのgies):大丈夫!私はもっ​​と近づいた何かを見つけました! 私は今、それがこのようなものになり、私のボットでコマンド持っhttps://www.npmjs.com/package/youtube-search
:だから今、私は>>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]を取得

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); 
    }); 
} 

をチャネル。 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); 

EDIT

は、あなたの結果は実際には配列であることに気づくことができませんでした。次のようにアクセスする必要があります:console.log(results[0].link)JSON.parse()

+0

結果として私はgettting http://pastebin.com/yUr2Jy3になります。 私は今それについて何が噛んでいるのか分かりません。 これは、コードスニペットhttp://pastebin.com/qBiRhWXj – Paraxo

+0

@Paraxoに入力した方法と場所です。最初のペーストビンリンクが壊れているため、エラーは表示されず、コードのみが表示されます。 – jonofan

+0

@Paraxoがこの回答を更新しました。実際に解析する必要はありません。アレイのようにアクセスしてください。 – jonofan

0

これは、GoogleのJavaScript APIを使用して私のために働く別のアプローチです。あなたが設定する必要がありますので、もう一度、SOスニペットは、この方法では、私はフィドルからの私のGoogle APIキーを削除したenable youtube API access.

が、その後、setup a google API keyする必要がありますので、I'll link you to the fiddle.

それを実行しません。それが上です。あなたが最初にテストしたいのであれば、私はあなたの鉱山を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

関連する問題