nyaa

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2022 License: MIT Imports: 9 Imported by: 0

README

nyaa-go

Go Report Card Actions Status codecov

Go Reference GitHub go.mod Go version License

An unofficial nyaa.si client library for Go

Installation

go get github.com/mkfsn/nyaa-go

Example

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/mkfsn/nyaa-go"
)

func main() {
	c := nyaa.NewClient()
	torrents, pageInfo, err := c.Search(context.Background(), nyaa.SearchOptions{
		Provider:  nyaa.ProviderNyaa,
		FilterBy:  nyaa.FilterByNoFilter,
		Category:  nyaa.CategoryAll,
		Query:     "Nana Mizuki - NANA CLIP 8 BDMV",
		SortBy:    nyaa.SortByDate,
		SortOrder: nyaa.SortOrderDesc,
		// Page:      nyaa.Page(0),
	})
	if err != nil {
		log.Fatalln(err)
	}
	
	fmt.Printf("pageInfo: %#v\n", pageInfo)

	for _, torrent := range torrents {
		fmt.Printf("%+v\n", torrent)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownProvider  = errors.New("unknown Provider")
	ErrUnknownFilterBy  = errors.New("unknown FilterBy")
	ErrUnknownSortBy    = errors.New("unknown SortBy")
	ErrUnknownSortOrder = errors.New("unknown SortOrder")
)

Functions

This section is empty.

Types

type Category

type Category int

Category represents the category for searching torrents in Nyaa. The available category varies by different providers.

const (
	CategoryAll Category = 0x000

	CategoryNyaaAnime                          Category = 0x110
	CategoryNyaaAnimeMusicVideo                Category = 0x111
	CategoryNyaaAnimeEnglishTranslated         Category = 0x112
	CategoryNyaaAnimeNonEnglishTranslated      Category = 0x113
	CategoryNyaaAnimeRaw                       Category = 0x114
	CategoryNyaaAudio                          Category = 0x120
	CategoryNyaaAudioLossless                  Category = 0x121
	CategoryNyaaAudioLossy                     Category = 0x122
	CategoryNyaaLiterature                     Category = 0x130
	CategoryNyaaLiteratureEnglishTranslated    Category = 0x131
	CategoryNyaaLiteratureNonEnglishTranslated Category = 0x132
	CategoryNyaaLiteratureRaw                  Category = 0x133
	CategoryNyaaLiveAction                     Category = 0x140
	CategoryNyaaLiveActionEnglishTranslated    Category = 0x141
	CategoryNyaaPictures                       Category = 0x150
	CategoryNyaaPicturesGraphics               Category = 0x151
	CategoryNyaaPicturesPhotos                 Category = 0x152
	CategoryNyaaSoftware                       Category = 0x160
	CategoryNyaaSoftwareApplications           Category = 0x161
	CategoryNyaaSoftwareGames                  Category = 0x162

	CategorySukebeiArt                           Category = 0x210
	CategorySukebeiArtAnime                      Category = 0x211
	CategorySukebeiArtDoujinshi                  Category = 0x212
	CategorySukebeiArtGames                      Category = 0x213
	CategorySukebeiArtManga                      Category = 0x214
	CategorySukebeiArtPictures                   Category = 0x215
	CategorySukebeiRealLife                      Category = 0x220
	CategorySukebeiRealLifePhotobooksAndPictures Category = 0x221
	CategorySukebeiRealLifeVideos                Category = 0x222
)

The categories for the currently supported providers.

func (Category) String

func (c Category) String() string

String implements fmt.Stringer interface.

func (Category) Value

func (c Category) Value(p Provider) string

Value returns the value of the query parameter in the HTTP request based on the given Provider.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents a Nyaa client.

func NewClient

func NewClient() *Client

NewClient returns a Nyaa client.

func (*Client) Search

func (c *Client) Search(ctx context.Context, opts SearchOptions) ([]*Torrent, *PageInfo, error)

Search sends a request based on the given SearchOptions and returns the matched torrents information.

Example
c := nyaa.NewClient()
torrents, _, err := c.Search(context.Background(), nyaa.SearchOptions{
	Provider:  nyaa.ProviderNyaa,
	FilterBy:  nyaa.FilterByNoFilter,
	Category:  nyaa.CategoryAll,
	Query:     "Nana Mizuki - NANA CLIP 8 BDMV",
	SortBy:    nyaa.SortByDate,
	SortOrder: nyaa.SortOrderDesc,
})
if err != nil {
	log.Fatalln(err)
}

for _, torrent := range torrents {
	fmt.Printf("%s\n", torrent.Name)
	fmt.Printf(" - %s\n", torrent.Category)
	fmt.Printf(" - %s\n", torrent.Size)
	fmt.Printf(" - %s\n", torrent.Link)
	fmt.Printf(" - %s\n", torrent.Date.UTC())
	// fmt.Printf(" - %d\n", torrent.Seeders)
	// fmt.Printf(" - %d\n", torrent.Leechers)
	// fmt.Printf(" - %d\n", torrent.CompletedDownloads)
}
Output:

[水樹奈々] Nana Mizuki - NANA CLIP 8 BDMV
 - Live Action - Non-English-translated
 - 35.9 GiB
 - https://nyaa.si/download/1421189.torrent
 - 2021-08-15 00:06:05 +0000 UTC

type FilterBy

type FilterBy int

FilterBy represents a way to filter the torrents when searching.

const (
	FilterByNoFilter FilterBy = iota
	FilterByNoRemakes
	FilterByTrustedOnly
)

The current ways for filtering torrents.

func (FilterBy) String

func (f FilterBy) String() string

String implements fmt.Stringer interface.

func (FilterBy) Value

func (f FilterBy) Value() string

Value returns the value of the query parameter in the HTTP request.

type Page added in v0.3.0

type Page int

Page represents the page number in both request and response.

func (Page) Value added in v0.3.0

func (p Page) Value() string

Value returns the value of the query parameter in the HTTP request.

type PageInfo added in v0.3.0

type PageInfo struct {
	IsSinglePage bool
	CurrentPage  Page
	LastPage     Page
}

PageInfo represents the pagination information parsed from the web page.

type Provider

type Provider int

Provider represents the supported sites of Nyaa.

const (
	// ProviderNyaa is for http://nyaa.si.
	ProviderNyaa Provider = iota
	// ProviderSukebei is for http://sukebei.nyaa.si (NSFW).
	ProviderSukebei
)

Currently supported providers.

func (Provider) BaseURL

func (p Provider) BaseURL() *url.URL

BaseURL returns the base URL of the provider.

func (Provider) Host

func (p Provider) Host() string

Host returns the host of the provider.

Example
fmt.Printf("%s\n", nyaa.ProviderNyaa.Host())
fmt.Printf("%s\n", nyaa.ProviderSukebei.Host())
Output:

nyaa.si
sukebei.nyaa.si

func (Provider) String

func (p Provider) String() string

String implements fmt.Stringer interface.

type SearchOptions

type SearchOptions struct {
	Provider  Provider
	FilterBy  FilterBy
	Category  Category
	Query     string
	SortBy    SortBy
	SortOrder SortOrder
	Page      Page
}

SearchOptions represents the options for searching torrents.

type SortBy

type SortBy int

SortBy represents the way to sort the torrent when searching.

const (
	SortByDate SortBy = iota
	SortByComments
	SortByDownloads
	SortBySeeders
	SortByLeechers
	SortBySize
)

The ways to sort the torrents.

func (SortBy) String

func (s SortBy) String() string

String implements fmt.Stringer interface.

func (SortBy) Value

func (s SortBy) Value() string

Value returns the value of the query parameter in the HTTP request.

type SortOrder

type SortOrder int

SortOrder represents the order of the torrent when searching.

const (
	SortOrderDesc SortOrder = iota
	SortOrderAsc
)

The order of the sorting.

func (SortOrder) String

func (s SortOrder) String() string

String implements fmt.Stringer interface.

func (SortOrder) Value

func (s SortOrder) Value() string

Value returns the value of the query parameter in the HTTP request.

type Torrent

type Torrent struct {
	Category           string    `json:"category"`
	Name               string    `json:"name"`
	Link               string    `json:"link"`
	Magnet             string    `json:"magnet"`
	Size               string    `json:"size"`
	Date               time.Time `json:"date"`
	Seeders            int64     `json:"seeders"`
	Leechers           int64     `json:"leechers"`
	CompletedDownloads int64     `json:"completeDownloads"`
	// contains filtered or unexported fields
}

Torrent represents a torrent with information retrieved from Nyaa.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL