基本的な質問は申し訳ありませんが、command-line
でyoutube api
コールを使用する例と、適切なメタデータフィールドを設定する方法をどこからでも探しています。Youtube-api ::コマンドラインでメタデータフィールドを設定する
以下のコードはここにGoogleから提供されていますSearch Youtube by 'keyword'
#!/usr/bin/python
import os
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
DEVELOPER_KEY = "mykey"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=options.q,
part="id,snippet",
maxResults=options.max_results
).execute()
videos = []
channels = []
playlists = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["videoId"]))
elif search_result["id"]["kind"] == "youtube#channel":
channels.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["channelId"]))
elif search_result["id"]["kind"] == "youtube#playlist":
playlists.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["playlistId"]))
print "Videos:\n", "\n".join(videos), "\n"
print "Channels:\n", "\n".join(channels), "\n"
print "Playlists:\n", "\n".join(playlists), "\n"
if __name__ == "__main__":
argparser.add_argument("--q", help="Search term", default="Google")
argparser.add_argument("--max-results", help="Max results", default=25)
args = argparser.parse_args()
try:
youtube_search(args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
search.py
私はここに挙げた例に続いてきた。例えば、カルマ警察ビデオのために、私が持っているYoutube>Data API>SampleRequests
検索を、したがって、試しました:
$ python script.py --q="karma police"
、利用できません。何も印刷しません。
私には何が欠けていますか?
PS。資格情報は、同じディレクトリにある環境.json file
に設定されています。
EDIT:トレースバック:
Traceback (most recent call last):
File "audio.py", line 64, in <module>
youtube_search(args)
File "audio.py", line 24, in youtube_search
developerKey=DEVELOPER_KEY)
File "/Library/Python/2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py", line 226, in build
credentials=credentials)
File "/Library/Python/2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/discovery.py", line 358, in build_from_document
credentials = _auth.default_credentials()
File "/Library/Python/2.7/site-packages/googleapiclient/_auth.py", line 41, in default_credentials
return oauth2client.client.GoogleCredentials.get_application_default()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1264, in get_application_default
return GoogleCredentials._get_implicit_credentials()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1249, in _get_implicit_credentials
credentials = checker()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1200, in _implicit_credentials_from_files
credentials_filename = _get_environment_variable_file()
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1348, in _get_environment_variable_file
' environment variable) does not exist!')
oauth2client.client.ApplicationDefaultCredentialsError: File “/Users//api/youtube/urlaudio/myproject.json” (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!
何も印刷されませんか? \ n "、" \ n ".join(ビデオ)、" \ n "' – hansaplast
実行の到達距離をチェックしましたか?例えば私は通常、 'print( '1')'コードを行間に追加して、実行がどこで停止するかを確認します。 – hansaplast
私のマシン上で動作します:-) [my computer上のスクリプトの出力](https://gist.github .com/philippkeller/cfbdaa202e121fb2b46a566479577d56) – hansaplast