diff options
Diffstat (limited to 'search_query.go')
-rw-r--r-- | search_query.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/search_query.go b/search_query.go new file mode 100644 index 0000000..ac5e178 --- /dev/null +++ b/search_query.go @@ -0,0 +1,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 +} |