【ytmusic】基本操作

import requests
from ytmusicapi import YTMusic
import json

ytmusic = YTMusic("headers_auth.json")


#プレイリスト作成→曲検索→検索結果をプレイリストに登録
playlistId = ytmusic.create_playlist('test', 'test description')
search_results = ytmusic.search(query="colors 宇多田ヒカル")
print(search_results)

try:
ytmusic.add_playlist_items(playlistId, [search_results[0]['videoId']])
print(search_results[0]['videoId'])
except NameError: # 処理する例外の型が指定されている
print('検索結果なし!') # ここが実行される
except: # NameError 以外はここで処理
print('不明のエラー')

 

【検索の場合】

YTMusic.search(query: strfilter: str = Nonelimit: int = 20ignore_spelling: bool = False) → List[Dict[KT, VT]]

Search YouTube music Returns results within the provided category.

Parameters:
  • query – Query string, i.e. ‘Oasis Wonderwall’
  • filter – Filter for item types. Allowed values: songsvideosalbumsartistsplaylistscommunity_playlistsfeatured_playlistsuploads. Default: Default search, including all types of items.
  • limit – Number of search results to return Default: 20
  • ignore_spelling – Whether to ignore YTM spelling suggestions. If True, the exact search term will be searched for, and will not be corrected. This does not have any effect when the filter is set to uploads. Default: False, will use YTM’s default behavior of autocorrecting the search.

実際の実行方法

 

search_results = ytmusic.search(query="colors 宇多田ヒカル")

print(search_results)

 

実際の実行方法

search_results = ytmusic.search(query="colors 宇多田ヒカル",filter=None,limit=1)
#リミットが壊れているのか使えない。
search_results = ytmusic.search(query="colors 宇多田ヒカル",filter="videos",limit=0)

 

実際の実行方法

search_results = ytmusic.get_history()
print(search_results)