Skip to content

Instantly share code, notes, and snippets.

@ogrew
Created March 16, 2023 12:59
Show Gist options
  • Save ogrew/4804663fcf1318b999cdf758617821ed to your computer and use it in GitHub Desktop.
Save ogrew/4804663fcf1318b999cdf758617821ed to your computer and use it in GitHub Desktop.
# 必要なライブラリをインポート
import sys
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# Google APIキーを指定
api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
# 動画タイトルを取得する関数
def get_channel_videos(channel_id, num):
# YouTubeのAPIを使用するための準備
youtube = build('youtube', 'v3', developerKey=api_key)
# 動画の情報を取得するためのリクエスト
request = youtube.search().list(
part='id,snippet',
channelId=channel_id,
type='video',
maxResults=num,
order='date'
)
# 動画の情報を取得
response = request.execute()
items = response['items']
for item in items:
title = item['snippet']['title']
if 'ジャルジャルのネタのタネ' in title:
title = title.replace('ジャルジャルのネタのタネ', '')
title = title.split('【JARUJARUTOWER】')[0]
title = title.replace('『', '').replace('』', '') # この行を追加
print(title)
# 引数から取得する動画タイトル数を指定
if len(sys.argv) > 1:
num = int(sys.argv[1])
else:
num = 10
# ジャルジャル公式YouTubeチャンネルのIDを指定
channel_id = 'UChwgNUWPM-ksOP3BbfQHS5Q'
# 動画タイトルを取得して表示
try:
get_channel_videos(channel_id, num)
except HttpError as e:
print('An error occurred: %s' % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment