From cc5fb3c24c70930e2e2e956888a14dd865da2131 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 28 May 2024 20:58:04 +0530 Subject: first commit --- search.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 search.go (limited to 'search.go') diff --git a/search.go b/search.go new file mode 100644 index 0000000..40f495d --- /dev/null +++ b/search.go @@ -0,0 +1,76 @@ +/* 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: + entry.URL = e.ChildAttr("a", "href") + entry.Title = e.ChildText("a") + 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 +} -- cgit v1.2.3