msbimport

package
v0.0.0-...-0f7311a Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: GPL-3.0 Imports: 21 Imported by: 2

README

Moe-Sticker-Bot Import Component (msbimport)

Go Reference

Description

This package is intended to fetch, parse, download and convert LINE and KakaoTalk Stickers from share link.

It is designed to be able to operate independentaly from moe-sticker-bot core so third party apps can also utilize this package.

此套件或CLI用於解析LINE/Kakaotalk貼圖的分享連結並下載和轉換。

此套件可獨立於moe-sticker-bot使用, 第三方App可以輕鬆利用此套件或CLI處理複雜貼圖。

CLI Usage/終端機程式使用

Source code of CLI is on: /moe-sticker-bot/cmd/msbimport

Download msbimport: 下載msbimporthttps://github.com/star-39/moe-sticker-bot/releases

Install dependencies:

# For Fedora / RHEL / CentOS etc. (Requires RPM Fusion)
dnf install ImageMagick libwebp bsdtar curl ffmpeg gifsicle
# For Debian / Ubuntu etc.
apt install imagemagick libarchive-tools curl ffmpeg gifsicle
# For Arch
pacman -S install ffmpeg imagemagick curl libarchive gifsicle bsdtar
# For macOS
brew install imagemagick ffmpeg curl bsdtar gifsicle

install tools/msb_kakao_decrypt.py /usr/local/bin/
install tools/msb_rlottie.py /usr/local/bin/
msbimport --help
Usage of ./msbimport:
  -convert
    	Convert to Telegram format(WEBP/WEBM) 轉換為Telegram格式。
  -dir string
    	Where to put sticker files. 指定存放貼圖檔的資料夾。
  -json
    	Output JSON serialized LineData, useful when integrating with other apps. 列印LineData為JSON。
  -link string
    	Import link(LINE/kakao) 分享連結。
  -log_level string
    	Log level (default "debug")
        
msbimport --link https://store.line.me/stickershop/product/27286

msbimport --link https://store.line.me/stickershop/product/27286 --convert --json

API Usage

A typical workflow is to call parseImportLink then prepareImportStickers.

go get -u https://github.com/star-39/moe-sticker-bot
import "github.com/star-39/moe-sticker-bot/pkg/msbimport"

//Create a context, which can be used to interrupt the process.
ctx, _ := context.WithCancel(context.Background())

//Create a empty LineData struct pointer.
ld := &msbimport.LineData{}

//LineData will be parsed to ld.
warn, err := msbimport.ParseImportLink("https://store.line.me/stickershop/product/27286", ld)
if err != nil {
    //handle error here.
}
if warn != "" {
    //handle warning message here.
}

err = msbimport.PrepareImportStickers(ctx, ld, "./", false)
if err != nil {
    //handle error here.
}

//If I18n title is needed(LINE only), TitleWg must be waited.
ld.TitleWg.Wait()
println(ld.I18nTitles)

for _, lf := range ld.Files {
    //Each file has its own waitgroup and musted be waited.
    lf.Wg.Wait()
    if lf.CError != nil {
        //hanlde sticker error here.
    }
    println(lf.OriginalFile)
    println(lf.ConvertedFile)
    //...
}

//Your stickers files will appear in the work dir you specified.

License

GPL v3 License.

Source code of this package MUST ALWAYS be disclosed no matter what use case is,

and source code referring to this package MUST ALSO be discolsed and share the same GPL v3 License.

Documentation

Index

Constants

View Source
const (
	FORMAT_TG_REGULAR_STATIC   = "tg_reg_static"
	FORMAT_TG_EMOJI_STATIC     = "tg_emoji_static"
	FORMAT_TG_REGULAR_ANIMATED = "tg_reg_ani"
	FORMAT_TG_EMOJI_ANIMATED   = "tg_emoji_ani"
)
View Source
const (
	// Decimal
	KB = 1000
	MB = 1000 * KB
	GB = 1000 * MB
	TB = 1000 * GB
	PB = 1000 * TB

	// Binary
	KiB = 1024
	MiB = 1024 * KiB
	GiB = 1024 * MiB
	TiB = 1024 * GiB
	PiB = 1024 * TiB
)

See: http://en.wikipedia.org/wiki/Binary_prefix

View Source
const (
	LINE_STICKER_STATIC       = "line_s"  //普通貼圖
	LINE_STICKER_ANIMATION    = "line_a"  //動態貼圖
	LINE_STICKER_POPUP        = "line_p"  //全螢幕
	LINE_STICKER_POPUP_EFFECT = "line_f"  //特效
	LINE_EMOJI_STATIC         = "line_e"  //表情貼
	LINE_EMOJI_ANIMATION      = "line_i"  //動態表情貼
	LINE_STICKER_MESSAGE      = "line_m"  //訊息
	LINE_STICKER_NAME         = "line_n"  //隨你填
	KAKAO_EMOTICON            = "kakao_e" //KAKAOTALK普通貼圖

	LINE_SRC_PER_STICKER_TEXT = "PER_STICKER_TEXT"
	LINE_SRC_ANIMATION        = "ANIMATION"
	LINE_SRC_STATIC           = "STATIC"
	LINE_SRC_POPUP            = "POPUP"
	LINE_SRC_NAME_TEXT        = "NAME_TEXT"

	//Indicates popup effect, bot popup APNG and static PNG should be kept.
	LINE_POPUP_LAYER_BACKGROUND = "BACKGROUND"
	//Indicates popup, only popup APNG needed.
	LINE_POPUP_LAYER_FOREGROUND = "FOREGROUND"

	StoreLine  = "line"
	StoreKakao = "kakao"

	WARN_KAKAO_PREFER_SHARE_LINK = "prefer share link for kakao"
)

Line sticker types

Variables

View Source
var BSDTAR_BIN = "bsdtar"
View Source
var CONVERT_ARGS []string
View Source
var CONVERT_BIN = "convert"
View Source
var FFMPEG_BIN = "ffmpeg"

Functions

func ArchiveExtract

func ArchiveExtract(f string) []string

func CheckDeps

func CheckDeps() []string

Check if required dependencies exist and return a string slice containing binaries that are not found in PATH.

func FCompress

func FCompress(f string, flist []string) error

func FCompressVol

func FCompressVol(f string, flist []string) []string

func FFToAnimatedWebpLQ

func FFToAnimatedWebpLQ(f string) error

Replaces .webm ext to .webp

func FFToAnimatedWebpWA

func FFToAnimatedWebpWA(f string) error

// animated webp has a pretty bad compression ratio comparing to VP9, // shrink down quality as much as possible.

func FFToGif

func FFToGif(f string) (string, error)

func FFToWebmSafe

func FFToWebmSafe(f string, isCustomEmoji bool) (string, error)

This function will be called if Telegram's API rejected our webm. It is normally due to overlength or bad FPS rate.

func FFToWebmTGVideo

func FFToWebmTGVideo(f string, isCustomEmoji bool) (string, error)

func FFtoPNG

func FFtoPNG(f string, pathOut string) error

appends png

func IMStackToWebp

func IMStackToWebp(base string, overlay string) (string, error)

func IMToAnimatedWebpLQ

func IMToAnimatedWebpLQ(f string) error

Replaces .webm ext to .webp

func IMToApng

func IMToApng(f string) (string, error)

func IMToPNGThumb

func IMToPNGThumb(f string) error

Replaces .webm or .webp to .png

func IMToPng

func IMToPng(f string) (string, error)

func IMToWebpTGStatic

func IMToWebpTGStatic(f string, isCustomEmoji bool) (string, error)

Convert any image to static WEBP image, for Telegram use. `format` takes either FORMAT_TG_REGULAR_STATIC or FORMAT_TG_EMOJI_STATIC

func IMToWebpWA

func IMToWebpWA(f string) error

Convert image to static Webp for Whatsapp, size limit is 100KiB.

func InitConvert

func InitConvert()

Should call before using functions in this package. Otherwise, defaults to Linux environment. This function also call CheckDeps to check if executables.

func LsFiles

func LsFiles(dir string, mustHave []string, mustNotHave []string) []string

func LsFilesR

func LsFilesR(dir string, mustHave []string, mustNotHave []string) []string
func ParseImportLink(link string, ld *LineData) (string, error)

This function serves as an entrypoint for this package. Parse a LINE or Kakao link and fetch metadata. The metadata (which means the LineData struct) can be used to call prepareImportStickers. Returns a string and an error. String act as a warning message, empty string means no warning yield.

Attention: After this function returns, ld.Amount, ld.Files will NOT be available!

func PrepareImportStickers

func PrepareImportStickers(ctx context.Context, ld *LineData, workDir string, convertToTGFormat bool, convertToTGEmoji bool) error

Prepare stickers files. Should be called after calling ParseImportLink(). A context is provided, which can be used to interrupt the process. Even if this function returns, file preparation might still in progress. LineData.Amount, LineData.Files will be produced after return. wg.Wait() is required for individual LineData.Files

convertToTGFormat: Convert original stickers to Telegram sticker format. convertToTGEmoji: If present sticker set is Emoji(LINE), convert to 100x100 Telegram CustomEmoji.

func RlottieToGIF

func RlottieToGIF(f string) (string, error)

Replaces tgs to gif.

func SecHex

func SecHex(n int) string

func SetImageTime

func SetImageTime(f string, t time.Time) error

Types

type Author

type Author struct {
	En     string `json:"en"`
	Es     string `json:"es"`
	In     string `json:"in"`
	Ja     string `json:"ja"`
	Ko     string `json:"ko"`
	Th     string `json:"th"`
	ZhHans string `json:"zh-Hans"`
	ZhHant string `json:"zh-Hant"`
}

type KakaoJson

type KakaoJson struct {
	Result KakaoJsonResult
}

type KakaoJsonResult

type KakaoJsonResult struct {
	//Korean title
	Title string
	//kakao ID
	TitleUrl string
	//PNG urls
	ThumbnailUrls []string
	//??
	TitleImageUrl string
	//Cover image
	TitleDetailUrl string
}

type LineData

type LineData struct {
	//Waitgroup for when linedata become available.
	Wg sync.WaitGroup
	//Store type, defined in const.
	Store string
	//Store link
	Link string
	//Store links for different langs
	I18nLinks []string
	//Sticker download link, typically ZIP.
	DLink string
	//Sticker download links.
	DLinks []string
	//Sticker file paths.
	Files []*LineFile
	//Sticker category, defined in const.
	Category string
	//Sticker pack ID.
	Id string
	//Sticker title appeared in store.
	Title string
	//I18n titles for LINE sticker packs, TitleWg must be waited before using this field.
	I18nTitles []string
	//WaitGroup for I18nTitles
	TitleWg sync.WaitGroup
	//Is Animated line sticker.
	IsAnimated bool
	//Is line emoji(emoticon).
	IsEmoji bool
	Amount  int
}

This is called linedata due to historical reason, instead, it handles "import" data, which includes kakao and line by far.

type LineFile

type LineFile struct {
	//Waitgroup for conversion.
	Wg sync.WaitGroup
	//Convert to 100x100 emoji.
	ConvertToEmoji bool
	// path of original file
	OriginalFile string
	// path of converted filea
	ConvertedFile string
	// conversion error
	CError error
}

type LineJson

type LineJson struct {
	Name string
	Sku  string
	Url  string
}

type LineProductInfo

type LineProductInfo struct {
	PackageID           int64     `json:"packageId"`
	OnSale              bool      `json:"onSale"`
	ValidDays           int64     `json:"validDays"`
	Title               Title     `json:"title"`
	Author              Author    `json:"author"`
	Price               []Price   `json:"price"`
	Stickers            []Sticker `json:"stickers"`
	HasAnimation        bool      `json:"hasAnimation"`
	HasSound            bool      `json:"hasSound"`
	StickerResourceType string    `json:"stickerResourceType"`
}

type Price

type Price struct {
	Country  string  `json:"country"`
	Currency string  `json:"currency"`
	Symbol   string  `json:"symbol"`
	Price    float64 `json:"price"`
}

type Sticker

type Sticker struct {
	ID     int64 `json:"id"`
	Width  int64 `json:"width"`
	Height int64 `json:"height"`
}

type Title

type Title struct {
	En     string `json:"en"`
	Es     string `json:"es"`
	In     string `json:"in"`
	Ja     string `json:"ja"`
	Ko     string `json:"ko"`
	Th     string `json:"th"`
	ZhHans string `json:"zh-Hans"`
	ZhHant string `json:"zh-Hant"`
}

Jump to

Keyboard shortcuts

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