Development guide of bilix
April 24, 2023 Β· View on GitHub
Thank you for your interest in contributing to bilix. Before you start, you can read some tips below. Please note that bilix is rapidly iterating, if you find some content outdated while reading this document, please refer to the code of the master branch.
Before starting
Before everything starts, you need to first fork this repository, and then clone your fork:
git clone https://github.com/your_user_name/bilix
After clone, I recommend you to test and develop in an independent python environment, and then perform local source editable installation after that:
pip install -e .
Try whether the bilix command can be executed normally. Passed the test? At this point,
you can develop bilix locallyπ»
Structure of bilix
Before making any changes to the code, you need to have some understanding of the structure of bilix.
bilix
βββ __init__.py
βββ __main__.py
βββ _process.py # related to multiprocessing
βββ cli
βΒ Β βββ assign.py # assign tasks, dynamically import related
βΒ Β βββ main.py # command line entry
βββ download
βΒ Β βββ base_downloader.py
βΒ Β βββ base_downloader_m3u8.py # basic m3u8 downloader
βΒ Β βββ base_downloader_part.py # basic segmented file downloader
βΒ Β βββ utils.py # some utils for download
βββ exception.py
βββ log.py
βββ progress
βΒ Β βββ abc.py # abstract class of progress
βΒ Β βββ cli_progress.py # progress for cli
βΒ Β βββ ws_progress.py
βββ serve
βΒ Β βββ __init__.py
βΒ Β βββ app.py
βΒ Β βββ auth.py
βΒ Β βββ serve.py
βΒ Β βββ user.py
βββ sites # site support
βββ utils.py # some utils
BaseDownloader
bilix provides two basic downloaders in bilix.download, m3u8 downloader and content range file downloader.
They are based on httpx and even lower-level asyncio and IO multiplexing, and integrate many practical functions
such as speed control, concurrency control, download resume, time range clip, and progress bar display.
The site extension of bilix will be based on these basic downloaders, and the basic downloaders
themselves also provide cli services
How does the downloader provide cli service
In bilix, as long as a class implements the handle method, it can be registered in the command line interface (cli).
The function signature of the handle method is
@classmethod
def handle(cls, method: str, keys: Tuple[str, ...], options: dict):
...
The implementation of the handle function should meet the following three principles:
- If the class thinks that it should not be assigned the download task according to
methodkeysoptions, thehandlefunction should returnNone - If the class can be assigned the task, but finds that the
methodis not within its acceptable range, it should raise aHandleMethodErrorexception - If the class can handle the task, and
methodis within its acceptable range, it should return two values, the first value is the downloader instance, and the second value is the download coroutine
Q: πWhy do I see that some downloaders return the class itself and the download function object?
@classmethod
def handle(cls, method: str, keys: Tuple[str, ...], options: dict):
if method == 'f' or method == 'get_file':
return cls, cls.get_file
A: Just for easy, if the return value is a class and the function object, it will be automatically assembled into an instance and coroutine according to the command line arguments, options and type hint.
How to add support for a site
Under bilix/sites, there are already some sites supported, if you want to add a new site support, you can follow the steps below:
- Create a new site folder under the
sitesfolder, such asexample - Add the site's api module
api.pyunder theexamplefolder, and follow the format of other sites to implement various APIs from input webpage url to output video url and video title - Add the site api module test
api_test.pyunder theexamplefolder, so that everyone can test whether the site is available at any time - Add the site downloader
donwloader.pyunder theexamplefolder, defineDownloaderExampleClass, select the correspondingBaseDownloaderto inherit according to the site, then define the method of downloading the video in the class, and implementhandlemethod. - Add
__init__.pyunder theexamplefolder, importDownloaderExampleclass, and addDownloaderExamplein__all__to facilitate bilix to find your downloader
Okay, let's test it
At present, other developers have contributed to the extension of bilix to other sitesπ, Maybe the accepted New site PR can also help you