Last active
April 2, 2017 04:24
-
-
Save sizumita/2e61ffd02532828c22a730410e613b00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from googleapiclient.discovery import build | |
from googleapiclient.errors import HttpError | |
print('youtubeの動画を検索することができます!') | |
print('キーワードを入れてね!') | |
qe = input("->") | |
class SearchYoutube(object): | |
''' | |
Controll Youtube API | |
''' | |
__DEVELOPER_KEY = 'AIzaSyA8JE41VqRqYX2jtz07XqAYXuQk7hQcXps' | |
__YOUTUBE_API_SERVICE_NAME = "youtube" | |
__YOUTUBE_API_VERSION = "v3" | |
def search(self, options): | |
youtube = build( | |
self.__YOUTUBE_API_SERVICE_NAME, | |
self.__YOUTUBE_API_VERSION, | |
developerKey=self.__DEVELOPER_KEY | |
) | |
# Call the search.list method to retrieve results matching the specified | |
# query term. | |
search_response = youtube.search().list( | |
q=options["q"], | |
part=options["part"], | |
maxResults=options["maxResults"] | |
).execute() | |
videos = [] | |
channels = [] | |
playlists = [] | |
urls = [] | |
# Add each result to the appropriate list, and then display the lists of | |
# matching videos, channels, and 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"])) | |
urls.append("youtube.com/watch?v={}".format(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("\n" "\n".join(urls),"\n") | |
print("Channels:\n" "\n".join(channels) | |
,"\n") | |
print("Playlists:\n" "\n".join(playlists),"\n") | |
def sreach(self): | |
if __name__ == "__main__": | |
args = { | |
"q": self, | |
"part": "id,snippet", | |
"maxResults": 50 | |
} | |
try: | |
search_youtube = SearchYoutube() | |
search_youtube.search(args) | |
except HttpError as e: | |
print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)) | |
sreach(qe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment