api

package
v1.8.4 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package api provides the FC2 API client.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWebSocketServerDisconnection is returned when the server disconnects.
	ErrWebSocketServerDisconnection = errors.New("server disconnected")
	// ErrWebSocketPaidProgram is returned when the server returns a paid program error.
	ErrWebSocketPaidProgram = errors.New("paid program")
	// ErrWebSocketLoginRequired is returned when the server returns a login required error.
	ErrWebSocketLoginRequired = errors.New("login required")
	// ErrWebSocketMultipleConnection is returned when the server returns a multiple connection error.
	ErrWebSocketMultipleConnection = errors.New("multiple connection error")
	// ErrWebSocketStreamEnded is returned when the server ends the stream.
	ErrWebSocketStreamEnded = errors.New("stream ended")
	// ErrWebSocketEmptyPlaylist is returned when the server does not return a valid playlist.
	ErrWebSocketEmptyPlaylist = errors.New("server did not return a valid playlist")

	// ErrQualityNotAvailable is returned when the quality is not available.
	ErrQualityNotAvailable = errors.New("requested quality is not available")

	// ErrNoResponse is returned when there is no response.
	ErrNoResponse = errors.New("no response")
)
View Source
var (
	// ErrRateLimit is returned when the API is rate limited.
	ErrRateLimit = errors.New("API rate limited")
)
View Source
var ErrUnknownLatency = errors.New("unknown latency")

ErrUnknownLatency is returned when the latency is unknown.

View Source
var ErrUnknownQuality = errors.New("unknown quality")

ErrUnknownQuality is returned when the quality is unknown.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Result      json.Number   `json:"result"`
	UserID      json.Number   `json:"userid"`
	Fc2ID       json.Number   `json:"fc2id"`
	Adult       json.Number   `json:"adult"`
	Title       string        `json:"title"`
	Description string        `json:"description"`
	URL         string        `json:"url"`
	Images      []interface{} `json:"images"`
}

Channel describes the FC2 channel.

type ChannelData

type ChannelData struct {
	ChannelID           string                `json:"channelid"`
	UserID              string                `json:"userid"`
	Adult               json.Number           `json:"adult"`
	Twoshot             json.Number           `json:"twoshot"`
	Title               string                `json:"title"`
	Info                string                `json:"info"`
	Image               string                `json:"image"`
	LoginOnly           json.Number           `json:"login_only"`
	GiftLimit           json.Number           `json:"gift_limit"`
	GiftList            []ChannelDataGiftList `json:"gift_list"`
	CommentLimit        string                `json:"comment_limit"`
	Tfollow             json.Number           `json:"tfollow"`
	Tname               string                `json:"tname"`
	Fee                 json.Number           `json:"fee"`
	Amount              json.Number           `json:"amount"`
	Interval            json.Number           `json:"interval"`
	Category            string                `json:"category"`
	CategoryName        string                `json:"category_name"`
	IsOfficial          json.Number           `json:"is_official"`
	IsPremiumPublisher  json.Number           `json:"is_premium_publisher"`
	IsLinkShare         json.Number           `json:"is_link_share"`
	Ticketid            json.Number           `json:"ticketid"`
	IsPremium           json.Number           `json:"is_premium"`
	TicketPrice         json.Number           `json:"ticket_price"`
	TicketOnly          json.Number           `json:"ticket_only"`
	IsApp               json.Number           `json:"is_app"`
	IsVideo             json.Number           `json:"is_video"`
	IsREST              json.Number           `json:"is_rest"`
	Count               json.Number           `json:"count"`
	IsPublish           int64                 `json:"is_publish"`
	IsLimited           json.Number           `json:"is_limited"`
	Start               json.Number           `json:"start"`
	Version             string                `json:"version"`
	FC2Channel          Channel               `json:"fc2_channel"`
	ControlTag          string                `json:"control_tag"`
	PublishMethod       string                `json:"publish_method"`
	VideoStereo3D       interface{}           `json:"video_stereo3d"`
	VideoMapping        interface{}           `json:"video_mapping"`
	VideoHorizontalView interface{}           `json:"video_horizontal_view"`
}

ChannelData describes the FC2 channel and stream.

type ChannelDataGiftList

type ChannelDataGiftList struct {
	ID   json.Number `json:"id"`
	Type json.Number `json:"type"`
	URL  []string    `json:"url"`
	Name string      `json:"name"`
}

ChannelDataGiftList describes the gifts that can be sent to the FC2 user.

type Client

type Client struct {
	*http.Client
}

Client is the FC2 API client.

func NewClient

func NewClient(client *http.Client) *Client

NewClient creates a new FC2 API client.

func (*Client) FindRestrictedStream

func (c *Client) FindRestrictedStream(ctx context.Context) (string, error)

FindRestrictedStream finds the first restricted stream.

func (*Client) FindUnrestrictedStream

func (c *Client) FindUnrestrictedStream(ctx context.Context) (string, error)

FindUnrestrictedStream finds the first unrestricted stream.

func (*Client) GetMeta

func (c *Client) GetMeta(ctx context.Context, channelID string) (GetMetaData, error)

GetMeta gets the metadata of the live stream.

func (*Client) GetWebSocketURL

func (c *Client) GetWebSocketURL(
	ctx context.Context,
	meta GetMetaData,
) (wsURL string, controlToken ControlToken, err error)

GetWebSocketURL gets the WebSocket URL for the live stream.

func (*Client) Login

func (c *Client) Login(ctx context.Context) error

Login to FC2 and fill the CookieJar.

You need to probably need to

func (*Client) LoginLoop

func (c *Client) LoginLoop(
	ctx context.Context,
	duration time.Duration,
)

LoginLoop will try to login to FC2 every duration.

type Comment

type Comment struct {
	UserName        string      `json:"user_name"`
	Comment         string      `json:"comment"`
	Timestamp       json.Number `json:"timestamp"`
	EncryptedUserID string      `json:"encrypted_user_id"`
	OrzToken        string      `json:"orz_token"`
	Hash            string      `json:"hash"`
	Color           string      `json:"color"`
	Size            string      `json:"size"`
	Lang            string      `json:"lang"`
	Anonymous       json.Number `json:"anonymous"`
	History         json.Number `json:"history"`
}

Comment is the response from the websocket.

type CommentArguments

type CommentArguments struct {
	Comments []Comment `json:"comments"`
}

CommentArguments is the type of response corresponding to the "comment" event.

type ControlDisconnectionArguments

type ControlDisconnectionArguments struct {
	Code int `json:"code"`
}

ControlDisconnectionArguments is the type of response corresponding to the "control_disconnection" event.

type ControlToken

type ControlToken struct {
	ID                        string      `json:"id"`
	ChannelListChannelID      string      `json:"ChannelListchannel_id"`
	UserID                    string      `json:"user_id"`
	ServiceID                 json.Number `json:"service_id"`
	OrzToken                  string      `json:"orz_token"`
	Premium                   json.Number `json:"premium"`
	Mode                      string      `json:"mode"`
	Language                  string      `json:"language"`
	ClientType                string      `json:"client_type"`
	ClientApp                 string      `json:"client_app"`
	ClientVersion             string      `json:"client_version"`
	AppInstallKey             string      `json:"app_install_key"`
	ChannelListChannelVersion string      `json:"ChannelListchannel_version"`
	IP                        string      `json:"ip"`
	Ipv6                      string      `json:"ipv6"`
	Commentable               json.Number `json:"commentable"`
	UserName                  string      `json:"user_name"`
	AdultAccess               json.Number `json:"adult_access"`
	AgentID                   json.Number `json:"agent_id"`
	CountryCode               string      `json:"country_code"`
	PayMode                   json.Number `json:"pay_mode"`
	Exp                       json.Number `json:"exp"`
	jwt.RegisteredClaims
}

ControlToken is the token used to authenticate with the FC2 API.

type GetChannelListChannel

type GetChannelListChannel struct {
	ID             string      `json:"id"`
	Bid            string      `json:"bid"`
	Video          json.Number `json:"video"`
	App            json.Number `json:"app"`
	Category       json.Number `json:"category"`
	Type           json.Number `json:"type"`
	Fc2ID          json.Number `json:"fc2id"`
	Name           string      `json:"name"`
	Title          string      `json:"title"`
	Image          string      `json:"image"`
	Start          string      `json:"start"`
	StartTime      json.Number `json:"start_time"`
	Sex            string      `json:"sex"`
	Pay            json.Number `json:"pay"`
	Interval       json.Number `json:"interval"`
	Amount         json.Number `json:"amount"`
	Lang           string      `json:"lang"`
	Total          json.Number `json:"total"`
	Count          json.Number `json:"count"`
	Login          json.Number `json:"login"`
	CommentL       json.Number `json:"comment_l"`
	Tid            json.Number `json:"tid"`
	Price          json.Number `json:"price"`
	Official       json.Number `json:"official"`
	CommentScore   json.Number `json:"comment_score"`
	DenyCountryFlg string      `json:"deny_country_flg"`
	Panorama       json.Number `json:"panorama"`
}

GetChannelListChannel describes the FC2 channel.

type GetChannelListResponse

type GetChannelListResponse struct {
	Link    string                  `json:"link"`
	IsAdult int64                   `json:"is_adult"`
	Time    int64                   `json:"time"`
	Channel []GetChannelListChannel `json:"channel"`
}

GetChannelListResponse is the response from the get_channel_list endpoint.

type GetControlServerResponse

type GetControlServerResponse struct {
	URL          string      `json:"url"`
	Orz          string      `json:"orz"`
	OrzRaw       string      `json:"orz_raw"`
	ControlToken string      `json:"control_token"`
	Status       json.Number `json:"status"`
}

GetControlServerResponse is the response from the get_control_server endpoint.

type GetMetaData

type GetMetaData struct {
	ChannelData ChannelData `json:"channel_data"`
	ProfileData ProfileData `json:"profile_data"`
	UserData    UserData    `json:"user_data"`
}

GetMetaData is the data of the response from the get_meta endpoint.

type GetMetaResponse

type GetMetaResponse struct {
	Status json.Number `json:"status"`
	Data   GetMetaData `json:"data"`
}

GetMetaResponse is the response from the get_meta endpoint.

type HLSInformation

type HLSInformation struct {
	Status                 json.Number `json:"status"`
	Playlists              []Playlist  `json:"playlists"`
	PlaylistsHighLatency   []Playlist  `json:"playlists_high_latency"`
	PlaylistsMiddleLatency []Playlist  `json:"playlists_middle_latency"`
}

HLSInformation is the response from the get_hls_information endpoint.

type Latency

type Latency int

Latency represents the latency of the live stream.

const (
	// LatencyUnknown represents an unknown latency.
	LatencyUnknown Latency = 0
	// LatencyLow represents a low latency.
	LatencyLow Latency = 1
	// LatencyHigh represents a high latency.
	LatencyHigh Latency = 2
	// LatencyMid represents a mid latency.
	LatencyMid Latency = 3
)

func LatencyFromMode

func LatencyFromMode(mode int) Latency

LatencyFromMode returns a Latency from a mode.

func LatencyParseString

func LatencyParseString(value string) Latency

LatencyParseString parses a string into a Latency.

func (Latency) String

func (l Latency) String() string

String returns the string representation of a Latency.

func (*Latency) UnmarshalText

func (l *Latency) UnmarshalText(text []byte) error

UnmarshalText unmarshals a string into a Latency.

type Playlist

type Playlist struct {
	Mode   int         `json:"mode"`
	Status json.Number `json:"status"`
	URL    string      `json:"url"`
}

Playlist describes a m3u8 playlist and its specifications.

func ExtractAndMergePlaylists

func ExtractAndMergePlaylists(hlsInfo HLSInformation) []Playlist

ExtractAndMergePlaylists extracts and merges the playlists.

func GetPlaylistOrBest

func GetPlaylistOrBest(sortedPlaylists []Playlist, expectMode int) (Playlist, error)

GetPlaylistOrBest returns the playlist that matches the mode or the best.

func SortPlaylists

func SortPlaylists(playlists []Playlist) []Playlist

SortPlaylists sorts the playlists by mode.

type ProfileData

type ProfileData struct {
	UserID string `json:"userid"`
	Fc2ID  string `json:"fc2id"`
	Name   string `json:"name"`
	Info   string `json:"info"`
	Icon   string `json:"icon"`
	Image  string `json:"image"`
	Sex    string `json:"sex"`
	Age    string `json:"age"`
}

ProfileData describes the FC2 user's profile.

type Quality

type Quality int

Quality represents the quality of the live stream.

const (
	// QualityUnknown represents an unknown quality.
	QualityUnknown Quality = 0
	// Quality150KBps represents a 150Kbps bitrate.
	Quality150KBps Quality = 10
	// Quality400KBps represents a 400Kbps bitrate.
	Quality400KBps Quality = 20
	// Quality1_2MBps represents a 1.2Mbps bitrate.
	Quality1_2MBps Quality = 30
	// Quality2MBps represents a 2Mbps bitrate.
	Quality2MBps Quality = 40
	// Quality3MBps represents a 3Mbps bitrate.
	Quality3MBps Quality = 50
	// QualitySound represents a sound only stream.
	QualitySound Quality = 90
)

func QualityFromMode

func QualityFromMode(mode int) Quality

QualityFromMode returns a Quality from a live stream mode.

func QualityParseString

func QualityParseString(value string) Quality

QualityParseString parses a string into a Quality.

func (Quality) String

func (q Quality) String() string

String implements the fmt.Stringer interface.

func (*Quality) UnmarshalText

func (q *Quality) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type UserData

type UserData struct {
	IsLogin       json.Number `json:"is_login"`
	UserID        json.Number `json:"userid"`
	Fc2ID         json.Number `json:"fc2id"`
	Icon          string      `json:"icon"`
	Name          string      `json:"name"`
	Point         interface{} `json:"point"`
	AdultAccess   interface{} `json:"adult_access"`
	Recauth       interface{} `json:"recauth"`
	IsPremiumUser interface{} `json:"is_premium_user"`
	GiftList      interface{} `json:"gift_list"`
	Stamina       interface{} `json:"stamina"`
}

UserData describes the FC2 user.

type WSResponse

type WSResponse struct {
	ID        int64           `json:"id,omitempty"`
	Name      string          `json:"name"`
	Arguments json.RawMessage `json:"arguments"`
}

WSResponse is the response from the websocket.

func (WSResponse) String

func (r WSResponse) String() string

type WebSocket

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

WebSocket is used to interact with the FC2 WebSocket.

func NewWebSocket

func NewWebSocket(
	client *http.Client,
	url string,
	healthCheckInterval time.Duration,
) *WebSocket

NewWebSocket creates a new WebSocket.

func (*WebSocket) Dial

func (w *WebSocket) Dial(ctx context.Context) (*websocket.Conn, error)

Dial connects to the WebSocket server.

func (*WebSocket) FetchPlaylist

func (w *WebSocket) FetchPlaylist(
	ctx context.Context,
	conn *websocket.Conn,
	msgChan chan *WSResponse,
	expectedMode int,
) (playlist Playlist, availables []Playlist, err error)

FetchPlaylist fetches the playlist.

func (*WebSocket) GetHLSInformation

func (w *WebSocket) GetHLSInformation(
	ctx context.Context,
	conn *websocket.Conn,
	msgChan <-chan *WSResponse,
) (HLSInformation, error)

GetHLSInformation returns the HLS information.

func (*WebSocket) HeartbeatLoop

func (w *WebSocket) HeartbeatLoop(
	ctx context.Context,
	conn *websocket.Conn,
	msgChan <-chan *WSResponse,
) error

HeartbeatLoop sends a heartbeat to keep the ws alive.

The only way to exit the heartbeat loop is to have the WS socket closed or to cancel the context.

func (*WebSocket) Listen

func (w *WebSocket) Listen(
	ctx context.Context,
	conn *websocket.Conn,
	msgChan chan<- *WSResponse,
	commentChan chan<- *Comment,
) error

Listen listens for messages from the WebSocket server.

Jump to

Keyboard shortcuts

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