Skip to content

Commit

Permalink
impl (search anime in animefire): adding a function to search anime i…
Browse files Browse the repository at this point in the history
…n animefire
  • Loading branch information
KitsuneSemCalda committed Aug 25, 2024
1 parent db1b9b8 commit 80eae83
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions internal/Network/search_anime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package network

import (
"net/http"
"os"
"sort"
"strings"

"github.com/PuerkitoBio/goquery"

"animatic-v2/internal/Message"
"animatic-v2/internal/Structures"
)

func GetAllAnimes(urlBuilded string) []Structures.Anime {
var allAnimes []Structures.Anime
currentPageUrl := urlBuilded

for {
response, err := http.Get(currentPageUrl)
if err != nil {
message.ErrorMessage(err.Error())
os.Exit(1)
}
defer response.Body.Close()

doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
message.ErrorMessage(err.Error())
os.Exit(1)
}

animes := make([]Structures.Anime, 0)

doc.Find(".row.ml-1.mr-1 a").Each(func(i int, s *goquery.Selection) {
anime := Structures.Anime{
Name: strings.TrimSpace(s.Text()),
Url: s.AttrOr("href", ""),
}

animes = append(animes, anime)
})

allAnimes = append(allAnimes, animes...)

nextPage, exists := doc.Find(".pagination .next a").Attr("href")
if !exists || nextPage == "" {
break
}

currentPageUrl = nextPage

if !strings.HasPrefix(currentPageUrl, "http") {
currentPageUrl = urlBuilded + nextPage
}

value, err := checkUrlStatus(currentPageUrl)
if !value || err != nil {
message.ErrorMessage("Can't search in next page")
os.Exit(1)
}
}

sort.Slice(allAnimes, func(i, j int) bool {
if strings.Contains(allAnimes[i].Name, "Dublado") && !strings.Contains(allAnimes[j].Name, "Dublado") {
return true
}
if !strings.Contains(allAnimes[i].Name, "Dublado") && strings.Contains(allAnimes[j].Name, "Dublado") {
return false
}
return strings.Compare(allAnimes[i].Name, allAnimes[j].Name) < 0
})

return allAnimes
}

0 comments on commit 80eae83

Please sign in to comment.