/* meow - Get search results from Nyaa through web scraping with GoLang * Copyright (C) 2024 Vidhu Kant Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package meow import ( "github.com/gocolly/colly/v2" "strconv" ) func Search(q SearchQuery) ([]Entry, error) { var res []Entry url, err := q.build() if err != nil { return res, err } c := colly.NewCollector() c.OnHTML("table tbody tr", func(e *colly.HTMLElement) { var entry Entry e.ForEach("td", func(_ int, e *colly.HTMLElement) { switch(e.Index) { case 0: entry.Category = e.ChildAttr("a", "title") case 1: e.ForEach("a", func(_ int, e *colly.HTMLElement) { if e.Attr("class") != "comments" { entry.URL = e.Attr("href") entry.Title = e.Text } }) case 2: e.ForEach("a", func(_ int, e *colly.HTMLElement) { switch(e.Index) { case 0: entry.TorrentURL = e.Attr("href") case 1: entry.MagnetURL = e.Attr("href") } }) case 3: entry.FileSize = e.Text case 4: entry.TimeStamp = e.Text case 5: entry.Seeders, _ = strconv.Atoi(e.Text) case 6: entry.Leechers, _ = strconv.Atoi(e.Text) case 7: entry.Downloads, _ = strconv.Atoi(e.Text) } }) entry.Flag = e.Attr("class") res = append(res, entry) }) c.Visit(url) return res, nil }