Skip to content

Instantly share code, notes, and snippets.

@portothree
Created May 28, 2019 02:40
Show Gist options
  • Save portothree/47a8b28970d5b21770e2ae8bfc1a86c8 to your computer and use it in GitHub Desktop.
Save portothree/47a8b28970d5b21770e2ae8bfc1a86c8 to your computer and use it in GitHub Desktop.
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
import pprint
pp = pprint.PrettyPrinter(indent=4)
DEVELOPER_KEY = ""
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def youtube_search(q, max_results=50,order="relevance", token=None, location=None, location_radius=None):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=q,
type="video",
pageToken=token,
order = order,
part="id,snippet",
maxResults=max_results,
location=location,
locationRadius=location_radius
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)
def geo_query(video_id):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
video_response = youtube.videos().list(
id=video_id,
part='snippet, recordingDetails, statistics'
).execute()
return video_response
tests = youtube_search("Bolacha")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment