Skip to content

Instantly share code, notes, and snippets.

@computermacgyver
Created March 14, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save computermacgyver/af96281bce7b8abf44567ba0bd9a5f35 to your computer and use it in GitHub Desktop.
Save computermacgyver/af96281bce7b8abf44567ba0bd9a5f35 to your computer and use it in GitHub Desktop.
Search a YouTube channel for videos within a specific date range.
#!/usr/bin/python3
#Need
# https://github.com/googleapis/oauth2client
# https://github.com/googleapis/google-api-python-client
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
import json
import time
import os
import datetime
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
# https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = ""
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
EMAIL = ""
"""
https://developers.google.com/youtube/v3/docs/search/list
maxResults=50
channelId=
order=date
publishedAfter=2010-01-01T00:00:00Z
publishedBefore=2010-01-02T00:00:00Z
type=video
videoDuration=long (only videos over 20 minutes -- would we want this?)
"""
params={
"channelId":"",
"part":"id,snippet",
"maxResults":50,
"order":"date", #Resources are sorted in reverse chronological order based on the date they were created.
"safeSearch":"none",
"type":"video",
"publishedAfter":"2015-01-01T00:00:00Z",
"publishedBefore":"2016-01-01T00:00:00Z"
}
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
d=datetime.date.today()
outputDir = "output_%i-%02d-%02d/"%(d.year,d.month,d.day)
os.system("mkdir -p %s"%(outputDir)) #Create directory if does not exist
result = youtube.search().list(
channelId=params["channelId"],
part=params["part"],
maxResults=params["maxResults"],
order=params["order"],
publishedAfter=params["publishedAfter"],
publishedBefore=params["publishedBefore"],
safeSearch=params["safeSearch"],
type=params["type"]
).execute()
#write to file
timestamp = int(time.time())
fh = open(outputDir+("%s.json"%timestamp),"w")
fh.write(json.dumps(result))
fh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment