-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl (search anime in animefire): adding a function to search anime i…
…n animefire
- Loading branch information
1 parent
db1b9b8
commit 80eae83
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |