qbittorrent

> updating info
This commit is contained in:
Eugene Amos 2023-11-28 14:18:27 -08:00
parent 90f0ec70ca
commit 304ce26b62
2 changed files with 70 additions and 19 deletions

View File

@ -10,9 +10,29 @@
</p> </p>
# :clipboard: Notes: `assign_category.py` # :clipboard: Notes: `assign_category.py`
A script that will auto assign a category of an incoming torrent. A script that will auto assign a category of an incoming torrent.
<details> <details>
<summary><strong>assign_category.py</strong> notes</summary> <summary><strong>assign_category.py</strong> notes</summary>
</br>
This python script will get called every time a torrent file is added to the qbittorrent application.
This script is dependent on the `qbittorrent-api` module that will <a href="https://docs.linuxserver.io/images/docker-qbittorrent" target="blank">get installed</a> on start of the main `qbittorrent` container.
When the script is called it will do the following:
1. Check the torrent files name for a season/episode pattern using the format of `SxxExx`. This check is also for uppercase or lowercase "s" & "e" and any number or combination of numbers in the "xx" spots.
2. Next the script will check if "1080p" or "2160p" is in the name.
- If both #1 and #2 are **TRUE**, then the category for that added torrent will change to `category_1080_shows`.</br> If both or either are **FALSE** then the script will move to #3.
- > Note: When you **right click** on a **category** in qbittorrent you can edit the **save path** location.
3. Next the script will again check for "1080p" or "2160p" AND "YTS.MX" in the name.
- If **TRUE** then the added torrent is a movie and the category will change to `category_1080_movies`.
- > Note: **95%** of the time I only add movies from **YTS.MX**. This can be changed to any movie identifier needed.
</details> </details>
<br /> <br />
@ -20,68 +40,85 @@ A script that will auto assign a category of an incoming torrent.
<br /> <br />
# :clipboard: Notes: `docker-compose-qbittorrent.yml` # :clipboard: Notes: `docker-compose-qbittorrent.yml`
This docker-compose file is traditionally used to create and start a container from the command line. For Portainer we can just copy and paste this script into the web editor field. This docker-compose file is traditionally used to create and start a container from the command line. For Portainer we can just copy and paste this script into the web editor field.
<details> <details>
<summary><strong>docker-compose-qbittorrent.yml</strong> notes</summary> <summary><strong>docker-compose-qbittorrent.yml</strong> notes</summary>
</br> </br>
This will create two services or containers called **qbittorrent** and **qbittorrent-openvpn**. This will create two services or containers called **qbittorrent** and **qbittorrent-openvpn**.
## `qbittorrent container` ## `qbittorrent container`
Key parts to this container are: Key parts to this container are:
- ### **image:** - ### **image:**
- <a href="https://docs.linuxserver.io/images/docker-qbittorrent" target="blank">linuxserver/qbittorrent:latest</a>: &nbsp; P2P bittorrent client specially built by the linuxserver.io team. - <a href="https://docs.linuxserver.io/images/docker-qbittorrent" target="blank">linuxserver/qbittorrent:latest</a>: &nbsp; P2P bittorrent client specially built by the linuxserver.io team.
- ### **volumes:** - ### **volumes:**
- `/home/<USERNAME>/docker/qbittorrent/custom_scripts:/custom-cont-init.d:ro` - `/home/<USERNAME>/docker/qbittorrent/custom_scripts:/custom-cont-init.d:ro`
</br></br> </br></br>
This will be the location where the **linuxserver.io** image looks for custom scripts to run at the start of the container. Our custom script `extra_packages.sh` will need to go in this folder.</br></br> This will be the location where the **linuxserver.io** image looks for custom scripts to run at the start of the container. Our custom script `extra_packages.sh` will need to go in this folder.</br></br>
For more info you can see the <a href="https://docs.linuxserver.io/general/container-customization/#custom-scripts" target="blank">documentation</a>. For more info you can see the <a href="https://docs.linuxserver.io/general/container-customization/#custom-scripts" target="blank">documentation</a>.
- ### **ports:** - ### **ports:**
- The port section will not be used if we are using **qbittorrent-openvpn** container. All web traffic will be diverted threw that container. - The port section will not be used if we are using **qbittorrent-openvpn** container. All web traffic will be diverted threw that container.
- ### **healthcheck:** - ### **healthcheck:**
- A feature of Docker that will check to see if the container is healthy or exited (bad). You will need to change `<SERVER IP ADDRESS>` to home server IP address. - A feature of Docker that will check to see if the container is healthy or exited (bad). You will need to change `<SERVER IP ADDRESS>` to home server IP address.
</br></br> </br></br>
In this configuration it will check to see if the container has a healthy network connection every 60 seconds. On 3 failed attempts it the container will get labeled as `exited`.</br></br> In this configuration it will check to see if the container has a healthy network connection every 60 seconds. On 3 failed attempts it the container will get labeled as `exited`.</br></br>
It is multiple ways or reasons to use **healthcheck** but for our purposes it will be used by the `inspector_qb.py` script which will reboot the container when needed. It is multiple ways or reasons to use **healthcheck** but for our purposes it will be used by the `inspector_qb.py` script which will reboot the container when needed.
- ### **labels:** - ### **labels:**
- `com.centurylinklabs.watchtower.depends-on: "qbittorrent-openvpn"` - Used by watchtower to for starting and stopping containers. This container will not be rebooted before `qbittorrent-openvpn` has already done so. - `com.centurylinklabs.watchtower.depends-on: "qbittorrent-openvpn"` - Used by watchtower to for starting and stopping containers. This container will not be rebooted before `qbittorrent-openvpn` has already done so.
- ### **network_mode:** - ### **network_mode:**
- Tells Docker that the all network activity to container `qbittorrent` will go threw `qbittorrent-openvpn` container. - Tells Docker that the all network activity to container `qbittorrent` will go threw `qbittorrent-openvpn` container.
- ### **depends_on:** - ### **depends_on:**
- Tells Docker that the container `qbittorrent` can only be run/started if `qbittorrent-openvpn` has a healthy healthcheck status. - Tells Docker that the container `qbittorrent` can only be run/started if `qbittorrent-openvpn` has a healthy healthcheck status.
## `qbittorrent-openvpn container` ## `qbittorrent-openvpn container`
The only function of this container is to establish a **VPN** network connection.</br></br> The only function of this container is to establish a **VPN** network connection.</br></br>
Key parts to this container are: Key parts to this container are:
- ### **image:** - ### **image:**
- <a href="https://haugene.github.io/docker-transmission-openvpn" target="blank">haugene/transmission-openvpn</a>: &nbsp; A Docker container used as an OpenVPN network tunnel. - <a href="https://haugene.github.io/docker-transmission-openvpn" target="blank">haugene/transmission-openvpn</a>: &nbsp; A Docker container used as an OpenVPN network tunnel.
- ### **cap_add:** - ### **cap_add:**
- Since we are connecting to a VPN, this container will need admin access to network related functions. - Since we are connecting to a VPN, this container will need admin access to network related functions.
- ### **ports:** - ### **ports:**
- This will open up ports that `qbittorrent` container will need for networking. - This will open up ports that `qbittorrent` container will need for networking.
</br></br> </br></br>
The web port for all `linuxserver.io` images are different in that both the `<external>:<internal>` ports have to be the same. Usually for most Docker containers your `<external>` port can change to whatever you want it to be BUT the `<internal>` port will have to stay the same.</br></br> The web port for all `linuxserver.io` images are different in that both the `<external>:<internal>` ports have to be the same. Usually for most Docker containers your `<external>` port can change to whatever you want it to be BUT the `<internal>` port will have to stay the same.</br></br>
I will be using port **8124** as indicated in the code below. The port can be changed, just remember that both port numbers will need to be the same for the `qbittorrent` container to get a network connection. I will be using port **8124** as indicated in the code below. The port can be changed, just remember that both port numbers will need to be the same for the `qbittorrent` container to get a network connection.
</br></br> </br></br>
``` yaml ``` yaml
8124:8124/tcp # qbittorrent web port 8124:8124/tcp # qbittorrent web port
6881:6881/tcp # qbittorrent tcp connection port 6881:6881/tcp # qbittorrent tcp connection port
6881:6881/udp # qbittorrent udp connection port 6881:6881/udp # qbittorrent udp connection port
``` ```
- ### **environment** - ### **environment**
- **OpenVPN Provider Info:** This is where you will enter your credentials from your VPN provider.</BR></BR> - **OpenVPN Provider Info:** This is where you will enter your credentials from your VPN provider.</BR></BR>
I am using `Privado` for my VPN connection. You can use any provider that uses OpenVPN connections. For a list of supported providers that this container can use <a href="https://haugene.github.io/docker-transmission-openvpn/supported-providers" target="blank">go here</a>. You can also find the correct name to put in the `OPENVPN_PROVIDER` section on the same part of the website. I am using `Privado` for my VPN connection. You can use any provider that uses OpenVPN connections. For a list of supported providers that this container can use <a href="https://haugene.github.io/docker-transmission-openvpn/supported-providers" target="blank">go here</a>. You can also find the correct name to put in the `OPENVPN_PROVIDER` section on the same part of the website.
</BR></BR> </BR></BR>
``` yaml ``` yaml
OPENVPN_PROVIDER=<PROVIDER NAME> OPENVPN_PROVIDER=<PROVIDER NAME>
OPENVPN_USERNAME=<PROVIDER USERNAME> OPENVPN_USERNAME=<PROVIDER USERNAME>
@ -96,39 +133,44 @@ Key parts to this container are:
<br /> <br />
<br /> <br />
# :clipboard: Notes: `extra_packages.sh` # :clipboard: Notes: `extra_packages.sh`
A script that will be executed when the `qbittorrent` container is starting. A script that will be executed when the `qbittorrent` container is starting.
<details> <details>
<summary><strong>extra_packages.sh</strong> notes</summary></br> <summary><strong>extra_packages.sh</strong> notes</summary></br>
This script goes in the `/custom_script` volume folder for **qbittorrent** container. It will install three programs. This script goes in the `/custom_script` volume folder for **qbittorrent** container. It will install three programs.
- **python3:** This is the dependant programming language needed. - **python3:** This is the dependant programming language needed.
- **pip3:** Package install for python. - **pip3:** Package install for python.
- **qbittorrent-api:** Client API used to interact with qbittorrent over web or CLI. - **qbittorrent-api:** Client API used to interact with qbittorrent over web or CLI.
linuxserver.io uses [**Alpine OS**](https://www.alpinelinux.org/releases) to build all images. Alpine OS is a lighter version of linux compared to Linux Mint or Ubuntu. Because of this **qbittorrent-api** is not in the main repo for the OS. As of writing this doc its only edge branch repo. When this script is in the `/custom_script` folder is will get executed every time **qbittorrent** container is started ensuring that it will always be ready when qbittorrent is running. linuxserver.io uses [**Alpine OS**](https://www.alpinelinux.org/releases) to build all images. Alpine OS is a lighter version of linux compared to Linux Mint or Ubuntu. Because of this **qbittorrent-api** is not in the main repo for the OS. As of writing this doc its only edge branch repo. When this script is in the `/custom_script` folder is will get executed every time **qbittorrent** container is started ensuring that it will always be ready when qbittorrent is running.
The first part of the script links to the **edge repo** so we can download and install it. The first part of the script links to the **edge repo** so we can download and install it.
``` shell ``` shell
# Add the "edge" testing repository # Add the "edge" testing repository
echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
``` ```
This will check for updates to the Alpine repo This will check for updates to the Alpine repo
``` shell ``` shell
# Update the package list # Update the package list
apk update apk update
``` ```
This will install **Python3** first then install a version of pip3 for Alpine called **py3-pip** This will install **Python3** first then install a version of pip3 for Alpine called **py3-pip**
``` shell ``` shell
# Install Python and pip # Install Python and pip
apk add python3 py3-pip apk add python3 py3-pip
``` ```
The last part of the script will install Alpines version of **qbittorrent-api** The last part of the script will install Alpines version of **qbittorrent-api**
``` shell ``` shell
# Install qbittorrent-api # Install qbittorrent-api
apk add py3-qbittorrent-api apk add py3-qbittorrent-api
@ -139,4 +181,4 @@ apk add py3-qbittorrent-api
<br /> <br />
<br /> <br />
<br /> <br />
<br /> <br />

View File

@ -1,22 +1,29 @@
#!/usr/lib/python3 #!/usr/bin/python3
# #
# qBittorrent-api: # qBittorrent-api:
# https://qbittorrent-api.readthedocs.io/en/v2023.4.47/introduction.html # https://qbittorrent-api.readthedocs.io/en/v2023.4.47/introduction.html
# #
# #
# #
# Make sure to set the folling variables.
# <USERNAME>
# <SERVER IP ADDRESS>
# <PORT>
#
#
import re
import sys import sys
import qbittorrentapi import qbittorrentapi
# User access info # qbittorrent webui login crendentials.
qbitt_username = '<username>' qbitt_username = '<USERNAME>'
qbitt_password = '<password>' qbitt_password = '<PASSWORD>'
# qbittorrent webui address. # qbittorrent webui address.
#url = 'http://192.168.1.59:8080' #url = 'http://192.168.1.59:8080'
url = 'http://<ip_address>:<port>' url = 'http://<SERVER IP ADDRESS>:<PORT>'
# The categories to use for matching torrents # The categories to use for matching torrents. Change as needed.
category_1080_shows = "1080+ | Shows" category_1080_shows = "1080+ | Shows"
category_1080_movies = "1080+ | Movies" category_1080_movies = "1080+ | Movies"
@ -31,22 +38,24 @@ torrents = qb.torrents_info()
# loop through the torrents # loop through the torrents
for torrent in torrents: for torrent in torrents:
#print(torrent['name'])
#print(torrent['hash'])
# Get the name of the completed torrent from the command line arguments inside qbittorrent # Get the name of the completed torrent from the command line arguments inside qbittorrent
torrent_name = sys.argv[1] # Name of the added torrent as defined by "%N" in qbittorrent settings. torrent_name = sys.argv[1] # Name of the added torrent as defined by "%N" in qbittorrent settings.
torrent_hash = sys.argv[2] # Hash of the added torrent as defined by "%I" in qbittorrent settings. torrent_hash = sys.argv[2] # Hash of the added torrent as defined by "%I" in qbittorrent settings.
#print(torrent_name)
#print(torrent_hash)
# Checking to see if added torrent is by YTS and is 1080p or 4k. # Define the regular expression pattern for 'SxxExx' or 'sxxexx' format.
if "1080p" in torrent_name or "2160p" in torrent_name and "YTS.MX" in torrent_name: pattern = r"[Ss]\d{2}[Ee]\d{2}"
# Set the category of the torrent. # Check for 'SxxExx' or 'sxxexx' episode pattern and '1080p' or '2160p'
# If so, assign 1080 show category.
if re.search(pattern, torrent_name) and ("1080p" in torrent_name or "2160p" in torrent_name):
qb.torrents_set_category(category=category_1080_shows, hashes=torrent_hash)
qb.torrents_set_auto_management(hashes=torrent_hash, enable=True)
# Then check for movies criteria.
elif ("1080p" in torrent_name or "2160p" in torrent_name) and "YTS.MX" in torrent_name:
qb.torrents_set_category(category=category_1080_movies, hashes=torrent_hash) qb.torrents_set_category(category=category_1080_movies, hashes=torrent_hash)
# Set torrent to auto management to update the save path. qb.torrents_set_auto_management(hashes=torrent_hash, enable=True)
qb.torrents_set_auto_management(hashes=torrent_hash, enable=True)
# Disconnect from the qBittorrent API # Disconnect from the qBittorrent API
qb.auth_log_out() qb.auth_log_out()