diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | main.go | 76 |
2 files changed, 66 insertions, 13 deletions
@@ -23,3 +23,6 @@ compress_into_oblivion: build run: $(GO_RUN) . + +series: + $(GO_RUN) . series @@ -7,6 +7,7 @@ import ( "fmt" "io" "net/http" + "os" "regexp" "strconv" "strings" @@ -18,22 +19,34 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -type ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error - const ( FeedUrl = "https://api.jovemnerd.com.br/feed-nerdcast/" ) +type ( + ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error +) + +var ( + SerieRegex = regexp.MustCompile(`(?P<serie>.+) (?P<number>[0-9abc]+) \- (?P<title>.+)`) +) + var ( - RegexCollection = map[string]string{ - "nerdcast": "NerdCast [0-9]+[a-c]* -", - "empreendedor": "Empreendedor [0-9]+ -", - "mamicas": "Caneca de Mamicas [0-9]+ -", - "english": "Speak English [0-9]+ -", - "nerdcash": "NerdCash [0-9]+ -", - "bunker": "Lá do Bunker [0-9]+ -", - "tech": "NerdTech [0-9]+ -", - "genera": "Generacast [0-9]+ -", + regexCollection = map[string]string{ + "nerdcast": "NerdCast [0-9]+[a-c]*", + "empreendedor": "Empreendedor [0-9]+", + "mamicas": "Caneca de Mamicas [0-9]+", + "english": "Speak English [0-9]+", + "nerdcash": "NerdCash [0-9]+", + "bunker": "Lá do Bunker( LDB especial Oscar|) [0-9]+", + "tech": "NerdTech [0-9]+", + "genera": "Generacast [0-9]+", + "rpg": "NerdCast RPG [0-9]+[a-c]*", + "catar": "Vai te Catar [0-9]+", + "cloud": "Nerd na Cloud [0-9]+", + "contar": "Vou (T|t)e Contar [0-9]+", + "parceiro": "Papo de Parceiro [0-9]+", + "cash": "NerdCash [0-9]+", } feedRequest = promauto.NewHistogramVec(prometheus.HistogramOpts{ @@ -60,7 +73,7 @@ func getSeries(r *http.Request) []string { var series []string for _, q := range strings.Split(query, ",") { - if _, ok := RegexCollection[q]; ok { + if _, ok := regexCollection[q]; ok { series = append(series, q) } } @@ -74,7 +87,7 @@ func getSeries(r *http.Request) []string { func match(title string, series []string) bool { for _, s := range series { - if ok, err := regexp.MatchString(RegexCollection[s], title); err == nil && ok { + if ok, err := regexp.MatchString(regexCollection[s], title); err == nil && ok { return true } } @@ -224,7 +237,44 @@ func podcast(w http.ResponseWriter, r *http.Request) error { return nil } +func genSeries() error { + xml, err := fetchXML(context.Background()) + if err != nil { + return err + } + + doc := etree.NewDocument() + err = doc.ReadFromBytes(xml) + if err != nil { + return err + } + + unique := make(map[string]any) + els := doc.FindElements("//channel/item") + for _, e := range els { + txt := e.FindElement("title").Text() + res := SerieRegex.FindStringSubmatch(txt) + if len(res) > 1 { + unique[res[1]] = nil + } + } + + for k := range unique { + fmt.Println(k) + } + + return nil +} + func main() { + if len(os.Args) > 1 && os.Args[1] == "series" { + err := genSeries() + if err != nil { + panic(err.Error()) + } + return + } + var ( addr = flag.String("addr", ":8080", "Server address") ) |