aboutsummaryrefslogtreecommitdiff
path: root/search_query.go
blob: b332437c9a2aa66e46decdf3a7dc417d4b6076ce (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* 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 (
  "fmt"
  "errors"
)

type Category struct {
  Category    int
  SubCategory int
}

type SearchQuery struct {
  BaseURL      string
  SearchString string
  Filter       int
  Category     Category
  Page         int
}

const (
  FilterNone        int = 0
  FilterNoRemakes   int = 1
  FilterTrustedOnly int = 2
)

var (
  CategoryAll                  = Category{ Category: 0, SubCategory: 0 }

  CategoryAnime                = Category{ Category: 1, SubCategory: 0 }
  CategoryAnimeAMV             = Category{ Category: 1, SubCategory: 1 }
  CategoryAnimeEnglish         = Category{ Category: 1, SubCategory: 2 }
  CategoryAnimeNonEnglish      = Category{ Category: 1, SubCategory: 3 }
  CategoryAnimeRaw             = Category{ Category: 1, SubCategory: 4 }

  CategoryAudio                = Category{ Category: 2, SubCategory: 0 }
  CategoryAudioLossless        = Category{ Category: 2, SubCategory: 1 }
  CategoryAudioLossy           = Category{ Category: 2, SubCategory: 2 }

  CategoryLiterature           = Category{ Category: 3, SubCategory: 0 }
  CategoryLiteratureEnglish    = Category{ Category: 3, SubCategory: 1 }
  CategoryLiteratureNonEnglish = Category{ Category: 3, SubCategory: 2 }
  CategoryLiteratureRaw        = Category{ Category: 3, SubCategory: 3 }

  CategoryLiveAction           = Category{ Category: 4, SubCategory: 0 }
  CategoryLiveActionEnglish    = Category{ Category: 4, SubCategory: 1 }
  CategoryLiveActionIdolPV     = Category{ Category: 4, SubCategory: 2 }
  CategoryLiveActionNonEnglish = Category{ Category: 4, SubCategory: 3 }
  CategoryLiveActionRaw        = Category{ Category: 4, SubCategory: 4 }

  CategoryPictures             = Category{ Category: 5, SubCategory: 0 }
  CategoryPicturesGraphics     = Category{ Category: 5, SubCategory: 1 }
  CategoryPicturesPhotos       = Category{ Category: 5, SubCategory: 2 }

  CategorySoftware             = Category{ Category: 6, SubCategory: 0 }
  CategorySoftwareApps         = Category{ Category: 6, SubCategory: 1 }
  CategorySoftwareGames        = Category{ Category: 6, SubCategory: 2 }
)

func (q SearchQuery) build() (string, error) {
  if q.BaseURL == "" {
    q.BaseURL = "nyaa.si"
  }

  if len(q.BaseURL) < 8 || q.BaseURL[:8] != "https://" {
    q.BaseURL = "https://" + q.BaseURL
  }

  if q.BaseURL[len(q.BaseURL) - 1:] != "/" {
    q.BaseURL = q.BaseURL + "/"
  }

  if q.Page < 1 {
    q.Page = 1
  }

  if q.Page > 100 {
    return "", errors.New("max page number is 100")
  }

  return fmt.Sprintf("%s?f=%d&c=%d_%d&q=%s&p=%d", q.BaseURL, q.Filter, q.Category.Category, q.Category.SubCategory, q.SearchString, q.Page), nil
}