luaprovider

package module
v0.20.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: Unlicense Imports: 32 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

a logo depicting a moon

Lua Provider

Go Reference

Warning

The API is not stable and may change at any time.

This is a generic provider for libmangal that uses Lua scripts to create subproviders.

It uses native go implementation of the Lua interpreter and provides a set of libraries that can be used in the scripts.

Note: This is a fork of mangalorg/luaprovider.

Warning

Provider scripts are code: they run with your network access (http, headless browsers) and the SDK libraries. Scripts get no io/os and require only loads files from the provider's own package paths, but only install providers you trust. Requests aren't limited to the internet: a script can also reach localhost and your LAN (a router, a FlareSolverr, other local services) unless the app guards the HTTPClient it passes (mangal does, see its providers.allow_private_addresses). Headless browsers aren't covered by that.

Features

  • Built-in Lua VM without CGO with gopher-lua
  • Batteries-included library
  • Ships with a CLI helper tool for generating script templates
  • Luadoc generation which enables autocompletion for your IDE

Note

It is recommended to use lua-language-server to get nice completions for your IDE

VSCode extension

Usage

script, err := os.ReadFile("main.lua")
if err != nil {
	panic(err)
}
info := libmangal.ProviderInfo{ID: "weebcentral", Name: "Weeb Central", Version: "0.3.1"}

loader, err := luaprovider.NewLoader(script, info, luaprovider.DefaultOptions())
if err != nil {
	panic(err)
}
client, err := libmangal.NewClient(loader, libmangal.DefaultClientOptions())
if err != nil {
	panic(err)
}
defer client.Close()

mangas, err := client.SearchMangas("dandadan")

mangal loads scripts from its providers directory, with the info from a mangal.toml next to each script (see saturno).

Scripts

Overview

See examples of scripts.

See the SDK documentation.

Scripts must look like this:

function SearchMangas(query)
  return {}
end

function MangaVolumes(manga)
 return {}
end

function VolumeChapters(volume)
 return {}
end

function ChapterPages(chapter)
  return {}
end

Notice the four required global functions

  • SearchMangas - searches for mangas based on the given query.
  • MangaVolumes - gets manga volumes. Each manga must have at least 1 volume.
  • VolumeChapters - gets chapters of the given volume.
  • ChapterPages - gets pages of the given chapter.

See the template for the fields each of them can return.

Optionally, MangaByID(id) gets the manga with the given id (the id its search results have), or returns nil if there's none (a not found response:check() error counts as none too). Apps use it to get a manga again without searching; without it they search and match the id.

Errors raised with error() are plain messages. For HTTP errors, response:check() raises one the app can tell apart (not found, rate limited), and send already does for 429.

If the site documents a rate limit, set it when the script loads with sdk.http.rate_limit(permits, seconds[, match[, burst]]), e.g. sdk.http.rate_limit(1, 2, "site.com") for one request every 2 seconds to site.com. Requests (page images too) wait for every limit they match.

For a site behind a Cloudflare challenge, request:flaresolverr() sends that request through FlareSolverr when the app has one configured (sdk.http.has_flaresolverr(), else it raises), e.g. sdk.http.request("GET", url):flaresolverr():send(). Only GET and POST; FlareSolverr doesn't pass the request's headers or cookies, and json comes back inside the page's <pre>. It's slow, so use it only where needed. Apps set Options.FlareSolverr to a function making the transport (e.g. mangoprovider's flaresolverr.NewTransportClient); one session per loaded provider, closed with it.


The scripts can load sdk with

local sdk = require("sdk")

Which provides these packages:

Documentation.

Packages

Script Development

Install the helper tool

just install-cmd

Then use it to generate a new workflow

lua-provider-gen -sdk -provider -luarc

This command will create the following files:

  • sdk.lua - gives IDE autocompletion
  • main.lua - a provider script template (mangal loads main.lua next to a mangal.toml)
  • .luarc.json - a language server configuration

Credits

Moon logo: Moon phase icons created by vectorsmarket15 - Flaticon

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound    = luahttp.ErrNotFound
	ErrRateLimited = luahttp.ErrRateLimited
)

Errors to check with errors.Is (the same values as libmangal.ErrNotFound/ErrRateLimited), from what the scripts' requests answered (see the http lib's response:check()).

View Source
var ErrClosed = errors.New("provider closed")

ErrClosed is returned by the calls to a closed provider.

Functions

func LuaDoc

func LuaDoc() string

LuaDoc will generate library documentation so that language servers can benefit from it.

It's optimized for the https://github.com/LuaLS/lua-language-server

func LuaTemplate

func LuaTemplate() string

LuaTemplate will generate template for the valid lua script used by this provider.

func LuarcJSON

func LuarcJSON() string

func NewLoader

func NewLoader(script []byte, info lm.ProviderInfo, options Options) (lm.ProviderLoader, error)

NewLoader creates new lua provider loader for the given script.

It won't run the script itself.

Types

type FilterOptions added in v0.15.0

type FilterOptions = options.Filter

FilterOptions are the same as mangoprovider's.

func DefaultFilterOptions added in v0.15.0

func DefaultFilterOptions() FilterOptions

type HTTPError added in v0.15.8

type HTTPError = luahttp.Error

HTTPError is a request that answered an error status other than 429, see the http lib.

type Options

type Options struct {
	// HTTPClient HTTP client to use for all requests. It's used as is: keeping a script's
	// requests off private addresses is the app's job (mangal guards it). Headless browsers
	// don't go through it.
	HTTPClient *http.Client

	// UserAgent to use for all HTTP requests.
	UserAgent string

	// CacheStore returns a gokv.Store implementation for use as a cache storage.
	CacheStore func(dbName, bucketName string) (gokv.Store, error)

	// PackagePaths is a list of optional extra package paths.
	PackagePaths []string

	// Filter options, scripts read them from sdk.options.
	Filter FilterOptions

	// RateLimits of the provider's requests (e.g. from its manifest), on top of the ones the
	// script sets with sdk.http.rate_limit. Page images are limited too.
	RateLimits []luahttp.RateLimit

	// FlareSolverr makes the transport scripts' request:flaresolverr() goes through, e.g.
	// mangoprovider's flaresolverr.NewTransportClient(url, client). Called once per provider
	// load, on the first such request; a transport that is an io.Closer (its session) is
	// closed with the provider. Nil: FlareSolverr isn't configured, those requests raise.
	FlareSolverr func() http.RoundTripper

	// Settings: Optional, the user's values of the provider's declared settings
	// (libmangal.ProviderInfo.Settings), asked when it loads; scripts read them with
	// sdk.settings.get. Nil: every setting has its default.
	Settings lm.SettingsFunc

	// Debug logs what doesn't go through HTTPClient: cache hits, FlareSolverr requests and
	// headless page loads, in netutil.LoggingTransport's format. Nothing if nil.
	Debug func(format string, a ...any)
}

Options are the general luaprovider options.

func DefaultOptions

func DefaultOptions() Options

type RateLimitError added in v0.18.0

type RateLimitError = luahttp.RateLimitError

RateLimitError is a request that answered 429.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址