· 5 years ago · May 29, 2020, 05:26 AM
1#!/usr/bin/python
2from apiclient.discovery import build
3from apiclient.errors import HttpError
4from oauth2client.tools import argparser
5
6
7# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
8# tab of
9# https://cloud.google.com/console
10# Please ensure that you have enabled the YouTube Data API for your project.
11DEVELOPER_KEY = "FUCK YOU SLUT FAGGOT KEVIN"
12YOUTUBE_API_SERVICE_NAME = "youtube"
13YOUTUBE_API_VERSION = "v3"
14
15def youtube_search(options):
16 youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
17 developerKey=DEVELOPER_KEY)
18
19 # Call the search.list method to retrieve results matching the specified
20 # query term.
21 search_response = youtube.search().list(
22 q=options.q,
23 part="id,snippet",
24 maxResults=options.max_results
25 ).execute()
26
27 videos = []
28 channels = []
29 playlists = []
30
31 # Add each result to the appropriate list, and then display the lists of
32 # matching videos, channels, and playlists.
33 for search_result in search_response.get("items", []):
34 if search_result["id"]["kind"] == "youtube#video":
35 videos.append("%s (%s)" % (search_result["snippet"]["title"],
36 search_result["id"]["videoId"]))
37 elif search_result["id"]["kind"] == "youtube#channel":
38 channels.append("%s (%s)" % (search_result["snippet"]["title"],
39 search_result["id"]["channelId"]))
40 elif search_result["id"]["kind"] == "youtube#playlist":
41 playlists.append("%s (%s)" % (search_result["snippet"]["title"],
42 search_result["id"]["playlistId"]))
43
44 print ("Videos:\n"), "\n".join(videos), "\n"
45
46
47if __name__ == "__main__":
48 too_search = "Coolio" or "coolio"
49 argparser.add_argument("--q", help="Search term", default=too_search)
50 argparser.add_argument("--max-results", help="Max results", default=25)
51 args = argparser.parse_args()
52
53
54try:
55 youtube_search(args)
56except HttpError:
57 print ("An HTTP error %d occurred:\n%s") % (e.resp.status, e.content)