This repository has been archived on 2024-08-30. You can view files and clone it, but cannot push or open issues or pull requests.
RaceBot/models/release.go
2023-04-15 16:23:34 +07:00

84 lines
2.9 KiB
Go

package models
import (
"git.rouggy.com/rouggy/RaceBot/database"
"github.com/jinzhu/gorm"
"github.com/moistari/rls"
)
type Release struct {
gorm.Model
Type string `gorm:"column:type;varchar(10);not null" form:"type" json:"type" binding:"required,max=10"`
TorrentName string `gorm:"column:torrent_name;varchar(100);not null" form:"torrent_name" json:"torrent_name" binding:"required,max=100"`
Title string `gorm:"column:title;varchar(100);not null" form:"title" json:"title" binding:"required,max=100"`
Season int `gorm:"column:season;integer" form:"season" json:"season" binding:"required,max=100"`
Episode int `gorm:"column:episode;integer" form:"episode" json:"episode" binding:"required,max=100"`
Subtitle string `gorm:"column:subtitle;varchar(10)" form:"subtitle" json:"subtitle" binding:"max=10"`
Source string `gorm:"column:source;varchar(15)" form:"source" json:"source" binding:"max=15"`
Resolution string `gorm:"column:resolution;varchar(10)" form:"resolution" json:"resolution" binding:"max=10"`
Codec string `gorm:"many2many:release_codecs;column:codec;varchar(10)" form:"codec" json:"codec" binding:"max=10"`
HDR string `gorm:"many2many:releases_hdr;column:hdr;varchar(10)" form:"hdr" json:"hdr" binding:"max=10"`
Audio string `gorm:"many2many:releases_audio;column:audio;varchar(10)" form:"audio" json:"audio" binding:"max=10"`
Channels string `gorm:"column:channels;varchar(10)" form:"channels" json:"channels" binding:"max=10"`
Group string `gorm:"column:group;varchar(20)" form:"group" json:"group" binding:"max=20"`
Other string `gorm:"many2many:releases_other;column:other;varchar(20)" form:"other" json:"other" binding:"max=20"`
Language string `gorm:"many2many:releases_languages;column:language;varchar(20)" form:"language" json:"language" binding:"max=20"`
ReleaseType string `gorm:"column:release_type;varchar(20)" form:"release_type" json:"release_type" binding:"max=20"`
Year int `gorm:"column:year;integer" form:"year" json:"year" binding:"max=4"`
}
func NewRelease() *Release {
r := &Release{}
return r
}
func (r *Release) ParseString(title string) {
rel := rls.ParseString(title)
r.TorrentName = title
r.Source = rel.Source
r.Resolution = rel.Resolution
if rel.Audio != nil {
r.Audio = rel.Audio[0]
}
r.Audio = rel.Channels
r.Codec = rel.Codec[0]
if rel.HDR != nil {
r.HDR = rel.HDR[0]
}
if rel.Other != nil {
r.Other = rel.Other[0]
}
if rel.Language != nil {
r.Language = rel.Language[0]
}
if r.Title == "" {
r.Title = rel.Title
}
if r.Season == 0 {
r.Season = rel.Series
}
if r.Episode == 0 {
r.Episode = rel.Episode
}
if r.Year == 0 {
r.Year = rel.Year
}
if r.Group == "" {
r.Group = rel.Group
}
}
func (r *Release) SaveRelease() (*Release, error) {
db := database.GetDB()
err := db.Create(&r).Error
if err != nil {
return &Release{}, err
}
return r, nil
}