diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000..e69de29b diff --git a/404.html b/404.html new file mode 100644 index 00000000..437072cf --- /dev/null +++ b/404.html @@ -0,0 +1,568 @@ + + + +
+ + + + + + + + + + + + + + + +All notable changes to this project will be documented in this file.
+for_handle
to get channel by handle. Client
to operate YouTube DATA API. #120.Detail at: https://developers.google.com/youtube/v3/revision_history#may-12,-2021
+Methods get_channel_sections_by_channel
, get_channel_section_by_id
has remove parameter hl
.
Modify the auth flow methods.
+Remove deprecated api.
+Provide remain get apis. like activities, captions, channel_sections, i18n, video_abuse_report_reason, search resource and so on.
+You can see the README
_ to get more detail for those api.
Provide the page token parameter to skip data have retrieved.
+This for follow api methods
+api.get_playlists()
+api.get_playlist_items()
+api.get_videos_by_chart()
+api.get_videos_by_myrating()
+api.get_comment_threads()
+api.get_comments()
+api.get_subscription_by_channel()
+api.get_subscription_by_me()
+
example
+In[1]: r = api.get_subscription_by_channel(channel_id="UCAuUUnT6oDeKwE6v1NGQxug", limit=5, count=None, page_token="CAUQAA")
+In[2]:r.prevPageToken
+Out[2]: 'CAUQAQ'
+
Now you can use authorized access token to get your subscriptions. +You can to the demo A demo for get my subscription to see simple usage. +Or you can see the subscriptions usage docs.
+#43 add api for get my subscriptions
+
+#41 add api for channel subscriptions
+
Now some apis can get all target items just by one method call.
+For example, you can get playlist's all items by follow call
+In [1]: r = api.get_playlist_items(playlist_id="PLWz5rJ2EKKc_xXXubDti2eRnIKU0p7wHd", parts=["id", "snippet"], count=None)
+In [2]: r.pageInfo
+Out[2]: PageInfo(totalResults=73, resultsPerPage=50)
+In [3]: len(r.items)
+Out[4]: 73
+
You can see the README to find which methods support this.
+Now introduce new model ApiResponse representing the response from youtube, so previous usage has been invalidated.
+You need to read the docs README to get the simple new usage.
+Split some method into multiple usage, for example get video has been split three methods:
+If you want to get some more data for your channel. You need provide the authorization first.
+So, this doc show how to do authorize.
+At the beginning. You must know what is authorization.
+You can see some information at the Official Documentation.
+Then you need have an app with the Access scopes approval by YouTube.
+If everything goes well. Now let do a simple authorize with Python-Youtube
library.
Suppose now we want to get user's permission to manage his youtube account.
+For the Python-YouTube
library, the default scopes are:
You can get more scope information at Access scopes.
+And We set the default redirect url is https://localhost/
.
Now we can begin do the follows step.
+Initialize the api instance with you app credentials
+In [1]: from pyyoutube import Client
+
+In [2]: cli = Client(client_id="you client id", client_secret="you client secret")
+
+In [3]: cli.get_authorize_url()
+Out[3]:
+('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=client_id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=PyYouTube&access_type=offline&prompt=select_account',
+ 'PyYouTube')
+
Now you get the authorization url, you just need copy the link, and open browser to paste the link, click the enter bar.
+If you enter the url. you will see this.
+ +Now you need to chose or enter you google account with youtube.
+If your app have not got the approval from youtube. You will get an warning from youtube. If you have been approved, you will +see the next image show directly.
+ +For now, you need to click the button Advanced
, then click the Go to Python-YouTube (unsafe)
.
Now you can get a window to give permissions.
+ +click the blue button allow
to give the permission.
Then you will get a Connection Error, don't worry. This just because we set the redirect link to localhost
.
Now you need to copy the full url in the browser address bar. Then back to you console.
+In [4]: token = cli.generate_access_token(authorization_response="the whole url")
+
+In [5]: token
+Out[5]: AccessToken(access_token='access token', expires_in=3600, token_type='Bearer')
+
now you have got your access token to visit your self data.
+For example, you can get your playlists.
+In [6]: playlists = cli.playlists.list(mine=True)
+
+In [7]: playlists.items
+Out[7]:
+[Playlist(kind='youtube#playlist', id='PLBaidt0ilCManGDIKr8UVBFZwN_UvMKvS'),
+ Playlist(kind='youtube#playlist', id='PLBaidt0ilCMbUdj0EppB710c_X5OuCP2g')]
+
Tips
+If you have some confuse. you need to read the Authorize Requests first.
+This doc is a simple tutorial to show how to use this library to get data from YouTube DATA API.
+You can get the whole description for YouTube API at YouTube API Reference.
+At the beginning. You need to create a Google Project by your google account.
+Every new account has 12 project to cost.
+Click the Select a project-> NEW PROJECT
to create a new project to use our library.
Fill the basic info to finish created.
+ +Once the project created, the browser will redirect project home page.
+Then click the ≡≡
symbol on the left top. Chose the APIs & Services
tab.
You will see follow info.
+ +Click the + ENABLE APIS AND SERVICES
symbol. And input YouTube DATA API
to search.
Then chose the YouTube DATA API
item.
Then click the ENABLE
blue button. Now the service has been activated.
To use this API, you may need credentials. Click 'Create credentials' to get started.
+ +You need to fill in some information to create credentials.
+Just chose YouTube DATA API v3
, Other non-UI (e.g. cron job, daemon)
and Public data
.
Then click the blue button What credentials do I need?
to create.
Now you have generated one api key.
+Use this key. You can retrieve public data for YouTube data by our library
+from pyyoutube import Client
+
+cli = Client(api_key="your api key")
+
If you want to get some examples to see, check out the examples.
+If you have an opens source application using python-youtube, send me a link, and I am very happy to add a link to it here.
+But if you want to get user data by OAuth. You need create the credential for OAuth client ID
.
And get more info at next page for Authorization.
+ + + + + + +A Python wrapper around for YouTube Data API.
+Author: IkarosKun merle.liukun@gmail.com
+With the YouTube Data API, you can add a variety of YouTube features to your application.
+Use the API to upload videos, manage playlists and subscriptions, update channel settings, and more.
+This library provides a Python interface for the YouTube DATA API.
+Library could work on Python 3.6+.
+Tips
+This library only supports DATA API
, It does not support Analytics and Reporting APIs
and Live Streaming API
.
This library supports Python 3.6 and newer.
+These following distributions will be installed automatically when installing Python-Youtube.
+You can install this library from PyPI
+$ pip install --upgrade python-youtube
+
Also, you can build this library from source code
+$ git clone https://github.com/sns-sdks/python-youtube.git
+$ cd python-youtube
+$ make env
+$ make build
+
If you have been installing the requirements use make env
.
+You can use following command to test the code
$ make tests-html
+
This doc will show you the new api structure for this library.
+To make the package easier to maintain and easy to use. We are shifted to using classes for different YouTube resources in an easier, higher-level programming experience.
+ +In this structure, every resource will have self class. And to operate with YouTube API.
+from pyyoutube import Client
+
+client = Client(api_key="your api key")
+
for example to get channel data.
+resp = client.channels.list(
+ parts=["id", "snippet"],
+ channel_id="UCa-vrCLQHviTOVnEKDOdetQ"
+)
+# resp output
+# ChannelListResponse(kind='youtube#channelListResponse')
+# resp.items[0].id output
+# UCa-vrCLQHviTOVnEKDOdetQ
+
A Python wrapper around for YouTube Data API.
Author: IkarosKun merle.liukun@gmail.com
"},{"location":"#introduction","title":"Introduction","text":"With the YouTube Data API, you can add a variety of YouTube features to your application.
Use the API to upload videos, manage playlists and subscriptions, update channel settings, and more.
This library provides a Python interface for the YouTube DATA API.
Library could work on Python 3.6+.
Tips
This library only supports DATA API
, It does not support Analytics and Reporting APIs
and Live Streaming API
.
All notable changes to this project will be documented in this file.
"},{"location":"CHANGELOG/#version-094-2024-02-18","title":"Version 0.9.4 (2024-02-18)","text":""},{"location":"CHANGELOG/#whats-new","title":"What's New","text":"for_handle
to get channel by handle. Client
to operate YouTube DATA API. #120.Detail at: https://developers.google.com/youtube/v3/revision_history#may-12,-2021
Methods get_channel_sections_by_channel
, get_channel_section_by_id
has remove parameter hl
.
Modify the auth flow methods.
"},{"location":"CHANGELOG/#whats-new_7","title":"What's New","text":"Remove deprecated api.
"},{"location":"CHANGELOG/#version-060","title":"Version 0.6.0","text":""},{"location":"CHANGELOG/#whats-new_10","title":"What's New","text":"Provide remain get apis. like activities, captions, channel_sections, i18n, video_abuse_report_reason, search resource and so on.
You can see the README
_ to get more detail for those api.
Provide the page token parameter to skip data have retrieved.
This for follow api methods
api.get_playlists()\napi.get_playlist_items()\napi.get_videos_by_chart()\napi.get_videos_by_myrating()\napi.get_comment_threads()\napi.get_comments()\napi.get_subscription_by_channel()\napi.get_subscription_by_me()\n
example
In[1]: r = api.get_subscription_by_channel(channel_id=\"UCAuUUnT6oDeKwE6v1NGQxug\", limit=5, count=None, page_token=\"CAUQAA\")\nIn[2]:r.prevPageToken\nOut[2]: 'CAUQAQ'\n
"},{"location":"CHANGELOG/#version-052","title":"Version 0.5.2","text":""},{"location":"CHANGELOG/#whats-new_12","title":"What's New","text":"Now you can use authorized access token to get your subscriptions. You can to the demo A demo for get my subscription to see simple usage. Or you can see the subscriptions usage docs.
#43 add api for get my subscriptions\n\n#41 add api for channel subscriptions\n
"},{"location":"CHANGELOG/#version-051","title":"Version 0.5.1","text":""},{"location":"CHANGELOG/#whats-new_13","title":"What's New","text":"Now some apis can get all target items just by one method call.
For example, you can get playlist's all items by follow call
In [1]: r = api.get_playlist_items(playlist_id=\"PLWz5rJ2EKKc_xXXubDti2eRnIKU0p7wHd\", parts=[\"id\", \"snippet\"], count=None)\nIn [2]: r.pageInfo\nOut[2]: PageInfo(totalResults=73, resultsPerPage=50)\nIn [3]: len(r.items)\nOut[4]: 73\n
You can see the README to find which methods support this.
"},{"location":"CHANGELOG/#version-050","title":"Version 0.5.0","text":""},{"location":"CHANGELOG/#broken-change_2","title":"Broken Change","text":"Now introduce new model ApiResponse representing the response from youtube, so previous usage has been invalidated.
You need to read the docs README to get the simple new usage.
"},{"location":"CHANGELOG/#whats-new_14","title":"What's New","text":"Split some method into multiple usage, for example get video has been split three methods:
If you want to get some more data for your channel. You need provide the authorization first.
So, this doc show how to do authorize.
"},{"location":"authorization/#prerequisite","title":"Prerequisite","text":"At the beginning. You must know what is authorization.
You can see some information at the Official Documentation.
Then you need have an app with the Access scopes approval by YouTube.
If everything goes well. Now let do a simple authorize with Python-Youtube
library.
Suppose now we want to get user's permission to manage his youtube account.
For the Python-YouTube
library, the default scopes are:
You can get more scope information at Access scopes.
And We set the default redirect url is https://localhost/
.
Now we can begin do the follows step.
Initialize the api instance with you app credentials
In [1]: from pyyoutube import Client\n\nIn [2]: cli = Client(client_id=\"you client id\", client_secret=\"you client secret\")\n\nIn [3]: cli.get_authorize_url()\nOut[3]:\n('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=client_id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=PyYouTube&access_type=offline&prompt=select_account',\n 'PyYouTube')\n
Now you get the authorization url, you just need copy the link, and open browser to paste the link, click the enter bar.
"},{"location":"authorization/#do-authorization","title":"Do authorization","text":"If you enter the url. you will see this.
Now you need to chose or enter you google account with youtube.
If your app have not got the approval from youtube. You will get an warning from youtube. If you have been approved, you will see the next image show directly.
For now, you need to click the button Advanced
, then click the Go to Python-YouTube (unsafe)
.
Now you can get a window to give permissions.
click the blue button allow
to give the permission.
Then you will get a Connection Error, don't worry. This just because we set the redirect link to localhost
.
Now you need to copy the full url in the browser address bar. Then back to you console.
In [4]: token = cli.generate_access_token(authorization_response=\"the whole url\")\n\nIn [5]: token\nOut[5]: AccessToken(access_token='access token', expires_in=3600, token_type='Bearer')\n
now you have got your access token to visit your self data.
"},{"location":"authorization/#get-your-data","title":"Get your data","text":"For example, you can get your playlists.
In [6]: playlists = cli.playlists.list(mine=True)\n\nIn [7]: playlists.items\nOut[7]:\n[Playlist(kind='youtube#playlist', id='PLBaidt0ilCManGDIKr8UVBFZwN_UvMKvS'),\n Playlist(kind='youtube#playlist', id='PLBaidt0ilCMbUdj0EppB710c_X5OuCP2g')]\n
Tips
If you have some confuse. you need to read the Authorize Requests first.
"},{"location":"getting_started/","title":"Getting Started","text":"This doc is a simple tutorial to show how to use this library to get data from YouTube DATA API.
You can get the whole description for YouTube API at YouTube API Reference.
"},{"location":"getting_started/#prerequisite","title":"Prerequisite","text":"At the beginning. You need to create a Google Project by your google account.
Every new account has 12 project to cost.
"},{"location":"getting_started/#create-your-project","title":"Create your project","text":"Click the Select a project-> NEW PROJECT
to create a new project to use our library.
Fill the basic info to finish created.
"},{"location":"getting_started/#enable-youtube-data-api-service","title":"Enable YouTube DATA API service","text":"Once the project created, the browser will redirect project home page.
Then click the \u2261\u2261
symbol on the left top. Chose the APIs & Services
tab.
You will see follow info.
Click the + ENABLE APIS AND SERVICES
symbol. And input YouTube DATA API
to search.
Then chose the YouTube DATA API
item.
Then click the ENABLE
blue button. Now the service has been activated.
To use this API, you may need credentials. Click 'Create credentials' to get started.
You need to fill in some information to create credentials.
Just chose YouTube DATA API v3
, Other non-UI (e.g. cron job, daemon)
and Public data
.
Then click the blue button What credentials do I need?
to create.
Now you have generated one api key.
Use this key. You can retrieve public data for YouTube data by our library
from pyyoutube import Client\n\ncli = Client(api_key=\"your api key\")\n
If you want to get some examples to see, check out the examples.
If you have an opens source application using python-youtube, send me a link, and I am very happy to add a link to it here.
But if you want to get user data by OAuth. You need create the credential for OAuth client ID
.
And get more info at next page for Authorization.
"},{"location":"installation/","title":"Installation","text":"This library supports Python 3.6 and newer.
"},{"location":"installation/#dependencies","title":"Dependencies","text":"These following distributions will be installed automatically when installing Python-Youtube.
You can install this library from PyPI
$ pip install --upgrade python-youtube\n
Also, you can build this library from source code
$ git clone https://github.com/sns-sdks/python-youtube.git\n$ cd python-youtube\n$ make env\n$ make build\n
"},{"location":"installation/#testing","title":"Testing","text":"If you have been installing the requirements use make env
. You can use following command to test the code
$ make tests-html\n
"},{"location":"introduce-new-structure/","title":"Introduce Structure","text":"This doc will show you the new api structure for this library.
"},{"location":"introduce-new-structure/#brief","title":"Brief","text":"To make the package easier to maintain and easy to use. We are shifted to using classes for different YouTube resources in an easier, higher-level programming experience.
In this structure, every resource will have self class. And to operate with YouTube API.
"},{"location":"introduce-new-structure/#simple-usage","title":"Simple usage","text":""},{"location":"introduce-new-structure/#initial-client","title":"Initial Client","text":"from pyyoutube import Client\n\nclient = Client(api_key=\"your api key\")\n
"},{"location":"introduce-new-structure/#get-data","title":"Get data.","text":"for example to get channel data.
resp = client.channels.list(\n parts=[\"id\", \"snippet\"],\n channel_id=\"UCa-vrCLQHviTOVnEKDOdetQ\" \n)\n# resp output\n# ChannelListResponse(kind='youtube#channelListResponse')\n# resp.items[0].id output\n# UCa-vrCLQHviTOVnEKDOdetQ\n
"},{"location":"usage/work-with-api/","title":"Work with Api","text":"Tips
This is previous version to operate YouTube DATA API.
We recommend using the latest version of methods to operate YouTube DATA API.
The API is exposed via the pyyoutube.Api
class.
There provide two method to create instance the pyyoutube.Api
.
You can just initialize with an api key.
>>> from pyyoutube import Api\n\n>>> api = Api(api_key=\"your api key\")\n
If you want to get some authorization data. you need to initialize with access token.
>>> from pyyoutube import Api\n\n>>> api = Api(access_token='your api key')\n
You can read the docs to see how to get an access token.
Or you can ask for user to do oauth flow:
>>> from pyyoutube import Api\n\n>>> api = Api(client_id=\"client key\", client_secret=\"client secret\")\n# Get authorization url\n>>> api.get_authorization_url()\n# ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=scope&state=PyYouTube&access_type=offline&prompt=select_account', 'PyYouTube')\n# user to do\n# copy the response url\n>>> api.generate_access_token(authorization_response=\"link for response\")\n# AccessToken(access_token='token', expires_in=3599, token_type='Bearer')\n
"},{"location":"usage/work-with-api/#usage","title":"Usage","text":"Now you can use the instance to get data from YouTube.
"},{"location":"usage/work-with-api/#channel-data","title":"CHANNEL DATA","text":"The library provides several ways to get channel's data.
If a channel is not found, the property items
will return with blank list.
You can use channel id:
>>> channel_by_id = api.get_channel_info(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\")\n>>> channel_by_id.items\n[Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')]\n>>> channel_by_id.items[0].to_dict()\n{'kind': 'youtube#channel',\n 'etag': '\"j6xRRd8dTPVVptg711_CSPADRfg/AW8QEqbNRoIJv9KuzCIg0CG6aJA\"',\n 'id': 'UC_x5XG1OV2P6uZZ5FSM9Ttw',\n 'snippet': {'title': 'Google Developers',\n 'description': 'The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.',\n 'customUrl': 'googlecode',\n 'publishedAt': '2007-08-23T00:34:43.000Z',\n 'thumbnails': {'default': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s88-c-k-c0xffffffff-no-rj-mo',\n 'width': 88,\n 'height': 88},\n 'medium': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s240-c-k-c0xffffffff-no-rj-mo',\n 'width': 240,\n 'height': 240},\n 'high': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s800-c-k-c0xffffffff-no-rj-mo',\n 'width': 800,\n 'height': 800},\n 'standard': None,\n 'maxres': None},\n 'defaultLanguage': None,\n 'localized': {'title': 'Google Developers',\n 'description': 'The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.'},\n 'country': 'US'},\n ...\n }\n
You can pass a channel id with comma-separated id string or a list, tuple or set of ids to get multiple channels. Many methods also provide this functionality.
with ids:
>>> channel_by_ids = api.get_channel_info(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw,UCa-vrCLQHviTOVnEKDOdetQ\")\n>>> channel_by_ids.items\n[Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw'),\n Channel(kind='youtube#channel', id='UCa-vrCLQHviTOVnEKDOdetQ')]\n
You can also use channel name:
>>> channel_by_username = api.get_channel_info(for_username=\"GoogleDevelopers\")\n>>> channel_by_username.items[0]\nChannel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')\n
If you have authorized, you can get your channels:
>>> channel_by_mine = api_with_authorization.get_channel_info(mine=True)\n>>> channel_by_mine.items[0]\nChannel(kind='youtube#channel', id='UCa-vrCLQHviTOVnEKDOdetQ')\n
Tips
To get your channel, you must do authorization first, otherwise you will get an error.
"},{"location":"usage/work-with-api/#playlist","title":"PLAYLIST","text":"There are methods to get playlists by playlist id, channel id or get your own playlists.
Get playlists by id:
>>> playlists_by_id = api.get_playlist_by_id(playlist_id=\"PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw\")\n>>> playlists_by_id.items\n[Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw')]\n
Get playlists by channel (If you want to get all playlists for the target channel's , just provide the parameter count=None
):
>>> playlists_by_channel = api.get_playlists(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\")\n>>> playlists_by_channel.items\n[Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw'),\n Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIJO83u2UmyC8ud41AvUnhgj'),\n Playlist(kind='youtube#playlist', id='PLOU2XLYxmsILfV1LiUhDjbh1jkFjQWrYB'),\n Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKNr3Wfhm8o0TSojW7hEPPY'),\n Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIJ8ItHmK4bRlY4GCzMgXLAJ')]\n
Get your playlists(this requires authorization):
>>> playlists_by_mine = api.get_playlists(mine=True)\n
"},{"location":"usage/work-with-api/#playlist-item","title":"PLAYLIST ITEM","text":"Similarly, you can get playlist items by playlist item id or playlist id.
Get playlist items by id:
>>> playlist_item_by_id = api.get_playlist_item_by_id(playlist_item_id=\"UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA\"\n... \"1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2\")\n\n>>> playlist_item_by_id.items\n[PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2')]\n
Get playlist items by playlist id (If you want to get target playlist all items, just provide the parameter count=None
):
>>> playlist_item_by_playlist = api.get_playlist_items(playlist_id=\"PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw\", count=2)\n\n>>> playlist_item_by_playlist.items\n[PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2'),\n PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy4yODlGNEE0NkRGMEEzMEQy')]\n>>> playlist_item_by_id.items[0].snippet.resourceId\nResourceId(kind='youtube#video', videoId='CvTApw9X8aA')\n
"},{"location":"usage/work-with-api/#video","title":"VIDEO","text":"You can get a video's information by several methods.
Get videos by video id(s):
>>> video_by_id = api.get_video_by_id(video_id=\"CvTApw9X8aA\")\n\n>>> video_by_id\nVideoListResponse(kind='youtube#videoListResponse')\n\n>>> video_by_id.items\n[Video(kind='youtube#video', id='CvTApw9X8aA')]\n
Get videos by chart (If you want to get all videos, just provide the parameter count=None
):
>>> video_by_chart = api.get_videos_by_chart(chart=\"mostPopular\", region_code=\"US\", count=2)\n\n>>> video_by_chart.items\n[Video(kind='youtube#video', id='RwnN2FVaHmw'),\n Video(kind='youtube#video', id='hDeuSfo_Ys0')]\n
Get videos by your rating (this requires authorization, also if you want to get all videos, just provide the parameter count=None
):
>>> videos_by_rating = api.get_videos_by_myrating(rating=\"like\", count=2)\n
"},{"location":"usage/work-with-api/#comment-thread","title":"COMMENT THREAD","text":"You can get comment thread information by id or some filter.
Get comment thread by id(s):
>>> ct_by_id = api.get_comment_thread_by_id(comment_thread_id='Ugz097FRhsQy5CVhAjp4AaABAg,UgzhytyP79_Pwa\n... Dd4UB4AaABAg')\n\n>>> ct_by_id.items\n[CommentThread(kind='youtube#commentThread', id='Ugz097FRhsQy5CVhAjp4AaABAg'),\n CommentThread(kind='youtube#commentThread', id='UgzhytyP79_PwaDd4UB4AaABAg')]\n
Get all comment threads related to a channel (including comment threads for the channel's video, also if you want to get all comment threads, just provide the parameter count=None
):
>>> ct_by_all = api.get_comment_threads(all_to_channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\", count=2)\n\n>>> ct_by_all.items\n[CommentThread(kind='youtube#commentThread', id='UgwlB_Cza9WtzUWahYN4AaABAg'),\n CommentThread(kind='youtube#commentThread', id='UgyvoQJ2LsxCBwGEpMB4AaABAg')]\n
Get comment threads only for the channel (If you want to get all comment threads, just provide the parameter count=None
):
>>> ct_by_channel = api.get_comment_threads(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\", count=2)\n\n>>> ct_by_channel.items\n[CommentThread(kind='youtube#commentThread', id='UgyUBI0HsgL9emxcZpR4AaABAg'),\n CommentThread(kind='youtube#commentThread', id='Ugzi3lkqDPfIOirGFLh4AaABAg')]\n
Get comment threads only for the video (If you want to get all comment threads, just provide the parameter count=None
):
>>> ct_by_video = api.get_comment_threads(video_id=\"D-lhorsDlUQ\", count=2)\n\n>>> ct_by_video.items\n[CommentThread(kind='youtube#commentThread', id='UgydxWWoeA7F1OdqypJ4AaABAg'),\n CommentThread(kind='youtube#commentThread', id='UgxKREWxIgDrw8w2e_Z4AaABAg')]\n
"},{"location":"usage/work-with-api/#comment","title":"COMMENT","text":"You can get comment information by id or use the top-level comment id to get replies.
Tips
The reply has the same structure as a comment.
Get comments by id(s):
>>> comment_by_id = api.get_comment_by_id(comment_id='UgxKREWxIgDrw8w2e_Z4AaABAg,UgyrVQaFfEdvaSzstj14AaABAg')\n\n>>> comment_by_id.items\n[Comment(kind='youtube#comment', id='UgxKREWxIgDrw8w2e_Z4AaABAg', snippet=CommentSnippet(authorDisplayName='Hieu Nguyen', likeCount=0)),\n Comment(kind='youtube#comment', id='UgyrVQaFfEdvaSzstj14AaABAg', snippet=CommentSnippet(authorDisplayName='Mani Kanta', likeCount=0))]\n
Get replies by comment id (If you want to get all comments, just provide the parameter count=None
):
>>> comment_by_parent = api.get_comments(parent_id=\"UgwYjZXfNCUTKPq9CZp4AaABAg\")\n>>> comment_by_parent.items\n[Comment(kind='youtube#comment', id='UgwYjZXfNCUTKPq9CZp4AaABAg.8yxhlQJogG18yz_cXK9Kcj', snippet=CommentSnippet(authorDisplayName='Marlon L\u00f3pez', likeCount=0))]\n
"},{"location":"usage/work-with-api/#video-category","title":"VIDEO CATEGORY","text":"You can get video category with id or region.
Get video categories with id(s):
>>> video_category_by_id = api.get_video_categories(category_id=\"17,18\")\n\n>>> video_category_by_id.items\n[VideoCategory(kind='youtube#videoCategory', id='17'),\n VideoCategory(kind='youtube#videoCategory', id='18')]\n
Get video categories with region code:
>>> video_categories_by_region = api.get_video_categories(region_code=\"US\")\n\n>>> video_categories_by_region.items\n[VideoCategory(kind='youtube#videoCategory', id='1'),\n VideoCategory(kind='youtube#videoCategory', id='2'),\n VideoCategory(kind='youtube#videoCategory', id='10'),\n VideoCategory(kind='youtube#videoCategory', id='15'),\n ...]\n
"},{"location":"usage/work-with-api/#subscriptions","title":"SUBSCRIPTIONS","text":"You can get subscription information by id, by point channel, or your own.
Tips
If you want to get the subscriptions not set to public, you need do authorization first and get the access token. You can see the demo A demo for get my subscription.
To get subscription info by id(s), this needs your token to have the permission for the subscriptions belonging to a channel or user:
>>> r = api.get_subscription_by_id(\n... subscription_id=[\n... \"zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo\",\n... \"zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo\"])\n>>> r\nSubscriptionListResponse(kind='youtube#subscriptionListResponse')\n>>> r.items\n[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description='')),\n Subscription(kind='youtube#subscription', id='zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo', snippet=SubscriptionSnippet(title='ikaros-life', description='This is a test channel.'))]\n
Get your own subscriptions, this need you do authorization first or give the authorized access token:
>>> r = api.get_subscription_by_me(\n... mine=True,\n... parts=[\"id\", \"snippet\"],\n... count=2\n... )\n>>> r\nSubscriptionListResponse(kind='youtube#subscriptionListResponse')\n>>> r.items\n[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwtJ-Aho6DZeutqZiP4Q79Q', snippet=SubscriptionSnippet(title='Next Day Video', description='')),\n Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description=''))]\n
Get public channel's subscriptions:
>>> r = api.get_subscription_by_channel(\n... channel_id=\"UCAuUUnT6oDeKwE6v1NGQxug\",\n... parts=\"id,snippet\",\n... count=2\n... )\n>>> r\nSubscriptionListResponse(kind='youtube#subscriptionListResponse')\n>>> r.items\n[Subscription(kind='youtube#subscription', id='FMP3Mleijt-52zZDGkHtR5KhwkvCcdQKWWWIA1j5eGc', snippet=SubscriptionSnippet(title='TEDx Talks', description=\"TEDx is an international community that organizes TED-style events anywhere and everywhere -- celebrating locally-driven ideas and elevating them to a global stage. TEDx events are produced independently of TED conferences, each event curates speakers on their own, but based on TED's format and rules.\\n\\nFor more information on using TED for commercial purposes (e.g. employee learning, in a film, or in an online course), please submit a media request using the link below.\")),\n Subscription(kind='youtube#subscription', id='FMP3Mleijt_ZKvy5M-HhRlsqI4wXY7VmP5g8lvmRhVU', snippet=SubscriptionSnippet(title='TED Residency', description='The TED Residency program is an incubator for breakthrough ideas. It is free and open to all via a semi-annual competitive application. Those chosen as TED Residents spend four months at TED headquarters in New York City, working on their idea. Selection criteria include the strength of their idea, their character, and their ability to bring a fresh perspective and positive contribution to the diverse TED community.'))]\n
"},{"location":"usage/work-with-api/#activities","title":"ACTIVITIES","text":"You can get activities by channel id. You can also get your own activities after you have completed authorization.
Get public channel activities:
>>> r = api.get_activities_by_channel(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\", count=2)\n>>> r\nActivityListResponse(kind='youtube#activityListResponse')\n>>> r.items\n[Activity(kind='youtube#activity', id='MTUxNTc3NzM2MDAyODIxOTQxNDM0NjAwMA==', snippet=ActivitySnippet(title='2019 Year in Review - The Developer Show', description='Here to bring you the latest developer news from across Google this year is Developer Advocate Timothy Jordan. In this last week of the year, we\u2019re taking a look back at some of the coolest and biggest announcements we covered in 2019! \\n\\nFollow Google Developers on Instagram \u2192 https://goo.gle/googledevs\\n\\nWatch more #DevShow \u2192 https://goo.gle/GDevShow\\nSubscribe to Google Developers \u2192 https://goo.gle/developers')),\n Activity(kind='youtube#activity', id='MTUxNTc3MTI4NzIzODIxOTQxNDM0NzI4MA==', snippet=ActivitySnippet(title='GDE Promo - Lara Martin', description='Meet Lara Martin, a Flutter/Dart Google Developers Expert and get inspired by her journey. Watch now for a preview of her story! #GDESpotlights #IncludedWithGoogle\\n\\nLearn about the GDE program \u2192 https://goo.gle/2qWOvAy\\n\\nGoogle Developers Experts \u2192 https://goo.gle/GDE\\nSubscribe to Google Developers \u2192 https://goo.gle/developers'))]\n
Get your activities:
>>> r = api_with_token.get_activities_by_me()\n>>> r.items\n[Activity(kind='youtube#activity', id='MTUxNTc0OTk2MjI3NDE0MjYwMDY1NjAwODA=', snippet=ActivitySnippet(title='\u534e\u5c71\u65e5\u51fa', description='\u51b7\u51b7\u7684\u5c71\u5934')),\n Activity(kind='youtube#activity', id='MTUxNTc0OTk1OTAyNDE0MjYwMDY1NTc2NDg=', snippet=ActivitySnippet(title='\u6d77\u4e0a\u65e5\u51fa', description='\u7f8e\u7f8e\u7f8e'))]\n
Get your video captions:
>>> r = api.get_captions_by_video(video_id=\"oHR3wURdJ94\", parts=[\"id\", \"snippet\"])\n>>> r\nCaptionListResponse(kind='youtube#captionListResponse')\n>>> r.items\n[Caption(kind='youtube#caption', id='SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:40:49.981Z')),\n Caption(kind='youtube#caption', id='fPMuDm722CIRcUAT3NTPQHQZJZJxt39kU7JvrHk8Kzs=', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:39:46.991Z'))]\n
If you already have caption id(s), you can get video caption by id(s):
>>> r = api.get_captions_by_video(video_id=\"oHR3wURdJ94\", parts=[\"id\", \"snippet\"], caption_id=\"SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I\")\n>>> r\nCaptionListResponse(kind='youtube#captionListResponse')\n>>> r.items\n[Caption(kind='youtube#caption', id='SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:40:49.981Z'))]\n
"},{"location":"usage/work-with-api/#channel-sections","title":"CHANNEL SECTIONS","text":"You can get channel sections by self id or belonged channel id or your own channel.
Get channel sections by channel id:
>>> r = api.get_channel_sections_by_channel(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\")\n>>>> r\nChannelSectionResponse(kind='youtube#channelSectionListResponse')\n>>> r.items\n[ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.B8DTd9ZXJqM'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.MfvRjkWLxgk'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.fEjJOXRoWwg'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.PvTmxDBxtLs'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.pmcIOsL7s98'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.c3r3vYf9uD0'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.ZJpkBl-mXfM'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.9_wU0qhEPR8'),\n ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.npYvuMz0_es')]\n
Get authorized user's channel sections:
>>> r = api.get_channel_sections_by_channel(mine=True)\n>>> r.items\n[ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.jNQXAC9IVRw'),\n ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.LeAltgu_pbM'),\n ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.nGzAI5pLbMY')]\n
Get channel section detail info by id:
>>> r = api.get_channel_section_by_id(section_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE\")\n>>> r\nChannelSectionResponse(kind='youtube#channelSectionListResponse')\n>>> r1.items\n[ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE')]\n
"},{"location":"usage/work-with-api/#i18n-resource","title":"I18N RESOURCE","text":"You can get a list of content regions that the YouTube website supports:
>>> r = api.get_i18n_regions(parts=[\"snippet\"])\n>>> r.items\n[I18nRegion(kind='youtube#i18nRegion', id='DZ', snippet=I18nRegionSnippet(gl='DZ', name='Algeria')),\n I18nRegion(kind='youtube#i18nRegion', id='AR', snippet=I18nRegionSnippet(gl='AR', name='Argentina')),\n I18nRegion(kind='youtube#i18nRegion', id='AU', snippet=I18nRegionSnippet(gl='AU', name='Australia'))\n ...]\n
You can get a list of application languages that the YouTube website supports:
>>> r = api.get_i18n_languages(parts=[\"snippet\"])\n>>> r.items\n[I18nLanguage(kind='youtube#i18nLanguage', id='af', snippet=I18nLanguageSnippet(hl='af', name='Afrikaans')),\n I18nLanguage(kind='youtube#i18nLanguage', id='az', snippet=I18nLanguageSnippet(hl='az', name='Azerbaijani')),\n I18nLanguage(kind='youtube#i18nLanguage', id='id', snippet=I18nLanguageSnippet(hl='id', name='Indonesian')),\n ...]\n
"},{"location":"usage/work-with-api/#member","title":"MEMBER","text":"The API request must be authorized by the channel owner.
You can retrieve a list of members (formerly known as \"sponsors\") for a channel:
>>> r = api_with_token.get_members(parts=[\"snippet\"])\n>>> r.items\n[MemberListResponse(kind='youtube#memberListResponse'),\n MemberListResponse(kind='youtube#memberListResponse')]\n
"},{"location":"usage/work-with-api/#membership-level","title":"MEMBERSHIP LEVEL","text":"The API request must be authorized by the channel owner.
You can retrieve a list membership levels for a channel:
>>> r = api_with_token.get_membership_levels(parts=[\"snippet\"])\n>>> r.items\n[MembershipsLevelListResponse(kind='youtube#membershipsLevelListResponse'),\n MembershipsLevelListResponse(kind='youtube#membershipsLevelListResponse')]\n
"},{"location":"usage/work-with-api/#video-abuse-report-reason","title":"VIDEO ABUSE REPORT REASON","text":"You can retrieve a list of reasons that can be used to report abusive videos:
>>> r = api_with_token.get_video_abuse_report_reason(parts=[\"snippet\"])\n>>> r.items\n[VideoAbuseReportReason(kind='youtube#videoAbuseReportReason'),\n VideoAbuseReportReason(kind='youtube#videoAbuseReportReason')]\n
"},{"location":"usage/work-with-api/#search","title":"SEARCH","text":"You can use those methods to search the video,playlist,channel data. For more info, you can see the Search Request Docs.
You can search different type of resource with keywords:
>>> r = api.search_by_keywords(q=\"surfing\", search_type=[\"channel\",\"video\", \"playlist\"], count=5, limit=5)\n>>> r.items\n[SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult')]\n
You can search your app send videos:
>>> r = api_with_token.search_by_developer(q=\"news\", count=1)\n>>> r.items\n[SearchResult(kind='youtube#searchResult')]\n
You can search your videos:
>>> r = api_with_token.search_by_mine(q=\"news\", count=1)\n>>> r.items\n[SearchResult(kind='youtube#searchResult')]\n
Or you can build your request using the search
method:
>>> r = api.search(\n... location=\"21.5922529, -158.1147114\",\n... location_radius=\"10mi\",\n... q=\"surfing\",\n... parts=[\"snippet\"],\n... count=5,\n... published_after=\"2020-02-01T00:00:00Z\",\n... published_before=\"2020-03-01T00:00:00Z\",\n... safe_search=\"moderate\",\n... search_type=\"video\")\n>>> r.items\n[SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult')]\n\n>>> r = api.search(\n... event_type=\"live\",\n... q=\"news\",\n... count=3,\n... parts=[\"snippet\"],\n... search_type=\"video\",\n... topic_id=\"/m/09s1f\",\n... order=\"viewCount\")\n>>> r.items\n[SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult'),\n SearchResult(kind='youtube#searchResult')]\n
"},{"location":"usage/work-with-client/","title":"Work with Client","text":"We have refactored the project code to support more methods and improve code usability.
And new structure like follows.
In this structure, we identify each entity as a class of resources and perform operations on the resources.
"},{"location":"usage/work-with-client/#instantiate","title":"INSTANTIATE","text":"Client is exposed via the pyyoutube.Client
class.
You can initialize it with api key
, to get public data.
from pyyoutube import Client\n\ncli = Client(api_key=\"your api key\")\n
If you want to update your channel data. or upload video. You need initialize with access token
, Or do auth flow.
from pyyoutube import Client\n\ncli = Client(access_token=\"Access Token with permissions\")\n
from pyyoutube import Client\n\ncli = Client(client_id=\"ID for app\", client_secret=\"Secret for app\")\n# Get authorization url\ncli.get_authorize_url()\n# ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=scope&state=PyYouTube&access_type=offline&prompt=select_account', 'PyYouTube')\n# Click url and give permissions.\n# Copy the redirected url.\ncli.generate_access_token(authorization_response=\"redirected url\")\n# AccessToken(access_token='token', expires_in=3599, token_type='Bearer')\n
"},{"location":"usage/work-with-client/#from-client_secret","title":"from client_secret","text":"Only web
and some installed
type client_secrets are supported.
The fields client_id
and client_secret
must be set.
If the field redirect_uris
has 1 or more values set, the Client.DEFAULT_REDIRECT_URI
will be set to the first entrie.
from pyyoutube import Client\n\nfile_path = \"path/to/client_secret.json\"\ncli = Client(client_secret_path=file_path)\n\n# Then go through auth flow descriped above\n
Once initialize client. Then you can operate API to get data.
"},{"location":"usage/work-with-client/#usage","title":"Usage","text":""},{"location":"usage/work-with-client/#channel-resource","title":"Channel Resource","text":"The API supports the following methods for channels
resources:
resp = cli.channels.list(channel_id=\"UC_x5XG1OV2P6uZZ5FSM9Ttw\")\n# ChannelListResponse(kind='youtube#channelListResponse')\nprint(resp.items)\n# [Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')]\n
"},{"location":"usage/work-with-client/#update-channel-metadata","title":"update channel metadata","text":"import pyyoutube.models as mds\n\nbody = mds.Channel(\n id=\"channel id\",\n brandingSettings=mds.ChannelBrandingSetting(\n image=mds.ChannelBrandingSettingImage(\n bannerExternalUrl=\"new banner url\"\n )\n )\n)\n\nchannel = cli.channels.update(\n part=\"brandingSettings\",\n body=body\n)\nprint(channel.brandingSettings.image.bannerExternalUrl)\n# 'https://yt3.googleusercontent.com/AegVxoIusdXEmsJ9j3bcJR3zuImOd6TngNw58iJAP0AOAXCnb1xHPcuEDOQC8J85SCZvt5i8A_g'\n
"},{"location":"usage/work-with-client/#video-resource","title":"Video Resource","text":"The API supports the following methods for videos
resources.
Retrieves the ratings that the authorized user gave to a list of specified videos.
resp = cli.videos.get_rating(video_id=\"Z56Jmr9Z34Q\")\n\nprint(resp.items)\n# [VideoRatingItem(videoId='Z56Jmr9Z34Q', rating='none')]\n
"},{"location":"usage/work-with-client/#list","title":"list","text":"Returns a list of videos that match the API request parameters.
resp = cli.videos.list(video_id=\"Z56Jmr9Z34Q\")\n\nprint(resp.items)\n# [Video(kind='youtube#video', id='Z56Jmr9Z34Q')]\n
"},{"location":"usage/work-with-client/#insert","title":"insert","text":"Uploads a video to YouTube and optionally sets the video's metadata.
import pyyoutube.models as mds\nfrom pyyoutube.media import Media\n\nbody = mds.Video(\n snippet=mds.VideoSnippet(\n title=\"video title\",\n description=\"video description\"\n )\n)\n\nmedia = Media(filename=\"video.mp4\")\n\nupload = cli.videos.insert(\n body=body,\n media=media,\n parts=[\"snippet\"],\n notify_subscribers=True\n)\n\nvideo_body = None\n\nwhile video_body is None:\n status, video_body = upload.next_chunk()\n if status:\n print(f\"Upload progress: {status.progress()}\")\n\nprint(video_body)\n# {\"kind\": \"youtube#video\", \"etag\": \"17W46NjVxoxtaoh1E6GmbQ2hv5c\",....}\n
"},{"location":"usage/work-with-client/#update","title":"update","text":"Updates a video's metadata.
import pyyoutube.models as mds\n\nbody = mds.Video(\n id=\"fTK1Jj6QlDw\",\n snippet=mds.VideoSnippet(\n title=\"What a nice day\",\n description=\"Blue sky with cloud. updated.\",\n categoryId=\"1\",\n )\n)\n\nresp = cli.videos.update(\n parts=[\"snippet\"],\n body=body,\n return_json=True,\n)\nprint(resp)\n# {\"kind\": \"youtube#video\", \"etag\": \"BQUtovVd0TBJwC5S8-Pu-dK_I6s\", \"id\": \"fTK1Jj6QlDw\", \"snippet\": {\"publishedAt\": \"2022-12-15T03:45:16Z\", \"channelId\": \"UCa-vrCLQHviTOVnEKDOdetQ\", \"title\": \"What a nice day\", \"description\": \"Blue sky with cloud. updated.\", \"thumbnails\": {\"default\": {\"url\": \"https://i.ytimg.com/vi/fTK1Jj6QlDw/default.jpg\", \"width\": 120, \"height\": 90}, \"medium\": {\"url\": \"https://i.ytimg.com/vi/fTK1Jj6QlDw/mqdefault.jpg\", \"width\": 320, \"height\": 180}, \"high\": {\"url\": \"https://i.ytimg.com/vi/fTK1Jj6QlDw/hqdefault.jpg\", \"width\": 480, \"height\": 360}, \"standard\": {\"url\": \"https://i.ytimg.com/vi/fTK1Jj6QlDw/sddefault.jpg\", \"width\": 640, \"height\": 480}, \"maxres\": {\"url\": \"https://i.ytimg.com/vi/fTK1Jj6QlDw/maxresdefault.jpg\", \"width\": 1280, \"height\": 720}}, \"channelTitle\": \"ikaros data\", \"categoryId\": \"1\", \"liveBroadcastContent\": \"none\", \"localized\": {\"title\": \"What a nice day\", \"description\": \"Blue sky with cloud. updated.\"}, \"defaultAudioLanguage\": \"en-US\"}}\n
"},{"location":"usage/work-with-client/#delete","title":"delete","text":"Deletes a YouTube video.
cli.videos.delete(video_id=\"fTK1Jj6QlDw\")\n# True\n
"},{"location":"usage/work-with-client/#rate","title":"rate","text":"Add a like or dislike rating to a video or remove a rating from a video.
cli.videos.rate(video_id=\"fTK1Jj6QlDw\", rating=\"like\")\n# True\n
"},{"location":"usage/work-with-client/#reportabuse","title":"reportAbuse","text":"Report a video for containing abusive content.
import pyyoutube.models as mds\n\nbody = mds.VideoReportAbuse(\n videoId=\"fTK1Jj6QlDw\",\n reasonId=\"32\"\n)\ncli.videos.report_abuse(body=body)\n# True\n
"}]}
\ No newline at end of file
diff --git a/sitemap.xml b/sitemap.xml
new file mode 100644
index 00000000..bbe1675d
--- /dev/null
+++ b/sitemap.xml
@@ -0,0 +1,43 @@
+
+Tips
+This is previous version to operate YouTube DATA API.
+We recommend using the latest version of methods to operate YouTube DATA API.
+The API is exposed via the pyyoutube.Api
class.
There provide two method to create instance the pyyoutube.Api
.
You can just initialize with an api key.
+>>> from pyyoutube import Api
+
+>>> api = Api(api_key="your api key")
+
If you want to get some authorization data. you need to initialize with access token.
+>>> from pyyoutube import Api
+
+>>> api = Api(access_token='your api key')
+
You can read the docs to see how to get an access token.
+Or you can ask for user to do oauth flow:
+>>> from pyyoutube import Api
+
+>>> api = Api(client_id="client key", client_secret="client secret")
+# Get authorization url
+>>> api.get_authorization_url()
+# ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=scope&state=PyYouTube&access_type=offline&prompt=select_account', 'PyYouTube')
+# user to do
+# copy the response url
+>>> api.generate_access_token(authorization_response="link for response")
+# AccessToken(access_token='token', expires_in=3599, token_type='Bearer')
+
Now you can use the instance to get data from YouTube.
+The library provides several ways to get channel's data.
+If a channel is not found, the property items
will return with blank list.
You can use channel id:
+>>> channel_by_id = api.get_channel_info(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw")
+>>> channel_by_id.items
+[Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')]
+>>> channel_by_id.items[0].to_dict()
+{'kind': 'youtube#channel',
+ 'etag': '"j6xRRd8dTPVVptg711_CSPADRfg/AW8QEqbNRoIJv9KuzCIg0CG6aJA"',
+ 'id': 'UC_x5XG1OV2P6uZZ5FSM9Ttw',
+ 'snippet': {'title': 'Google Developers',
+ 'description': 'The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.',
+ 'customUrl': 'googlecode',
+ 'publishedAt': '2007-08-23T00:34:43.000Z',
+ 'thumbnails': {'default': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s88-c-k-c0xffffffff-no-rj-mo',
+ 'width': 88,
+ 'height': 88},
+ 'medium': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s240-c-k-c0xffffffff-no-rj-mo',
+ 'width': 240,
+ 'height': 240},
+ 'high': {'url': 'https://yt3.ggpht.com/a/AGF-l78iFtAxyRZcUBzG91kbKMES19z-zGW5KT20_g=s800-c-k-c0xffffffff-no-rj-mo',
+ 'width': 800,
+ 'height': 800},
+ 'standard': None,
+ 'maxres': None},
+ 'defaultLanguage': None,
+ 'localized': {'title': 'Google Developers',
+ 'description': 'The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.'},
+ 'country': 'US'},
+ ...
+ }
+
You can pass a channel id with comma-separated id string or a list, tuple or set of ids to get multiple channels. +Many methods also provide this functionality.
+with ids:
+>>> channel_by_ids = api.get_channel_info(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw,UCa-vrCLQHviTOVnEKDOdetQ")
+>>> channel_by_ids.items
+[Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw'),
+ Channel(kind='youtube#channel', id='UCa-vrCLQHviTOVnEKDOdetQ')]
+
You can also use channel name:
+>>> channel_by_username = api.get_channel_info(for_username="GoogleDevelopers")
+>>> channel_by_username.items[0]
+Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')
+
If you have authorized, you can get your channels:
+>>> channel_by_mine = api_with_authorization.get_channel_info(mine=True)
+>>> channel_by_mine.items[0]
+Channel(kind='youtube#channel', id='UCa-vrCLQHviTOVnEKDOdetQ')
+
Tips
+To get your channel, you must do authorization first, otherwise you will get an error.
+There are methods to get playlists by playlist id, channel id or get your own playlists.
+Get playlists by id:
+>>> playlists_by_id = api.get_playlist_by_id(playlist_id="PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw")
+>>> playlists_by_id.items
+[Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw')]
+
Get playlists by channel (If you want to get all playlists for the target channel's , just provide the
+parameter count=None
):
>>> playlists_by_channel = api.get_playlists(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw")
+>>> playlists_by_channel.items
+[Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw'),
+ Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIJO83u2UmyC8ud41AvUnhgj'),
+ Playlist(kind='youtube#playlist', id='PLOU2XLYxmsILfV1LiUhDjbh1jkFjQWrYB'),
+ Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIKNr3Wfhm8o0TSojW7hEPPY'),
+ Playlist(kind='youtube#playlist', id='PLOU2XLYxmsIJ8ItHmK4bRlY4GCzMgXLAJ')]
+
Get your playlists(this requires authorization):
+>>> playlists_by_mine = api.get_playlists(mine=True)
+
Similarly, you can get playlist items by playlist item id or playlist id.
+Get playlist items by id:
+>>> playlist_item_by_id = api.get_playlist_item_by_id(playlist_item_id="UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA"
+... "1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2")
+
+>>> playlist_item_by_id.items
+[PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2')]
+
Get playlist items by playlist id (If you want to get target playlist all items, just provide the
+parameter count=None
):
>>> playlist_item_by_playlist = api.get_playlist_items(playlist_id="PLOU2XLYxmsIKpaV8h0AGE05so0fAwwfTw", count=2)
+
+>>> playlist_item_by_playlist.items
+[PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy41NkI0NEY2RDEwNTU3Q0M2'),
+ PlaylistItem(kind='youtube#playlistItem', id='UExPVTJYTFl4bXNJS3BhVjhoMEFHRTA1c28wZkF3d2ZUdy4yODlGNEE0NkRGMEEzMEQy')]
+>>> playlist_item_by_id.items[0].snippet.resourceId
+ResourceId(kind='youtube#video', videoId='CvTApw9X8aA')
+
You can get a video's information by several methods.
+Get videos by video id(s):
+>>> video_by_id = api.get_video_by_id(video_id="CvTApw9X8aA")
+
+>>> video_by_id
+VideoListResponse(kind='youtube#videoListResponse')
+
+>>> video_by_id.items
+[Video(kind='youtube#video', id='CvTApw9X8aA')]
+
Get videos by chart (If you want to get all videos, just provide the parameter count=None
):
>>> video_by_chart = api.get_videos_by_chart(chart="mostPopular", region_code="US", count=2)
+
+>>> video_by_chart.items
+[Video(kind='youtube#video', id='RwnN2FVaHmw'),
+ Video(kind='youtube#video', id='hDeuSfo_Ys0')]
+
Get videos by your rating (this requires authorization, also if you want to get all videos, just provide the
+parameter count=None
):
>>> videos_by_rating = api.get_videos_by_myrating(rating="like", count=2)
+
You can get comment thread information by id or some filter.
+Get comment thread by id(s):
+>>> ct_by_id = api.get_comment_thread_by_id(comment_thread_id='Ugz097FRhsQy5CVhAjp4AaABAg,UgzhytyP79_Pwa
+... Dd4UB4AaABAg')
+
+>>> ct_by_id.items
+[CommentThread(kind='youtube#commentThread', id='Ugz097FRhsQy5CVhAjp4AaABAg'),
+ CommentThread(kind='youtube#commentThread', id='UgzhytyP79_PwaDd4UB4AaABAg')]
+
Get all comment threads related to a channel (including comment threads for the channel's video, also if you want to get
+all comment threads, just provide the parameter count=None
):
>>> ct_by_all = api.get_comment_threads(all_to_channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw", count=2)
+
+>>> ct_by_all.items
+[CommentThread(kind='youtube#commentThread', id='UgwlB_Cza9WtzUWahYN4AaABAg'),
+ CommentThread(kind='youtube#commentThread', id='UgyvoQJ2LsxCBwGEpMB4AaABAg')]
+
Get comment threads only for the channel (If you want to get all comment threads, just provide the
+parameter count=None
):
>>> ct_by_channel = api.get_comment_threads(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw", count=2)
+
+>>> ct_by_channel.items
+[CommentThread(kind='youtube#commentThread', id='UgyUBI0HsgL9emxcZpR4AaABAg'),
+ CommentThread(kind='youtube#commentThread', id='Ugzi3lkqDPfIOirGFLh4AaABAg')]
+
Get comment threads only for the video (If you want to get all comment threads, just provide the
+parameter count=None
):
>>> ct_by_video = api.get_comment_threads(video_id="D-lhorsDlUQ", count=2)
+
+>>> ct_by_video.items
+[CommentThread(kind='youtube#commentThread', id='UgydxWWoeA7F1OdqypJ4AaABAg'),
+ CommentThread(kind='youtube#commentThread', id='UgxKREWxIgDrw8w2e_Z4AaABAg')]
+
You can get comment information by id or use the top-level comment id to get replies.
+Tips
+The reply has the same structure as a comment.
+Get comments by id(s):
+>>> comment_by_id = api.get_comment_by_id(comment_id='UgxKREWxIgDrw8w2e_Z4AaABAg,UgyrVQaFfEdvaSzstj14AaABAg')
+
+>>> comment_by_id.items
+[Comment(kind='youtube#comment', id='UgxKREWxIgDrw8w2e_Z4AaABAg', snippet=CommentSnippet(authorDisplayName='Hieu Nguyen', likeCount=0)),
+ Comment(kind='youtube#comment', id='UgyrVQaFfEdvaSzstj14AaABAg', snippet=CommentSnippet(authorDisplayName='Mani Kanta', likeCount=0))]
+
Get replies by comment id (If you want to get all comments, just provide the parameter count=None
):
>>> comment_by_parent = api.get_comments(parent_id="UgwYjZXfNCUTKPq9CZp4AaABAg")
+>>> comment_by_parent.items
+[Comment(kind='youtube#comment', id='UgwYjZXfNCUTKPq9CZp4AaABAg.8yxhlQJogG18yz_cXK9Kcj', snippet=CommentSnippet(authorDisplayName='Marlon López', likeCount=0))]
+
You can get video category with id or region.
+Get video categories with id(s):
+>>> video_category_by_id = api.get_video_categories(category_id="17,18")
+
+>>> video_category_by_id.items
+[VideoCategory(kind='youtube#videoCategory', id='17'),
+ VideoCategory(kind='youtube#videoCategory', id='18')]
+
Get video categories with region code:
+>>> video_categories_by_region = api.get_video_categories(region_code="US")
+
+>>> video_categories_by_region.items
+[VideoCategory(kind='youtube#videoCategory', id='1'),
+ VideoCategory(kind='youtube#videoCategory', id='2'),
+ VideoCategory(kind='youtube#videoCategory', id='10'),
+ VideoCategory(kind='youtube#videoCategory', id='15'),
+ ...]
+
You can get subscription information by id, by point channel, or your own.
+Tips
+If you want to get the subscriptions not set to public, you need do authorization first and get the access token. +You can see the demo A demo for get my subscription.
+To get subscription info by id(s), this needs your token to have the permission for the subscriptions belonging to a +channel or user:
+>>> r = api.get_subscription_by_id(
+... subscription_id=[
+... "zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo",
+... "zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo"])
+>>> r
+SubscriptionListResponse(kind='youtube#subscriptionListResponse')
+>>> r.items
+[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description='')),
+ Subscription(kind='youtube#subscription', id='zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo', snippet=SubscriptionSnippet(title='ikaros-life', description='This is a test channel.'))]
+
Get your own subscriptions, this need you do authorization first or give the authorized access token:
+>>> r = api.get_subscription_by_me(
+... mine=True,
+... parts=["id", "snippet"],
+... count=2
+... )
+>>> r
+SubscriptionListResponse(kind='youtube#subscriptionListResponse')
+>>> r.items
+[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwtJ-Aho6DZeutqZiP4Q79Q', snippet=SubscriptionSnippet(title='Next Day Video', description='')),
+ Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description=''))]
+
Get public channel's subscriptions:
+>>> r = api.get_subscription_by_channel(
+... channel_id="UCAuUUnT6oDeKwE6v1NGQxug",
+... parts="id,snippet",
+... count=2
+... )
+>>> r
+SubscriptionListResponse(kind='youtube#subscriptionListResponse')
+>>> r.items
+[Subscription(kind='youtube#subscription', id='FMP3Mleijt-52zZDGkHtR5KhwkvCcdQKWWWIA1j5eGc', snippet=SubscriptionSnippet(title='TEDx Talks', description="TEDx is an international community that organizes TED-style events anywhere and everywhere -- celebrating locally-driven ideas and elevating them to a global stage. TEDx events are produced independently of TED conferences, each event curates speakers on their own, but based on TED's format and rules.\n\nFor more information on using TED for commercial purposes (e.g. employee learning, in a film, or in an online course), please submit a media request using the link below.")),
+ Subscription(kind='youtube#subscription', id='FMP3Mleijt_ZKvy5M-HhRlsqI4wXY7VmP5g8lvmRhVU', snippet=SubscriptionSnippet(title='TED Residency', description='The TED Residency program is an incubator for breakthrough ideas. It is free and open to all via a semi-annual competitive application. Those chosen as TED Residents spend four months at TED headquarters in New York City, working on their idea. Selection criteria include the strength of their idea, their character, and their ability to bring a fresh perspective and positive contribution to the diverse TED community.'))]
+
You can get activities by channel id. You can also get your own activities after you have completed authorization.
+Get public channel activities:
+>>> r = api.get_activities_by_channel(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw", count=2)
+>>> r
+ActivityListResponse(kind='youtube#activityListResponse')
+>>> r.items
+[Activity(kind='youtube#activity', id='MTUxNTc3NzM2MDAyODIxOTQxNDM0NjAwMA==', snippet=ActivitySnippet(title='2019 Year in Review - The Developer Show', description='Here to bring you the latest developer news from across Google this year is Developer Advocate Timothy Jordan. In this last week of the year, we’re taking a look back at some of the coolest and biggest announcements we covered in 2019! \n\nFollow Google Developers on Instagram → https://goo.gle/googledevs\n\nWatch more #DevShow → https://goo.gle/GDevShow\nSubscribe to Google Developers → https://goo.gle/developers')),
+ Activity(kind='youtube#activity', id='MTUxNTc3MTI4NzIzODIxOTQxNDM0NzI4MA==', snippet=ActivitySnippet(title='GDE Promo - Lara Martin', description='Meet Lara Martin, a Flutter/Dart Google Developers Expert and get inspired by her journey. Watch now for a preview of her story! #GDESpotlights #IncludedWithGoogle\n\nLearn about the GDE program → https://goo.gle/2qWOvAy\n\nGoogle Developers Experts → https://goo.gle/GDE\nSubscribe to Google Developers → https://goo.gle/developers'))]
+
Get your activities:
+>>> r = api_with_token.get_activities_by_me()
+>>> r.items
+[Activity(kind='youtube#activity', id='MTUxNTc0OTk2MjI3NDE0MjYwMDY1NjAwODA=', snippet=ActivitySnippet(title='华山日出', description='冷冷的山头')),
+ Activity(kind='youtube#activity', id='MTUxNTc0OTk1OTAyNDE0MjYwMDY1NTc2NDg=', snippet=ActivitySnippet(title='海上日出', description='美美美'))]
+
Get your video captions:
+>>> r = api.get_captions_by_video(video_id="oHR3wURdJ94", parts=["id", "snippet"])
+>>> r
+CaptionListResponse(kind='youtube#captionListResponse')
+>>> r.items
+[Caption(kind='youtube#caption', id='SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:40:49.981Z')),
+ Caption(kind='youtube#caption', id='fPMuDm722CIRcUAT3NTPQHQZJZJxt39kU7JvrHk8Kzs=', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:39:46.991Z'))]
+
If you already have caption id(s), you can get video caption by id(s):
+>>> r = api.get_captions_by_video(video_id="oHR3wURdJ94", parts=["id", "snippet"], caption_id="SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I")
+>>> r
+CaptionListResponse(kind='youtube#captionListResponse')
+>>> r.items
+[Caption(kind='youtube#caption', id='SwPOvp0r7kd9ttt_XhcHdZthMwXG7Z0I', snippet=CaptionSnippet(videoId='oHR3wURdJ94', lastUpdated='2020-01-14T09:40:49.981Z'))]
+
You can get channel sections by self id or belonged channel id or your own channel.
+Get channel sections by channel id:
+>>> r = api.get_channel_sections_by_channel(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw")
+>>>> r
+ChannelSectionResponse(kind='youtube#channelSectionListResponse')
+>>> r.items
+[ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.B8DTd9ZXJqM'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.MfvRjkWLxgk'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.fEjJOXRoWwg'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.PvTmxDBxtLs'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.pmcIOsL7s98'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.c3r3vYf9uD0'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.ZJpkBl-mXfM'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.9_wU0qhEPR8'),
+ ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.npYvuMz0_es')]
+
Get authorized user's channel sections:
+>>> r = api.get_channel_sections_by_channel(mine=True)
+>>> r.items
+[ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.jNQXAC9IVRw'),
+ ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.LeAltgu_pbM'),
+ ChannelSection(kind='youtube#channelSection', id='UCa-vrCLQHviTOVnEKDOdetQ.nGzAI5pLbMY')]
+
Get channel section detail info by id:
+>>> r = api.get_channel_section_by_id(section_id="UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE")
+>>> r
+ChannelSectionResponse(kind='youtube#channelSectionListResponse')
+>>> r1.items
+[ChannelSection(kind='youtube#channelSection', id='UC_x5XG1OV2P6uZZ5FSM9Ttw.e-Fk7vMPqLE')]
+
You can get a list of content regions that the YouTube website supports:
+>>> r = api.get_i18n_regions(parts=["snippet"])
+>>> r.items
+[I18nRegion(kind='youtube#i18nRegion', id='DZ', snippet=I18nRegionSnippet(gl='DZ', name='Algeria')),
+ I18nRegion(kind='youtube#i18nRegion', id='AR', snippet=I18nRegionSnippet(gl='AR', name='Argentina')),
+ I18nRegion(kind='youtube#i18nRegion', id='AU', snippet=I18nRegionSnippet(gl='AU', name='Australia'))
+ ...]
+
You can get a list of application languages that the YouTube website supports:
+>>> r = api.get_i18n_languages(parts=["snippet"])
+>>> r.items
+[I18nLanguage(kind='youtube#i18nLanguage', id='af', snippet=I18nLanguageSnippet(hl='af', name='Afrikaans')),
+ I18nLanguage(kind='youtube#i18nLanguage', id='az', snippet=I18nLanguageSnippet(hl='az', name='Azerbaijani')),
+ I18nLanguage(kind='youtube#i18nLanguage', id='id', snippet=I18nLanguageSnippet(hl='id', name='Indonesian')),
+ ...]
+
The API request must be authorized by the channel owner.
+You can retrieve a list of members (formerly known as "sponsors") for a channel:
+>>> r = api_with_token.get_members(parts=["snippet"])
+>>> r.items
+[MemberListResponse(kind='youtube#memberListResponse'),
+ MemberListResponse(kind='youtube#memberListResponse')]
+
The API request must be authorized by the channel owner.
+You can retrieve a list membership levels for a channel:
+>>> r = api_with_token.get_membership_levels(parts=["snippet"])
+>>> r.items
+[MembershipsLevelListResponse(kind='youtube#membershipsLevelListResponse'),
+ MembershipsLevelListResponse(kind='youtube#membershipsLevelListResponse')]
+
You can retrieve a list of reasons that can be used to report abusive videos:
+>>> r = api_with_token.get_video_abuse_report_reason(parts=["snippet"])
+>>> r.items
+[VideoAbuseReportReason(kind='youtube#videoAbuseReportReason'),
+ VideoAbuseReportReason(kind='youtube#videoAbuseReportReason')]
+
You can use those methods to search the video,playlist,channel data. For more info, you can see +the Search Request Docs.
+You can search different type of resource with keywords:
+>>> r = api.search_by_keywords(q="surfing", search_type=["channel","video", "playlist"], count=5, limit=5)
+>>> r.items
+[SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult')]
+
You can search your app send videos:
+>>> r = api_with_token.search_by_developer(q="news", count=1)
+>>> r.items
+[SearchResult(kind='youtube#searchResult')]
+
You can search your videos:
+>>> r = api_with_token.search_by_mine(q="news", count=1)
+>>> r.items
+[SearchResult(kind='youtube#searchResult')]
+
Or you can build your request using the search
method:
>>> r = api.search(
+... location="21.5922529, -158.1147114",
+... location_radius="10mi",
+... q="surfing",
+... parts=["snippet"],
+... count=5,
+... published_after="2020-02-01T00:00:00Z",
+... published_before="2020-03-01T00:00:00Z",
+... safe_search="moderate",
+... search_type="video")
+>>> r.items
+[SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult')]
+
+>>> r = api.search(
+... event_type="live",
+... q="news",
+... count=3,
+... parts=["snippet"],
+... search_type="video",
+... topic_id="/m/09s1f",
+... order="viewCount")
+>>> r.items
+[SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult'),
+ SearchResult(kind='youtube#searchResult')]
+
We have refactored the project code to support more methods and improve code usability.
+And new structure like follows.
+ +In this structure, we identify each entity as a class of resources and perform operations on the resources.
+Client is exposed via the pyyoutube.Client
class.
You can initialize it with api key
, to get public data.
from pyyoutube import Client
+
+cli = Client(api_key="your api key")
+
If you want to update your channel data. or upload video. You need initialize with access token
, Or do auth flow.
from pyyoutube import Client
+
+cli = Client(access_token="Access Token with permissions")
+
from pyyoutube import Client
+
+cli = Client(client_id="ID for app", client_secret="Secret for app")
+# Get authorization url
+cli.get_authorize_url()
+# ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=scope&state=PyYouTube&access_type=offline&prompt=select_account', 'PyYouTube')
+# Click url and give permissions.
+# Copy the redirected url.
+cli.generate_access_token(authorization_response="redirected url")
+# AccessToken(access_token='token', expires_in=3599, token_type='Bearer')
+
Only web
and some installed
type client_secrets are supported.
The fields client_id
and client_secret
must be set.
If the field redirect_uris
has 1 or more values set, the Client.DEFAULT_REDIRECT_URI
will be set to the first entrie.
from pyyoutube import Client
+
+file_path = "path/to/client_secret.json"
+cli = Client(client_secret_path=file_path)
+
+# Then go through auth flow descriped above
+
Once initialize client. Then you can operate API to get data.
+The API supports the following methods for channels
resources:
resp = cli.channels.list(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw")
+# ChannelListResponse(kind='youtube#channelListResponse')
+print(resp.items)
+# [Channel(kind='youtube#channel', id='UC_x5XG1OV2P6uZZ5FSM9Ttw')]
+
import pyyoutube.models as mds
+
+body = mds.Channel(
+ id="channel id",
+ brandingSettings=mds.ChannelBrandingSetting(
+ image=mds.ChannelBrandingSettingImage(
+ bannerExternalUrl="new banner url"
+ )
+ )
+)
+
+channel = cli.channels.update(
+ part="brandingSettings",
+ body=body
+)
+print(channel.brandingSettings.image.bannerExternalUrl)
+# 'https://yt3.googleusercontent.com/AegVxoIusdXEmsJ9j3bcJR3zuImOd6TngNw58iJAP0AOAXCnb1xHPcuEDOQC8J85SCZvt5i8A_g'
+
The API supports the following methods for videos
resources.
Retrieves the ratings that the authorized user gave to a list of specified videos.
+resp = cli.videos.get_rating(video_id="Z56Jmr9Z34Q")
+
+print(resp.items)
+# [VideoRatingItem(videoId='Z56Jmr9Z34Q', rating='none')]
+
Returns a list of videos that match the API request parameters.
+resp = cli.videos.list(video_id="Z56Jmr9Z34Q")
+
+print(resp.items)
+# [Video(kind='youtube#video', id='Z56Jmr9Z34Q')]
+
Uploads a video to YouTube and optionally sets the video's metadata.
+import pyyoutube.models as mds
+from pyyoutube.media import Media
+
+body = mds.Video(
+ snippet=mds.VideoSnippet(
+ title="video title",
+ description="video description"
+ )
+)
+
+media = Media(filename="video.mp4")
+
+upload = cli.videos.insert(
+ body=body,
+ media=media,
+ parts=["snippet"],
+ notify_subscribers=True
+)
+
+video_body = None
+
+while video_body is None:
+ status, video_body = upload.next_chunk()
+ if status:
+ print(f"Upload progress: {status.progress()}")
+
+print(video_body)
+# {"kind": "youtube#video", "etag": "17W46NjVxoxtaoh1E6GmbQ2hv5c",....}
+
Updates a video's metadata.
+import pyyoutube.models as mds
+
+body = mds.Video(
+ id="fTK1Jj6QlDw",
+ snippet=mds.VideoSnippet(
+ title="What a nice day",
+ description="Blue sky with cloud. updated.",
+ categoryId="1",
+ )
+)
+
+resp = cli.videos.update(
+ parts=["snippet"],
+ body=body,
+ return_json=True,
+)
+print(resp)
+# {"kind": "youtube#video", "etag": "BQUtovVd0TBJwC5S8-Pu-dK_I6s", "id": "fTK1Jj6QlDw", "snippet": {"publishedAt": "2022-12-15T03:45:16Z", "channelId": "UCa-vrCLQHviTOVnEKDOdetQ", "title": "What a nice day", "description": "Blue sky with cloud. updated.", "thumbnails": {"default": {"url": "https://i.ytimg.com/vi/fTK1Jj6QlDw/default.jpg", "width": 120, "height": 90}, "medium": {"url": "https://i.ytimg.com/vi/fTK1Jj6QlDw/mqdefault.jpg", "width": 320, "height": 180}, "high": {"url": "https://i.ytimg.com/vi/fTK1Jj6QlDw/hqdefault.jpg", "width": 480, "height": 360}, "standard": {"url": "https://i.ytimg.com/vi/fTK1Jj6QlDw/sddefault.jpg", "width": 640, "height": 480}, "maxres": {"url": "https://i.ytimg.com/vi/fTK1Jj6QlDw/maxresdefault.jpg", "width": 1280, "height": 720}}, "channelTitle": "ikaros data", "categoryId": "1", "liveBroadcastContent": "none", "localized": {"title": "What a nice day", "description": "Blue sky with cloud. updated."}, "defaultAudioLanguage": "en-US"}}
+
Deletes a YouTube video.
+cli.videos.delete(video_id="fTK1Jj6QlDw")
+# True
+
Add a like or dislike rating to a video or remove a rating from a video.
+cli.videos.rate(video_id="fTK1Jj6QlDw", rating="like")
+# True
+
Report a video for containing abusive content.
+import pyyoutube.models as mds
+
+body = mds.VideoReportAbuse(
+ videoId="fTK1Jj6QlDw",
+ reasonId="32"
+)
+cli.videos.report_abuse(body=body)
+# True
+