Медиа-сервер на основе Telegram-бота / Питонист пробует Go

Overview

The journey of creating a Telegram bot that transformed my home theater experience began with a simple motivation: the desire to eliminate the tedious process of manually downloading and transferring movies. The concept was straightforward—what if I could simply send a link via Telegram and receive a movie ready for viewing? This vision became a reality through the integration of a Telegram bot with DLNA technology, enabling seamless playback of downloaded films on any device within my home network. This article delves into how a basic idea evolved into a fully functional media server, my transition from Python to Go, and why this bot has become a staple for my family and guests alike. The source code for the project is available on GitHub: [telegram-media-server].

Motivation

The inspiration for developing a Telegram bot to manage media files for my home theater emerged from a common frustration: the manual effort involved in accessing content. I envisioned a service that would allow me to download a film or video with just a couple of clicks and immediately launch it on my television via DLNA. Given that my television is quite dated, I sought to streamline my viewing process, which typically involved several steps:

  • Access the desktop computer
  • Search for a film or video
  • Initiate the download (via torrent or video download services)
  • After downloading, choose a method to transfer to the television:
    • Transfer to a USB drive
    • Set up a DLNA server on the computer to stream content
What is DLNA?

DLNA (Digital Living Network Alliance) is a technology that facilitates the exchange of multimedia content among devices on the same local network. With DLNA, films and videos downloaded by the bot become instantly available for viewing on televisions, media players, or even gaming consoles without complicated setups.

Despite the plethora of media servers available, none met my needs. They either lacked compatibility with my older television or were not user-friendly. Moreover, I found no media server that incorporated a Telegram bot to facilitate the quick download of pirated films from torrents or videos from YouTube. Most media servers could only stream YouTube videos rather than download them entirely, requiring additional proxy configurations that complicated the process.

Thus, the idea of using a Telegram bot to manage media files was born. With Telegram readily accessible to everyone and a robust API for bot creation, the solution proved to be remarkably convenient. Now, my partner and guests can easily access the bot, making it a daily tool for our viewing pleasure.

Functionality

The bot’s functionality includes several key features:

  • Content Downloading
    Downloading films from torrent files
    Downloading streaming videos from various sites (e.g., YouTube, VK, RuTube—thanks to the yt-dlp utility)
  • Torrent File Search
    Thanks to integration with Prowlarr, the bot can locate torrents by movie title and initiate downloads immediately.
  • Automatic Re-encoding
    Automatic re-encoding of videos via ffmpeg—if your television does not support the original format or codec, the bot will convert the video to a suitable format.
    This feature is particularly beneficial for older television models to ensure smooth playback (currently only for streaming videos).
  • Monitoring
    Download monitoring (the bot sends notifications about the download status, allowing users to view the percentage of completion).
  • File Management
    Access to a list of all downloaded media and the ability to delete files with a simple menu button.
  • Proxying
    Traffic proxying for specific domains—such as configuring it solely for YouTube.
  • Roles and Access
    A role-based system and admin panel that allows temporary access to the bot (for example, for guests).

Now, my movie-watching process has been streamlined to searching for a torrent or video link, sending it to the bot, and waiting for a notification that it’s ready to watch via DLNA.

How the Bot Works

The bot employs a straightforward role-based system with password protection. To get started, users must authenticate with the command:

/login

Available Roles:

Role Password Capabilities
Administrator ADMIN_PASSWORD Full access, including issuing temporary passwords
Regular User REGULAR_PASSWORD All functions except managing temporary access
Temporary User Temporary password Only able to add links for download

Main Commands:
In addition to user-friendly buttons, the bot supports intuitive commands familiar to Linux users:

Command Description Example
/start Welcome message /start
/login User authentication /login mypassword123
/ls View all downloaded media /ls
/rm Delete media by ID /rm 1
/rm all Delete all downloads /rm all
/temp Issue temporary access (admin only) /temp 1d

Usage Examples:

  1. Authenticate with the bot
  2. Send a link to a video or torrent file
  3. Track progress through the media list
  4. Receive a notification upon completion

The bot’s download manager allows for simultaneous downloads of multiple torrent files or videos. It can also handle series and automatically delete temporary files post-download, conserving disk space.

Temporary Access for Guests:
The bot allows for the issuance of temporary passwords to guests:

# Issue guest access for 1 day
/temp 1d

# Issue guest access for 3 hours
/temp 3h

# Issue guest access for 30 minutes
/temp 30m

The bot generates a temporary password that can be shared with guests, allowing them to authenticate and add links for download without managing other users’ downloads.

Supported Services:
The bot is compatible with all services supported by yt-dlp.

Multilingual Support:
The bot supports both Russian and English interface languages. Language selection can be managed in the configuration file .env through the LANG parameter:

# Russian language
LANG=ru

# English language
LANG=en

Adding support for additional languages is straightforward by creating the necessary localization files. Detailed installation and setup documentation can be found in the [project repository].

Hardware

In developing this bot, I aimed to run it on low-spec hardware, such as the BeagleBone Green. However, challenges arose. Initially, the bot was developed in Python, my primary development language. Yet, as I neared completion, it became evident that Python was not the ideal choice for universal compatibility across various hardware platforms. Specifically, I faced difficulties in selecting libraries for media downloading that would be accessible across different operating systems and architectures. Additionally, I desired the ability to cross-compile the bot for various architectures and distribute ready-made binaries.

Realizing that my primary stack was not suitable for this task, I began exploring alternative programming languages. After some consideration, I discovered what I believed to be the perfect language for my needs: Go. Although I had no prior experience with Go, I decided to rewrite the bot entirely in this language. While my initial motivation was to learn something new, there were tangible advantages to using Go:

Advantages of Go

  • Cross-compilation for any architecture: Many programming languages support this feature, but Go stands out for its ease of use. Unlike C or C++, which can be cumbersome for developing Telegram bots due to their complex syntax, Go offers a more comfortable experience with a comprehensive standard library. Furthermore, if the program does not utilize libraries written in C/C++, the resulting Go binary is not dependent on glibc.
  • Low entry barrier and its own ecosystem: After spending a few weeks learning the basics, one can create a decent service. Go provides not only the language itself but also built-in linters, formatters, and a very user-friendly package manager that manages dependencies. In contrast, using Python would require multiple third-party tools (such as poetry, flake8, isort, black, etc.).
  • Speed and low resource requirements: Go is a relatively fast programming language and appears to be less demanding on memory. Given the constraints of the BeagleBone Green, which has only 512 MB of RAM and a weak processor, choosing Python initially was not a wise decision.

Ultimately, the bot was completely rewritten, and new features were added that were absent in the Python version. Unfortunately, I was unable to run the bot on the BeagleBone Green due to insufficient RAM, which was consumed by external utilities. However, the bot should still function on any hardware with at least 1 GB of RAM and a Linux operating system. For an extended period, it operated smoothly on the OrangePi Zero 3, which handled the workload admirably, all for a mere cost of around 3000 rubles.

TrendTechie
Медиа-сервер на основе Telegram-бота / Питонист пробует Go