Last active
January 1, 2024 14:44
-
-
Save ozaki-r/343ebe0236e12871e8c716d451968972 to your computer and use it in GitHub Desktop.
Get items from the playlist of Your Likes via YouTube Data API (See https://developers.google.com/youtube/v3/quickstart/python?hl=ja)
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
#!/usr/bin/python | |
import httplib2 | |
import os | |
import sys | |
import json | |
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from google.auth.transport.requests import Request | |
# get_likes.py <outputfile> [<n_items>] | |
outputfile = sys.argv[1] | |
n = int(sys.argv[2]) if len(sys.argv) == 3 else 100 | |
# Path to your credential file | |
CLIENT_SECRETS_FILE = "client_secret.json" | |
SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'] | |
API_SERVICE_NAME = 'youtube' | |
API_VERSION = 'v3' | |
def get_authenticated_service(): | |
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) | |
credentials = flow.run_console() | |
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) | |
service = get_authenticated_service() | |
remains = n | |
next_page_token = '' | |
results = [] | |
while next_page_token is not None: | |
playlistitems_list_request = service.playlistItems().list( | |
playlistId='LM', # Playlist of Your Likes | |
part="snippet", | |
maxResults=n, | |
pageToken=next_page_token, | |
) | |
playlistitems_list_response = playlistitems_list_request.execute() | |
items = playlistitems_list_response["items"] | |
results.extend(items) | |
remains -= playlistitems_list_response["pageInfo"]["resultsPerPage"] | |
if remains <= 0: | |
break | |
next_page_token = playlistitems_list_response["nextPageToken"] | |
out = json.dumps(results) | |
with open(outputfile, 'w') as f: | |
f.write(out) |
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
require 'json' | |
require 'date' | |
json = ARGF.read | |
items = JSON.parse(json) | |
date20230101 = Date.parse('2023-01-01') | |
items.each_with_index do |item, i| | |
date = item["snippet"]["publishedAt"] | |
if Date.parse(date) < date20230101 | |
break | |
end | |
title = item["snippet"]["title"] | |
who = item["snippet"]["videoOwnerChannelTitle"].gsub(/ - Topic/, '') | |
desc = item["snippet"]["description"] | |
if desc =~ /Released on: ([\d\-]+)/ | |
release = $1 | |
print "#{i+1} #{title} by #{who}".ljust(50) | |
puts "at #{release}" | |
#puts "#{i+1},#{title},#{who},#{release}" | |
else | |
puts "#{i+1} #{title} by #{who}".ljust(50) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment