aboutsummaryrefslogtreecommitdiff
path: root/search.go
diff options
context:
space:
mode:
Diffstat (limited to 'search.go')
-rw-r--r--search.go76
1 files changed, 76 insertions, 0 deletions
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 <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+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
+}