Skip to content

Commit

Permalink
refactor: major fixes and restructuring
Browse files Browse the repository at this point in the history
Changes:
- Convert the program into a package with separate modules for better organisation
- Support running directly with uvicorn via 'python -m uvicorn gallery_dl_server:app' and as a package via 'python -m gallery_dl_server'
- Update README with important clarifications and instructions
- Move important templates to new 'docs' subfolder
- Update file paths in workflow
- Update '.ignore' files
- Add '.gitattributes' file for specifying line endings
- New default configuration file with more options and better formatting
- Terminate the startup shell script if it is not executed inside the Docker container
- Print an empty line to separate the startup logs and the server logs
- Remove redundant update function and update route
- Do not create and take ownership of the '/.cache/pip' and '/.local' directories inside the Docker container as permissions issues have been resolved and it is no longer intended for project dependencies to be updated inside the container
- Support loading from multiple configuration file locations: if loading is a success, log the list of config files loaded; if loading was a failure due to an OSError (exit code 1), log the list of valid config file paths
- Only copy the default configuration file into the directory mounted to '/config' inside the Docker container if none of the multiple valid configuration files are present
- Use the default OS-dependent gallery-dl configuration file locations if not running in the Docker container
- Moved functions for manipulating the config dict to new 'config' module
- Function for removing values from the config dict can now remove two consecutive items from nested lists based on the value of the second item; this is useful for removing gallery-dl command line arguments that accept a value where the argument and its value are separate list items
- New function for adding entries recursively to the config dict without unintentionally overwriting existing values
- Run the download script in a subprocess as a module (via Python's '-m' flag) to allow the subprocess to use relative imports and share its logging configuration with the main process
- Load and update the gallery-dl configuration inside the download subprocess to allow the download job to access the loaded configuration
- Initiate gallery-dl downloads in the subprocess via its 'job' module instead of invoking the subprocess with a system call directly to gallery-dl; this gives more fine-grained control and crucially prevents initialisation of the config dict which fixes the overriding options by preserving modifications to the config dict throughout the download subprocess
- Fix 'extract-audio' override option
- New functions located in the 'output' module to configure logging for the main process and gallery-dl
- Configure gallery-dl logging manually via its own 'output' module and set up a separate logger for writing unsupported URLs to a designated 'unsupported file'
- Configure logging for gallery-dl only after the configuration file is loaded to allow the config file options to be used for the logging configuration
- Use a custom logger for the main process that does not propagate to the root logger, which is configured and utilised instead by gallery-dl
- Exception handling for the gallery-dl download job which logs the name of the exception and returns the correct exit code
- Ensure the subprocess stdout is flushed each time it is written to so that log messages arrive in the intended order
- Determine the piped subprocess stdout log message level by prepending a custom prefix to the log message, which is removed before the message is logged
- If running in the Docker container, save a backup copy of the log file if it is not empty on server shutdown to '/config/logs' and append the filename with the date and time so that previous container logs will not be overwritten
- Force the file handler to open the log file with UTF-8 encoding for compatibility
  • Loading branch information
qx6ghqkz committed Jan 2, 2025
1 parent a4201a6 commit 0a66a01
Show file tree
Hide file tree
Showing 16 changed files with 1,881 additions and 1,030 deletions.
19 changes: 17 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
# Visual Studio
.vscode/

# Cache
__pycache__/
.ruff_cache/

# Scripts
scripts/
*.bat
*.cmd
*.ps1

# Environments
env/
venv/
.env
.venv

# Images
images/

# Git
.git/
.gitattributes
.gitignore

# pre-commit
.pre-commit-config.*
.ruff_cache/

# Docker
**/docker-compose.*
.dockerignore
docker-compose.*
Dockerfile

# Repo
.github/
LICENSE
README.md
SECURITY.md

# Misc
drafts/
gallery-dl/
logs/
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sh text eol=lf
4 changes: 2 additions & 2 deletions .github/workflows/docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ on:
- 'main'
- 'test'
paths:
- 'docs/gallery-dl.conf'
- 'gallery_dl_server/**'
- 'icons/**'
- 'templates/**'
- '**.py'
- 'Dockerfile'
- 'gallery-dl.conf'
- 'requirements.txt'
- 'start.sh'
tags:
Expand Down
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Cache
__pycache__/

# Scripts
scripts/
*.bat
*.cmd
*.ps1

# Environments
env/
venv/
.env
.venv

# Misc
drafts/
gallery-dl/
logs/
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ ENV GID=1000

RUN groupadd --gid $GID $GROUP \
&& useradd --home-dir $(pwd) --no-create-home --shell /bin/sh --gid $GID --uid $UID $USER \
&& mkdir -p /.cache/pip /.local \
&& chown -R $UID:$GID . /.cache/pip /.local \
&& chown -R $UID:$GID . \
&& chmod +x ./start.sh

CMD ["./start.sh"]
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ docker run -d \

This is an example service definition that could be put in `docker-compose.yaml`. This service uses a VPN client container for its networking.

[Gluetun](https://github.com/qdm12/gluetun) is recommended for VPN usage. See [docker-compose.yaml](https://github.com/qx6ghqkz/gallery-dl-server/blob/main/docker-compose.yaml) for a template.
[Gluetun](https://github.com/qdm12/gluetun) is recommended for VPN usage. See [docs/docker-compose.yaml](docs/docker-compose.yaml) for a template.

```yaml
services:
Expand All @@ -49,16 +49,28 @@ services:
restart: on-failure
```
**Important**: Make sure you mount the directory containing the config file rather than the file itself. This ensures changes to the config file are propagated to the running Docker container and you will not need to restart the container. More information on this issue [here](https://github.com/moby/moby/issues/15793#issuecomment-135411504).
#### Important Notes
You can use the environment variables `UID` and `GID` to change the user ID and group ID of the user running the server process. This is important because downloaded files will be owned by this user. Make sure the IDs match those of the user on your host system. The default `UID:GID` is `1000:1000` when left unspecified.
- Make sure you mount the directory containing the config file rather than the file itself. This ensures changes to the config file are propagated to the running Docker container and you will not need to restart the container. More information on this issue [here](https://github.com/moby/moby/issues/15793#issuecomment-135411504).
- The output download directory depends on the `base-directory` in your gallery-dl config file. Make sure it is the absolute path `/gallery-dl/` instead of the relative path `./gallery-dl/` or you will need to mount your download directory to `/usr/src/app/gallery-dl` instead (not recommended).

- You can use the environment variables `UID` and `GID` to change the user ID and group ID of the user running the server process. This is important because downloaded files will be owned by that user. Make sure the IDs match those of the user on your host system. The default `UID:GID` is `1000:1000` when left unspecified.

### Python

If you have Python ^3.6.0 installed in your PATH you can simply run like so. The -u flag is necessary to catch the output immediately.
If you have Python 3.9 or above installed and on your PATH, you can simply run the server using the command line. Clone this repository and install the required dependencies located in `requirements.txt` in a virtual environment.

Run the command below in the root folder while inside the virtual environment. On Windows, replace `python3` with `python`. The `-u` flag is to force the stdout and stderr streams to be unbuffered for real-time logging.

```shell
python3 -u -m uvicorn gallery-dl-server:app --port 9080
python3 -u -m uvicorn gallery_dl_server:app --host "0.0.0.0" --port "9080" --log-level "info" --no-access-log
```

The program can also be run as a package, and optional environment variable overrides can be provided inline. On Windows, this can be done using `set "VAR=value" &&` in Command Prompt or `$env:VAR="value";` in PowerShell.

```shell
HOST="0.0.0.0" PORT="9080" LOG_LEVEL="info" ACCESS_LOG="False" python3 -u -m gallery_dl_server
```

### Port Mapping
Expand Down Expand Up @@ -104,15 +116,20 @@ services:

Configuration of gallery-dl is as documented in the [official documentation](https://github.com/mikf/gallery-dl#configuration).

The configuration file must be mounted inside the Docker container in one of the locations where gallery-dl will check for the config file.
A configuration file is **required.** If running outside of Docker, the [default locations](https://github.com/mikf/gallery-dl#locations) will be used to search for a configuration file. When running *with* Docker, the configuration file must be mounted inside the Docker container in one of the locations where gallery-dl-server will search for the config file.

### Locations

- `/config/gallery-dl.conf`
- `/config/config.json`

The server uses a custom target config location of `/config/gallery-dl.conf`. A [default configuration file](https://github.com/qx6ghqkz/gallery-dl-server/blob/main/gallery-dl.conf) for use with gallery-dl-server has been provided and will automatically be placed in the directory mounted to `/config` if the file does not already exist in that location.
A [default configuration file](docs/gallery-dl.conf) for use with gallery-dl-server has been provided and will automatically be placed in the directory mounted to `/config` if no valid config file exists in that location.

For more information on configuration file options, see [gallery-dl/docs/configuration.rst](https://github.com/mikf/gallery-dl/blob/master/docs/configuration.rst).

Any additional directories specified in the configuration file must also be mounted inside the Docker container, for example if you specify a cookies file location then make sure that location is accessible from within the Docker container.
Any additional locations specified in the configuration file must also exist inside the Docker container. For example, if you specify a cookies file location, make sure that location is accessible from within the Docker container.

It is recommended you place any additional files such as archive, cache and cookies files inside the same `config` directory as `gallery-dl.conf` and then mount that directory to `/config` inside the container, as in the examples.
It is recommended you place any additional files such as archive, cache and cookies files inside the same directory mounted to `/config` along with the config file.

## Usage

Expand Down
File renamed without changes.
Loading

0 comments on commit 0a66a01

Please sign in to comment.