api

package
v1.2.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package api provide a client for the withny API.

Index

Constants

This section is empty.

Variables

View Source
var ErrStreamNotFound = errors.New("stream not found")

ErrStreamNotFound is when no stream is found when looking for the playback URL.

Functions

func FindGraphQLEndpointAndStreamUUID added in v0.2.0

func FindGraphQLEndpointAndStreamUUID(r io.Reader) (endpoint, suuid string, err error)

FindGraphQLEndpointAndStreamUUID finds the GraphQL endpoint and stream UUID.

Types

type AgencySecret

type AgencySecret struct {
	ID          int64  `json:"id"`
	UUID        string `json:"uuid"`
	ChannelName string `json:"username"`
	Name        string `json:"name"`
}

AgencySecret is the agency secret of the cast.

type Cast

type Cast struct {
	ID                      json.Number              `json:"id"`
	UUID                    string                   `json:"uuid"`
	Coupon                  string                   `json:"coupon"`
	ProfileImageURL         string                   `json:"profileImageUrl"`
	HeaderImageURL          string                   `json:"headerImageUrl"`
	IsFavorite              bool                     `json:"isFavorite"`
	CastSocialMediaAccounts []CastSocialMediaAccount `json:"castSocialMediaAccounts"`
	AgencySecret            AgencySecret             `json:"agencySecret"`
}

Cast is the cast of the user.

type CastSocialMediaAccount

type CastSocialMediaAccount struct {
	Platform    string `json:"platform"`
	ChannelName string `json:"username"`
}

CastSocialMediaAccount is the social media account of the cast.

type Claims

type Claims struct {
	jwt.RegisteredClaims
	UserUUID  string `json:"userUuid"`
	TokenUUID string `json:"tokenUuid"`
	Scope     string `json:"scope"`
}

Claims is the JWT claims for the withny API.

type Client

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

Client is a withny API client.

func NewClient

func NewClient(client *http.Client, reader CredentialsReader) *Client

NewClient creates a new withny API client.

func (*Client) GetPlaylists

func (c *Client) GetPlaylists(ctx context.Context, playbackURL string) ([]Playlist, error)

GetPlaylists will fetch the playlists from the given playbackURL.

func (*Client) GetStreamPlaybackURL

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

GetStreamPlaybackURL will fetch the playback URL for the given streamID.

func (*Client) GetStreams

func (c *Client) GetStreams(ctx context.Context, channelID string) (GetStreamsResponse, error)

GetStreams will fetch the streams for the given channelID.

func (*Client) GetUser

func (c *Client) GetUser(ctx context.Context, channelID string) (GetUserResponse, error)

GetUser will fetch the user for the given channelID.

func (*Client) Login

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

Login will login to withny and store the credentials in the client.

func (*Client) LoginLoop

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

LoginLoop will login to withny and refresh the token when needed.

func (*Client) LoginWithRefreshToken

func (c *Client) LoginWithRefreshToken(
	ctx context.Context,
	refreshToken string,
) (Credentials, error)

LoginWithRefreshToken will login with the given refreshToken.

func (*Client) LoginWithUserPassword

func (c *Client) LoginWithUserPassword(
	ctx context.Context,
	username, password string,
) (Credentials, error)

LoginWithUserPassword will login with the given email and password.

func (*Client) NewAuthRequestWithContext

func (c *Client) NewAuthRequestWithContext(
	ctx context.Context,
	method, url string,
	body io.Reader,
) (*http.Request, error)

NewAuthRequestWithContext creates a new authenticated request with the given context.

func (*Client) SetCredentials

func (c *Client) SetCredentials(creds Credentials)

SetCredentials sets the credentials for the client.

type Comment added in v0.2.0

type Comment struct {
	StreamUUID   string      `json:"streamUUID"`
	CommentUUID  string      `json:"commentUUID"`
	UserUUID     string      `json:"userUUID"`
	Username     string      `json:"username"`
	Name         string      `json:"name"`
	ContentType  string      `json:"contentType"`
	Content      string      `json:"content"`
	TipAmount    json.Number `json:"tipAmount"`
	ItemID       string      `json:"itemID"`
	ItemName     string      `json:"itemName"`
	ItemURI      string      `json:"itemURI"`
	AnimationURI string      `json:"animationURI"`
	ItemPower    json.Number `json:"itemPower"`
	ItemLifetime json.Number `json:"itemLifetime"`
	CreatedAt    *string     `json:"createdAt"`
	UpdatedAt    *string     `json:"updatedAt"`
	DeletedAt    *string     `json:"deletedAt"`
}

Comment is the comment of the stream.

type Credentials

type Credentials struct {
	Claims
	LoginResponse
}

Credentials is the credentials for the withny API.

type CredentialsReader

type CredentialsReader interface {
	Read() (SavedCredentials, error)
}

CredentialsReader is an interface for reading saved credentials.

type ErrorResponse added in v0.2.10

type ErrorResponse struct {
	Message string      `json:"message"`
	Status  json.Number `json:"status"`
}

ErrorResponse is the error response of the API.

type GetPlaybackURLError added in v0.2.10

type GetPlaybackURLError struct {
	Err      error
	StreamID string
}

GetPlaybackURLError is an error given by the GetStreamPlaybackURL API.

func (GetPlaybackURLError) Error added in v0.2.10

func (e GetPlaybackURLError) Error() string

Error returns the error message.

type GetStreamsResponse

type GetStreamsResponse []GetStreamsResponseElement

GetStreamsResponse is the response of the get streams request.

type GetStreamsResponseElement

type GetStreamsResponseElement struct {
	UUID            string      `json:"uuid"`
	Title           string      `json:"title"`
	About           string      `json:"about"`
	ThumbnailURL    string      `json:"thumbnailUrl"`
	BillingMode     string      `json:"billingMode"`
	Price           json.Number `json:"price"`
	StreamingMethod string      `json:"streamingMethod"`
	StartedAt       time.Time   `json:"startedAt"`
	ClosedAt        interface{} `json:"closedAt"`
	DeviceID        json.Number `json:"deviceId"`
	Cast            Cast        `json:"cast"`
	HasTicket       bool        `json:"hasTicket"`
}

GetStreamsResponseElement is the element of the get streams response.

type GetUserResponse

type GetUserResponse struct {
	ID                            json.Number `json:"id"`
	UUID                          string      `json:"uuid"`
	Username                      string      `json:"username"`
	Name                          string      `json:"name"`
	ProfileText                   string      `json:"profileText"`
	ProfileImageURL               string      `json:"profileImageUrl"`
	HeaderImageURL                string      `json:"headerImageUrl"`
	Cast                          Cast        `json:"cast"`
	CreateRoomNotificationEnabled bool        `json:"createRoomNotificationEnabled"`
	ProfileTextPlain              string      `json:"profileTextPlain"`
}

GetUserResponse is the response of the get user request.

type LoginResponse

type LoginResponse struct {
	Token        string `json:"token"`
	RefreshToken string `json:"refreshToken"`
	TokenType    string `json:"tokenType"`
}

LoginResponse is the response of the login request.

type MetaData

type MetaData struct {
	User   GetUserResponse
	Stream GetStreamsResponseElement
}

MetaData is the metadata of the stream.

type Playlist

type Playlist struct {
	Bandwidth  int64
	Resolution string
	Codecs     string
	Video      string
	FrameRate  float64
	URL        string
}

Playlist represents a stream in an M3U8 playlist.

func GetBestPlaylist

func GetBestPlaylist(
	streams []Playlist,
	constraints ...PlaylistConstraint,
) (best Playlist, found bool)

GetBestPlaylist returns the best playlist based on the constraints.

func ParseM3U8

func ParseM3U8(r io.Reader) (streams []Playlist)

ParseM3U8 parses an M3U8 playlist and returns a list of streams.

type PlaylistConstraint

type PlaylistConstraint struct {
	MinBandwidth int64    `yaml:"minBandwidth"`
	MaxBandwidth int64    `yaml:"maxBandwidth"`
	MinHeight    int64    `yaml:"minHeight"`
	MaxHeight    int64    `yaml:"maxHeight"`
	MinWidth     int64    `yaml:"minWidth"`
	MaxWidth     int64    `yaml:"maxWidth"`
	MinFrameRate float64  `yaml:"minFrameRate"`
	MaxFrameRate float64  `yaml:"maxFrameRate"`
	AudioOnly    bool     `yaml:"audioOnly"`
	Ignored      []string `yaml:"ignored"`
}

PlaylistConstraint is used to filter playlists based on their attributes.

type SavedCredentials

type SavedCredentials struct {
	Username     string `yaml:"username"`
	Password     string `yaml:"password"`
	Token        string `json:"token"`
	RefreshToken string `json:"refreshToken"`
}

SavedCredentials is the saved credentials given by the user for the withny API.

type Scraper added in v0.2.0

type Scraper struct {
	*Client
}

Scraper is used to scrape the withny website.

func NewScraper added in v0.2.0

func NewScraper(client *Client) *Scraper

NewScraper creates a new Scraper.

A scraper is needed since Withny is using SSR.

func (*Scraper) FindGraphQLAndStreamUUID added in v0.2.0

func (s *Scraper) FindGraphQLAndStreamUUID(
	ctx context.Context,
	channelID string,
) (endpoint, suuid string, err error)

FindGraphQLAndStreamUUID finds the GraphQL endpoint.

The GraphQL endpoint is hard-coded on the website and uses AWS AppSync. Technically, we could just hard-code it too, but to avoid any "unexpected" changes, we'll just scrape it.

type ServerError added in v1.1.1

type ServerError struct {
	Status int
	Body   string
}

ServerError is an error given by the withny server.

func (ServerError) Error added in v1.1.1

func (e ServerError) Error() string

Error returns the error message.

type UnauthorizedError added in v0.2.6

type UnauthorizedError struct {
	Body string
}

UnauthorizedError is when the request is unauthorized.

func (UnauthorizedError) Error added in v0.2.6

func (e UnauthorizedError) Error() string

Error returns the error message.

type WSCommentResponse added in v0.2.0

type WSCommentResponse struct {
	Data struct {
		OnPostComment Comment `json:"onPostComment"`
	} `json:"data"`
}

WSCommentResponse is the response of the WebSocket GraphQL Comments API.

type WSResponse added in v0.2.0

type WSResponse struct {
	Type    string          `json:"type"`
	ID      string          `json:"id"`
	Payload json.RawMessage `json:"payload"`
}

WSResponse is the response from the WebSocket.

type WebSocket added in v0.2.0

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

WebSocket is used to interact with the withny WebSocket.

func NewWebSocket added in v0.2.0

func NewWebSocket(
	client *Client,
	url string,
) *WebSocket

NewWebSocket creates a new WebSocket.

func (*WebSocket) ConnectionInit added in v0.2.0

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

ConnectionInit initializes the connection to the WebSocket.

func (*WebSocket) Dial added in v0.2.0

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

Dial connects to the WebSocket server.

func (*WebSocket) Subscribe added in v0.2.0

func (w *WebSocket) Subscribe(ctx context.Context, conn *websocket.Conn, streamID string) error

Subscribe subscribes to the WebSocket.

func (*WebSocket) WatchComments added in v0.2.0

func (w *WebSocket) WatchComments(
	ctx context.Context,
	conn *websocket.Conn,
	streamID string,
	commentChan chan<- *Comment,
) error

WatchComments listens for comments on the WebSocket.

Jump to

Keyboard shortcuts

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