package repository import ( "regexp" "strings" "git.kplus.net.ua/yevhen/resource-scraper/types/model" "git.kplus.net.ua/yevhen/resource-scraper/types/resource" ) // srrdbGroupSegment cuts the group off the tail of a release line, which always // sits last. Compiled once at package level instead of per record. // // The pattern takes the trailing word, so a group whose own name contains a // hyphen or a dot would lose everything before that character. Nothing in the // line tells such a name apart from the tokens in front of it, so widening the // character class without a list of real group names would only start eating the // title. var srrdbGroupSegment = regexp.MustCompile(`-(\w+)$`) // srrdbCategory reads the value of the category: segment of a search path, which // is guaranteed to be present by the time this is called. func srrdbCategory(uri string) string { _, rest, ok := strings.Cut(uri, "category:") if !ok { return "" } value, _, _ := strings.Cut(rest, "/") return value } // srrdbRow maps one API record onto a table row. status, nfo, content and // synced_at stay nil and are stored as NULL. func srrdbRow(result resource.SrrdbResult, run, category string) model.SrrdbRelease { return model.SrrdbRelease{ Release: result.Release, Date: result.Date, HasNfo: result.HasNFO, HasSrs: result.HasSRS, IsForeign: result.IsForeign, ImdbID: result.ImdbID, Size: result.Size, GroupName: srrdbGroupName(result.Release), Category: category, CreatedAt: run, } } // srrdbGroupName returns the trailing group of a release line, or an empty // string when the tail is not a plain word. The nil check is deliberate: // FindStringSubmatch returns nil on no match, and indexing it directly panics. func srrdbGroupName(release string) string { match := srrdbGroupSegment.FindStringSubmatch(release) if match == nil { return "" } return match[1] }