|
| 1 | +package subject |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/NullpointerW/anicat/log" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +func BuildFilterPerlReg(vbs []string) string { |
| 11 | + var reg string |
| 12 | + const tmp = `(?=.*?%s)` |
| 13 | + if len(vbs) != 0 { |
| 14 | + reg += "(?i)" |
| 15 | + for _, ct := range vbs { |
| 16 | + vb := strings.ReplaceAll(ct, ",", "|") |
| 17 | + vb = "(" + vb + ")" |
| 18 | + reg += fmt.Sprintf(tmp, vb) |
| 19 | + } |
| 20 | + return reg |
| 21 | + } else { |
| 22 | + return "" |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func BuildFilterRegs(vbs []string) []string { |
| 27 | + if len(vbs) != 0 { |
| 28 | + regs := make([]string, 0, len(vbs)) |
| 29 | + for _, ct := range vbs { |
| 30 | + vb := strings.ReplaceAll(ct, ",", "|") |
| 31 | + vb = "(?i)" + vb |
| 32 | + regs = append(regs, vb) |
| 33 | + } |
| 34 | + return regs |
| 35 | + } else { |
| 36 | + return nil |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func FilterWithRegs(s string, contains, exclusions []string) bool { |
| 41 | + var ( |
| 42 | + containOk, exclusionOk bool |
| 43 | + ) |
| 44 | + if len(contains) == 0 { |
| 45 | + containOk = true |
| 46 | + } |
| 47 | + if len(exclusions) == 0 { |
| 48 | + exclusionOk = true |
| 49 | + } |
| 50 | + if !containOk { |
| 51 | + //containOks := make([]bool, 0, len(contains)) |
| 52 | + for _, reg := range contains { |
| 53 | + var ok bool |
| 54 | + csre, err := regexp.Compile(reg) |
| 55 | + if err != nil { |
| 56 | + log.Error(log.Struct{"err", err}, "globalFilter: contains regexp compile failed") |
| 57 | + ok = true |
| 58 | + } else { |
| 59 | + ok = csre.MatchString(s) |
| 60 | + log.Debug(log.Struct{"containRegexp", csre.String(), "matchingString", s, "matched", ok}) |
| 61 | + //improve: break immediately |
| 62 | + if !ok { |
| 63 | + return false |
| 64 | + } |
| 65 | + } |
| 66 | + //containOks = append(containOks, ok) |
| 67 | + } |
| 68 | + containOk = true |
| 69 | + //for _, ok := range containOks { |
| 70 | + // if !ok { |
| 71 | + // containOk = false |
| 72 | + // break |
| 73 | + // } |
| 74 | + //} |
| 75 | + } |
| 76 | + |
| 77 | + if !exclusionOk { |
| 78 | + //exclusionOks := make([]bool, 0, len(exclusions)) |
| 79 | + for _, reg := range exclusions { |
| 80 | + var ok bool |
| 81 | + clsre, err := regexp.Compile(reg) |
| 82 | + if err != nil { |
| 83 | + log.Error(log.Struct{"err", err}, "globalFilter: exclusions regexp compile failed") |
| 84 | + ok = true |
| 85 | + } else { |
| 86 | + ok = !clsre.MatchString(s) |
| 87 | + log.Debug(log.Struct{"exclusionRegexp", clsre.String(), "matchingString", s, "matched", ok}) |
| 88 | + if !ok { |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + //exclusionOks = append(exclusionOks, ok) |
| 93 | + } |
| 94 | + exclusionOk = true |
| 95 | + //for _, ok := range exclusionOks { |
| 96 | + // if !ok { |
| 97 | + // exclusionOk = false |
| 98 | + // break |
| 99 | + // } |
| 100 | + //} |
| 101 | + } |
| 102 | + return containOk && exclusionOk |
| 103 | +} |
| 104 | + |
| 105 | +func FilterWithCustomReg(s string, e Extra) bool { |
| 106 | + clsOk, exlOk := true, true |
| 107 | + if cls := e.RssOption.MustContain; cls != "" { |
| 108 | + clsReg, err := regexp.Compile(cls) |
| 109 | + if err != nil { |
| 110 | + log.Error(log.Struct{"err", err}, "customFilter: contains regexp compile failed") |
| 111 | + } else { |
| 112 | + clsOk = clsReg.MatchString(s) |
| 113 | + } |
| 114 | + } |
| 115 | + if exl := e.RssOption.MustNotContain; exl != "" { |
| 116 | + exlReg, err := regexp.Compile(exl) |
| 117 | + if err != nil { |
| 118 | + log.Error(log.Struct{"err", err}, "customFilter: contains regexp compile failed") |
| 119 | + } else { |
| 120 | + exlOk = !exlReg.MatchString(s) |
| 121 | + } |
| 122 | + } |
| 123 | + return clsOk && exlOk |
| 124 | +} |
| 125 | + |
| 126 | +func FilterWithCustom(s string, e Extra) bool { |
| 127 | + cls, ext := strings.Fields(e.RssOption.MustContain), strings.Fields(e.RssOption.MustNotContain) |
| 128 | + return FilterWithRegs(s, cls, ext) |
| 129 | +} |
0 commit comments