26 Apr 2024
Earlier I wrote a post about How to create your own self-hosted YouTube. When I wrote the post, you could grab the channel ID rather simple. You just opened a video on the channel, and then clicked the channel name just below the video. Then you would have the channel ID in the URL, like this:https://www.youtube.com/channel/UCX6OQ3DkcsbYNE6H8uQQuVA
This method seems to have stopped working however, so let's do it another way.
https://www.googleapis.com/youtube/v3/channels?key=YOUR_API_KEY&forUsername=USERNAME&part=id
Replace USERNAME with the channel name, and YOUR_API_KEY with your API key. If you don't have an API key, I have described how to get one here: How to get your YouTube API key.
{
'kind': 'youtube#channelListResponse',
'etag': '0OJX0B6esbO3cdBfycAScSh5cPM',
'pageInfo': {
'totalResults': 1,
'resultsPerPage': 5
},
'items': [
{
'kind': 'youtube#channel',
'etag': 'xcfFMDuM69tPJiFWrWlfpwuDcqA',
'id': 'UCjFqcJQXGZ6T6sxyFB-5i6A'
}
]
}
Load the JSON into a variable and you can access the ID like this:
id = response['items'][0]['id']
import json
import requests
APIKEY = 'YOUR_API_KEY'
USERNAME = 'CHANNEL_USERNAME'
URL = 'https://www.googleapis.com/youtube/v3/channels?key=' + APIKEY + '&forUsername=' + USERNAME + '&part=id'
request = requests.get(URL)
response = json.loads(request.content)
print(response)
print()
print(response['items'][0]['id'])
The Python script above will output something similar to this:
{
'kind': 'youtube#channelListResponse',
'etag': '0OJX0B6esbO3cdBfycAScSh5cPM',
'pageInfo': {
'totalResults': 1,
'resultsPerPage': 5
},
'items': [
{
'kind': 'youtube#channel',
'etag': 'xcfFMDuM69tPJiFWrWlfpwuDcqA',
'id': 'UCjFqcJQXGZ6T6sxyFB-5i6A'
}
]
}
UCjFqcJQXGZ6T6sxyFB-5i6A
I don't recommend storing your credentials directly in your scripts. Instead I store my credentials in a .env-file. You can read how I use them here: Using .env-files for storing credentials.
Removing EXIF data from an image using Python
Published 2024-09-18 — Updated 2024-11-21
Python
EXIF data is information that is embedded within digital images and is automatically generated by digital cameras and smartphones.
Read the post →Quick and easy image recognition with 9 lines of code in Python
Published 2024-05-05 — Updated 2024-07-28
Machine Learning
Python
Need a quick and easy image recognition solution in Python? Learn how to create one in 9 lines of code.
Read the post →How to easily web scrape any website with Python
Published 2024-05-03
Datahoarding
Notes
Python
Web development
Learn how to easily web scrape any website using Python. I go through the various techniques I use.
Read the post →