nigonigo

package module
v0.0.0-...-225f368 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 22 Imported by: 0

README

[WIP] niconico API client for Golang

Build Status Go Reference license

Experimental implementation of niconico API client for Golang.

Features

  • Login/Logout
  • Search
  • MyList
  • Download video

Usage

T.B.D.

func main() {
	client := nigonigo.NewClient()

	contentID := "sm9"
	session, err := client.CreateVideoSession(contentID)
	if err != nil {
		log.Fatalf("Failed to create session: %v", err)
	}

	out, _ := os.Create(contentID + "." + session.FileExtension())
	defer out.Close()
	err = client.Download(context.Background(), session, out)
	if err != nil {
		log.Fatalf("Failed to download: %v", err)
	}
	log.Println("ok")
}
Command line tool:

install:

go install github.com/binzume/nigonigo/cmd/nigo@latest

usage:

nigo search -limit 10 -t "アニメ"
nigo search -sort "-viewCounter" -limit 3  -t "MMD 初音ミク" "あ"
nigo search "ねこ"
nigo auth -i "YOUR_MAILADDRESS"
 Password: ********
nigo download sm9
open sm9.mp4

License

MIT License

Documentation

Overview

niconico client for Golang.

https://www.nicovideo.jp/

Index

Constants

This section is empty.

Variables

View Source
var (
	AuthenticationRequired = errors.New("authentication required")
	InvalidResponse        = errors.New("invalid response received")
)

errors

View Source
var RequestLogger *log.Logger = nil
View Source
var UserAgent = "Mozilla/5.0 Nigonigo/1.0"

Functions

func NewHttpClient

func NewHttpClient() (*http.Client, error)

Types

type ChannelInfo

type ChannelInfo struct {
	ID          string
	Name        string
	Description string
}

type Client

type Client struct {
	HttpClient *http.Client
	Session    *NicoSession
}

func NewClient

func NewClient() *Client

func (*Client) AddMyListItem

func (c *Client) AddMyListItem(mylistId, contentID, description string) error

func (*Client) CreateDMCSession

func (c *Client) CreateDMCSession(reqsession jsonObject, sessionApiURL string) (*DMCSession, error)

func (*Client) CreateDMCSessionById

func (c *Client) CreateDMCSessionById(contentID string) (*DMCSession, error)

func (*Client) CreateDMCSessionByVideoData

func (c *Client) CreateDMCSessionByVideoData(data *VideoData) (*DMCSession, error)

func (*Client) CreateMyList

func (c *Client) CreateMyList(mylist *MyList) error

func (*Client) CreateSmileSessionByVideoData

func (c *Client) CreateSmileSessionByVideoData(data *VideoData) (*SmileSession, error)

func (*Client) CreateVideoSession

func (c *Client) CreateVideoSession(contentID string) (VideoSession, error)

func (*Client) DeleteMyList

func (c *Client) DeleteMyList(mylistId string) error

func (*Client) DeleteMyListItem

func (c *Client) DeleteMyListItem(mylistId string, itemID string) error

func (*Client) Download

func (c *Client) Download(ctx context.Context, session VideoSession, w io.Writer) error

func (*Client) DownloadFromDMC

func (c *Client) DownloadFromDMC(ctx context.Context, session *DMCSession, w io.Writer) error

func (*Client) DownloadFromSmile

func (c *Client) DownloadFromSmile(ctx context.Context, data *VideoData, w io.Writer) error

func (*Client) GetAvailableSessions

func (c *Client) GetAvailableSessions() ([]string, error)

func (*Client) GetChannelVideos

func (c *Client) GetChannelVideos(channelID string, page int) (*VideoListPage, error)

func (*Client) GetDefListItems

func (c *Client) GetDefListItems() ([]*MyListItem, error)

func (*Client) GetMyListItems

func (c *Client) GetMyListItems(mylistId string) ([]*MyListItem, error)

func (*Client) GetMyLists

func (c *Client) GetMyLists() ([]*MyList, error)

func (*Client) GetPublicMyList

func (c *Client) GetPublicMyList(mylistId string) (*MyList, []*VideoInfo, error)

func (*Client) GetVideoData

func (c *Client) GetVideoData(contentId string) (*VideoData, error)

func (*Client) LoadLoginSession

func (c *Client) LoadLoginSession(path string) error

func (*Client) Login

func (c *Client) Login(id, password string) error

func (*Client) LoginWithJsonFile

func (c *Client) LoginWithJsonFile(path string) error

func (*Client) Logout

func (c *Client) Logout() error

func (*Client) SaveLoginSession

func (c *Client) SaveLoginSession(path string) error

func (*Client) SearchByKeyword

func (c *Client) SearchByKeyword(s string, offset, limit int) (*SearchResult, error)

func (*Client) SearchByTag

func (c *Client) SearchByTag(tag string, offset, limit int) (*SearchResult, error)

func (*Client) SearchChannel

func (c *Client) SearchChannel(q string, mode SearchChannelMode, page int) ([]*ChannelInfo, error)

func (*Client) SearchVideo

func (c *Client) SearchVideo(q string, targets, fields []SearchField, sort string, offset, limit int, filter SearchFilter) (*SearchResult, error)

func (*Client) SearchVideo2

func (c *Client) SearchVideo2(req *SearchRequest) (*SearchResult, error)

func (*Client) SetSessionString

func (c *Client) SetSessionString(sessionStr string) error

type DMCSession

type DMCSession struct {
	ID         string `json:"id"`
	ContentURI string `json:"content_uri"`
	Protocol   struct {
		Name       string                 `json:"name"`
		Parameters map[string]interface{} `json:"parameters"`
	} `json:"protocol"`
	KeepMethod struct {
		Heartbeat *struct {
			LifetimeMs   int    `json:"lifetime"`
			OnetimeToken string `json:"onetime_token"`
		} `json:"heartbeat"`
	} `json:"keep_method"`
	// contains filtered or unexported fields
}

func (*DMCSession) FileExtension

func (s *DMCSession) FileExtension() string

func (*DMCSession) IsHLS

func (s *DMCSession) IsHLS() bool

IsHLS returns true if protocol is hls

func (*DMCSession) IsHTTP

func (s *DMCSession) IsHTTP() bool

IsHTTP returns true if protocol is http or https

func (*DMCSession) IsRTMP

func (s *DMCSession) IsRTMP() bool

IsRTMP returns true if protocol is rtmp

type HLSDownloader

type HLSDownloader struct {
	HttpClient *http.Client
	// contains filtered or unexported fields
}

func NewHLSDownloader

func NewHLSDownloader(client *http.Client) *HLSDownloader

func (*HLSDownloader) Download

func (c *HLSDownloader) Download(ctx context.Context, url string, w io.Writer) error

func (*HLSDownloader) GetSegment

func (c *HLSDownloader) GetSegment(ctx context.Context, url string, w io.Writer) error

func (*HLSDownloader) GetSegment2

func (c *HLSDownloader) GetSegment2(ctx context.Context, url string, w io.Writer, key, iv []byte) error

type HTTPDownloader

type HTTPDownloader struct {
	HttpClient *http.Client
}

func NewHTTPDownloader

func NewHTTPDownloader(client *http.Client) *HTTPDownloader

func (*HTTPDownloader) Download

func (c *HTTPDownloader) Download(ctx context.Context, url string, w io.Writer) error

type MyList

type MyList struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	UserID      int64  `json:"user_id,string"`
	Public      int    `json:"public,string"`

	CreatedTime int64 `json:"create_time"`
	UpdatedTime int64 `json:"update_time"`

	SortOrder     string `json:"sort_order"`
	PlaylistToken string `json:"watch_playlist"`
}

type MyListItem

type MyListItem struct {
	ItemID      string         `json:"item_id"`
	Type        MyListItemType `json:"item_type"`
	Description string         `json:"description"`
	Data        VideoInfo      `json:"item_data"`

	CreatedTime int64 `json:"create_time"`
	UpdatedTime int64 `json:"update_time"`
	Watch       int   `json:"watch"`
}

type MyListItemType

type MyListItemType int
const (
	MyListItemTypeVideo MyListItemType = 0
	MyListItemTypeSeiga MyListItemType = 5
	MyListItemTypeBook  MyListItemType = 6
)

func (*MyListItemType) UnmarshalJSON

func (m *MyListItemType) UnmarshalJSON(b []byte) error

type NicoSession

type NicoSession struct {
	NiconicoID    string `json:"niconicoId"`
	IsPremium     bool   `json:"premium"`
	SessionString string `json:"user_session"`
}

type SearchChannelMode

type SearchChannelMode string
const (
	SearchChannelModeKeyword SearchChannelMode = "s"
	SearchChannelModeTag     SearchChannelMode = "t"
)

type SearchField

type SearchField = string
const (
	SearchFieldContentID      SearchField = "contentId"
	SearchFieldTitle          SearchField = "title"
	SearchFieldLengthSeconds  SearchField = "lengthSeconds"
	SearchFieldThumbnailURL   SearchField = "thumbnailUrl"
	SearchFieldViewCounter    SearchField = "viewCounter"
	SearchFieldMylistCounter  SearchField = "mylistCounter"
	SearchFieldCommentCounter SearchField = "commentCounter"
	SearchFieldDescription    SearchField = "description"
	SearchFieldTags           SearchField = "tags"
	SearchFieldTagsExact      SearchField = "tagsExact"
	SearchFieldLockTagsExact  SearchField = "lockTagsExact"
	SearchFieldCategoryTags   SearchField = "categoryTags"
	SearchFieldGenre          SearchField = "genre"
	SearchFieldGenreKey       SearchField = "genre.keyword"
	SearchFieldStartTime      SearchField = "startTime"
	SearchFieldUserID         SearchField = "userId"
	SearchFieldChannelID      SearchField = "channelId"
	SearchFieldThreadID       SearchField = "threadId"
)

type SearchFilter

type SearchFilter interface{}

TODO

func AndFilter

func AndFilter(filters []SearchFilter) SearchFilter

func EqualFilter

func EqualFilter(field SearchField, value string) SearchFilter

func NotFilter

func NotFilter(filter SearchFilter) SearchFilter

func OrFilter

func OrFilter(filters []SearchFilter) SearchFilter

func RangeFilter

func RangeFilter(field SearchField, from, to string, includeUpper bool) SearchFilter

type SearchRequest

type SearchRequest struct {
	Query   string
	Offset  int
	Limit   int
	Sort    string
	Targets []SearchField
	Filter  SearchFilter
	Fields  []SearchField
}

type SearchResult

type SearchResult struct {
	TotalCount int
	Offset     int
	Items      []*SearchResultItem
}

type SearchResultItem

type SearchResultItem struct {
	ContentID    string `json:"contentId"`
	Title        string `json:"title"`
	ThumbnailURL string `json:"thumbnailUrl"`
	Duration     int    `json:"lengthSeconds"`
	ViewCount    int    `json:"viewCounter"`
	MylistCount  int    `json:"mylistCounter"`
	CommentCount int    `json:"commentCounter"`

	Description int    `json:"description"`
	UserID      int    `json:"userId"`
	ChannelID   int    `json:"channelId"`
	ThreadID    int    `json:"threadId"`
	Tags        string `json:"tags"`
	StartTime   string `json:"startTime"`
}

type SmileSession

type SmileSession struct {
	VideoData *VideoData
}

func (*SmileSession) FileExtension

func (s *SmileSession) FileExtension() string

type SourceStream

type SourceStream struct {
	ID           string `json:"id"`
	Available    bool   `json:"available"`
	Bitrate      int    `json:"bitrate"`
	SamplingRate int    `json:"sampling_rate"`
	Resolution   struct {
		Width  int `json:"width"`
		Height int `json:"height"`
	} `json:"resolution"`
}

type SourceStream2

type SourceStream2 struct {
	ID        string `json:"id"`
	Available bool   `json:"isAvailable"`
	Metadata  struct {
		SamplingRate int `json:"samplingRate"`
		Bitrate      int `json:"bitrate"`
		Resolution   struct {
			Width  int `json:"width"`
			Height int `json:"height"`
		} `json:"resolution"`
	} `json:"metadata"`
}

func (*SourceStream2) SourceStream

func (s *SourceStream2) SourceStream() *SourceStream

type VideoData

type VideoData struct {
	Video struct {
		ContentID      string `json:"id"`
		Title          string `json:"title"`
		ThumbnailURL   string `json:"thumbnailURL"`
		Description    string `json:"description"`
		Duration       int    `json:"duration"`
		PostedDateTime string `json:"postedDateTime"`
		ViewCount      int    `json:"viewCount"`
		MylistCount    int    `json:"mylistCount"`
		// Deprecated?
		DMC struct {
			Quality struct {
				Audios []*SourceStream `json:"audios"`
				Videos []*SourceStream `json:"videos"`
			} `json:"quality"`
			SessionAPI map[string]interface{} `json:"session_api"`
			TrackingID string                 `json:"tracking_id"`
			Encryption map[string]interface{} `json:"encryption"`
		} `json:"dmcInfo"`
		// Deprecated?
		Smile struct {
			URL              string   `json:"url"`
			CurrentQualityID string   `json:"currentQualityId"`
			QualityIds       []string `json:"qualityIds"`
			IsSlowLine       bool     `json:"isSlowLine"`
		} `json:"smileInfo"`
	} `json:"video"`
	// temp1.media.delivery.movie.audios
	Media struct {
		Delivery struct {
			Movie struct {
				Audios  []*SourceStream2       `json:"audios"`
				Videos  []*SourceStream2       `json:"videos"`
				Session map[string]interface{} `json:"session"`
			} `json:"movie"`
			Encryption map[string]interface{} `json:"encryption"`
			TrackingID string                 `json:"trackingId"`
		} `json:"delivery"`
		DeliveryLegacy struct {
		} `json:"deliveryLegacy"`
	} `json:"media"`
	Thread  map[string]interface{} `json:"thread"`
	Owner   map[string]interface{} `json:"owner"`
	Channel map[string]interface{} `json:"channel"`
	Context map[string]interface{} `json:"context"`
}

JSON.parse($("#js-initial-watch-data").dataset.apiData);

func (*VideoData) GenDMCSessionReq

func (data *VideoData) GenDMCSessionReq(audio, video string) (jsonObject, error)

func (*VideoData) GetAvailableAudio

func (v *VideoData) GetAvailableAudio() *SourceStream

func (*VideoData) GetAvailableSource

func (v *VideoData) GetAvailableSource(sources []*SourceStream) *SourceStream

func (*VideoData) GetAvailableVideo

func (v *VideoData) GetAvailableVideo() *SourceStream

func (*VideoData) GetEncryption

func (v *VideoData) GetEncryption() map[string]interface{}

func (*VideoData) GetSessionData

func (v *VideoData) GetSessionData() map[string]interface{}

func (*VideoData) IsDMC

func (v *VideoData) IsDMC() bool

func (*VideoData) IsNeedPayment

func (v *VideoData) IsNeedPayment() bool

func (*VideoData) IsSmile

func (v *VideoData) IsSmile() bool

func (*VideoData) SmileFileExtension

func (v *VideoData) SmileFileExtension() string

type VideoInfo

type VideoInfo struct {
	ContentID    string `json:"video_id"`
	Title        string `json:"title"`
	ThumbnailURL string `json:"thumbnail_url"`
	Duration     int    `json:"length_seconds,string"`
	ViewCount    int    `json:"view_counter,string"`
	MylistCount  int    `json:"mylist_counter,string"`
	CommentCount int    `json:"num_res,string"`
	StartTime    int64  `json:"first_retrieve"`

	Deleted int `json:"deleted,string"`
}

type VideoListPage

type VideoListPage struct {
	Title string
	Owner string
	Items []*VideoInfo
}

type VideoSession

type VideoSession interface {
	FileExtension() string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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