mirror of
https://github.com/SagerNet/sing-geosite.git
synced 2025-02-06 12:52:42 +08:00
Fix bad data
This commit is contained in:
parent
e6c443b935
commit
563f703dde
73
main.go
73
main.go
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/geosite"
|
"github.com/sagernet/sing-box/common/geosite"
|
||||||
@ -169,6 +170,75 @@ func parse(vGeositeData []byte) (map[string][]geosite.Item, error) {
|
|||||||
return domainMap, nil
|
return domainMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type filteredCodePair struct {
|
||||||
|
code string
|
||||||
|
badCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterTags(data map[string][]geosite.Item) {
|
||||||
|
var codeList []string
|
||||||
|
for code := range data {
|
||||||
|
codeList = append(codeList, code)
|
||||||
|
}
|
||||||
|
var badCodeList []filteredCodePair
|
||||||
|
var filteredCodeMap []string
|
||||||
|
var mergedCodeMap []string
|
||||||
|
for _, code := range codeList {
|
||||||
|
codeParts := strings.Split(code, "@")
|
||||||
|
if len(codeParts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
leftParts := strings.Split(codeParts[0], "-")
|
||||||
|
var lastName string
|
||||||
|
if len(leftParts) > 1 {
|
||||||
|
lastName = leftParts[len(leftParts)-1]
|
||||||
|
}
|
||||||
|
if lastName == "" {
|
||||||
|
lastName = codeParts[0]
|
||||||
|
}
|
||||||
|
if lastName == codeParts[1] {
|
||||||
|
delete(data, code)
|
||||||
|
filteredCodeMap = append(filteredCodeMap, code)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if "!"+lastName == codeParts[1] {
|
||||||
|
badCodeList = append(badCodeList, filteredCodePair{
|
||||||
|
code: codeParts[0],
|
||||||
|
badCode: code,
|
||||||
|
})
|
||||||
|
} else if lastName == "!"+codeParts[1] {
|
||||||
|
badCodeList = append(badCodeList, filteredCodePair{
|
||||||
|
code: codeParts[0],
|
||||||
|
badCode: code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, it := range badCodeList {
|
||||||
|
badList := data[it.badCode]
|
||||||
|
if badList == nil {
|
||||||
|
panic("bad list not found: " + it.badCode)
|
||||||
|
}
|
||||||
|
delete(data, it.badCode)
|
||||||
|
newMap := make(map[geosite.Item]bool)
|
||||||
|
for _, item := range data[it.code] {
|
||||||
|
newMap[item] = true
|
||||||
|
}
|
||||||
|
for _, item := range badList {
|
||||||
|
delete(newMap, item)
|
||||||
|
}
|
||||||
|
newList := make([]geosite.Item, 0, len(newMap))
|
||||||
|
for item := range newMap {
|
||||||
|
newList = append(newList, item)
|
||||||
|
}
|
||||||
|
data[it.code] = newList
|
||||||
|
mergedCodeMap = append(mergedCodeMap, it.badCode)
|
||||||
|
}
|
||||||
|
sort.Strings(filteredCodeMap)
|
||||||
|
sort.Strings(mergedCodeMap)
|
||||||
|
os.Stderr.WriteString("filtered " + strings.Join(filteredCodeMap, ",") + "\n")
|
||||||
|
os.Stderr.WriteString("merged " + strings.Join(mergedCodeMap, ",") + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func generate(release *github.RepositoryRelease, output string, cnOutput string, ruleSetOutput string) error {
|
func generate(release *github.RepositoryRelease, output string, cnOutput string, ruleSetOutput string) error {
|
||||||
vData, err := download(release)
|
vData, err := download(release)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -178,6 +248,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
filterTags(domainMap)
|
||||||
outputPath, _ := filepath.Abs(output)
|
outputPath, _ := filepath.Abs(output)
|
||||||
os.Stderr.WriteString("write " + outputPath + "\n")
|
os.Stderr.WriteString("write " + outputPath + "\n")
|
||||||
outputFile, err := os.Create(output)
|
outputFile, err := os.Create(output)
|
||||||
@ -227,7 +298,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".srs"))
|
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".srs"))
|
||||||
os.Stderr.WriteString("write " + srsPath + "\n")
|
//os.Stderr.WriteString("write " + srsPath + "\n")
|
||||||
outputRuleSet, err := os.Create(srsPath)
|
outputRuleSet, err := os.Create(srsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user