Hello
We're about to embark on an interesting journey: creating a Python 3 package to gather details from YouTube channels. I've whimsically named it 'The Better YouTube Channel Details'. Even if APIs are not usually your thing, this process might still pique your interest.
We'll break down this project into three parts for ease of digestion and to keep your attention focused.
🔹 First up: We're setting up the package and chatting with the API.
🔹 Next: We'll dive into testing our creation.
🔹 And finally: We'll release it to the world at https://packagist.org/
Join me on this adventure; I promise it'll be fun and educational!
Alright, let's start!
We're crafting this in Python 3. Remember, every package has a blueprint it needs to follow. Here's a quick overview, inspired by the official guidelines:
packaging_tutorial/├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── example_package_YOUR_USERNAME_HERE/
│ ├── __init__.py
│ └── example.py
└── tests/
Looks straightforward, right?
Our API will be simple and efficient:
Now, let's set up our project files.
🧙 Magic in the Making
We've got our project structure ready. It's time to focus on what we love: coding! We'll get back to the metadata later.
Let's dive into programming. Our task now is to add a file named src/api_client.py
. This file will handle all interactions with the API. To start, you'll need a rapid_api_key
. Here's how to get it:
X-RapidAPI-Key
displayed there.Here's a sneak peek at our YouTubeChannelDetailsApiClient
class:
class YouTubeChannelDetailsApiClient(object):
def __init__(self, rapid_api_key: str):
self.rapidapi_host = 'the-better-youtube-channel-details.p.rapidapi.com'
self.headers = {
"X-RapidAPI-Key": rapid_api_key,
"X-RapidAPI-Host": self.rapidapi_host,
}
We need a function to communicate with the API, and for that, we'll use the aiohttp
package. But first, install it:
pip install aiohttp
Now, we're all set to send HTTP requests.
We'll create a private function named __get_request
to handle our API requests. Here's what it needs to do:
Let's start coding this function:
async def __get_request(self, path: str, query: dict) -> str:
url = URL(
host=self.rapidapi_host,
path=path,
query=query,
scheme="https"
).as_string()
session_timeout = aiohttp.ClientTimeout(total=45.0)
is_ok = False
while True:
async with aiohttp.ClientSession(headers=self.headers, timeout=session_timeout) as session:
async with session.get(url=url) as response:
try:
json_response = await response.json()
if response.status == 200:
is_ok = True
except aiohttp.client.ContentTypeError:
continue
if is_ok:
break
await asyncio.sleep(0.3)
return json_response
aiohttp
for this. It's important to manage retries and handle potential errors.import asyncio
import aiohttp
from urllib.parse import urlencode
from purl import URL
pip install asyncio
pip install aiohttp
pip install purl
So, we've got our code. But does it work as expected? We'll only find out by testing, which I'll cover in the next post.
I hope you're finding this journey enjoyable and informative. If you like what you're reading, feel free to show some love with a thumbs up or a comment. Your feedback keeps me going!
Thanks for joining me today. Stay tuned for the next installment! 🌟👋