aboutsummaryrefslogtreecommitdiff
path: root/search.go
blob: c018f0afdda8888fd0f500ed8c0c0ce7b25daafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 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:
        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
}