shared

package
v0.0.0-...-ec66866 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldMsg   = "msg"
	FieldLevel = "level"
)

Variables

View Source
var (
	ErrNotImplemented  = errors.New("not implemented")
	ErrNoRemoteActions = errors.New("remote actions not available")
)

Functions

func AuthToToken

func AuthToToken(auth string) (*oauth2.Token, error)

func ChunkSlice

func ChunkSlice[T any](slice []T, chunkSize int) [][]T

Slice to chunks.

func CompareImages

func CompareImages(url1, url2 url.URL) (bool, error)

Downloads img to memory and compare.

Supports jpeg and png.

func CompareNames

func CompareNames(name1, name2 string) float64

Compare albums, tracks, artists names.

Max: 1.0 (same).

func GenerateULID

func GenerateULID() string

func GenerateWord

func GenerateWord() string

Generate random word.

Example: "Wobuxahe".

func GetEntityURL

func GetEntityURL(base string, etype EntityType, id RemoteID) url.URL

func IsContextError

func IsContextError(err error) bool

Context canceled or deadline exceeded?

func IsNil

func IsNil(values ...any) bool

Check for nil by comparing or reflect. Returns true if one of values is nil.

func IsValidPort

func IsValidPort(val int) error

func LoadImageFromUrl

func LoadImageFromUrl(url url.URL) (image.Image, error)

Supports jpeg and png.

func Max

func Max(a, b int) int

Returns a if a > b.

func Normalize

func Normalize(str string) string

Trim space -> to ASCII -> to upper.

func NormalizeStringSliceSearchablePart

func NormalizeStringSliceSearchablePart(slice []string) []string

Normalized vals in slice. Skips empty strings, cuts strings with SearchablePart.

func NumDiff

func NumDiff(v1, v2 uint64) uint64

Number difference regardless of the position of the args.

func NumDiffWeight

func NumDiffWeight(v1, v2 uint64, weightMap map[uint64]float64) float64

Number difference depend on weightMap.

Example:

v1 = 8, v2 = 10, weightMap = {0: 1.0, 1: 0.8, 2: 0.1}.

Result: 0.1.

func SameNameSlices

func SameNameSlices(s1, s2 []string) float64

Normalize and get the similarity of the slice.

Max: 1.0 if same names.

func SearchableNormalized

func SearchableNormalized(who, what string) string

Who + " " + SearchablePart(what) - all in Normalize().

func SearchablePart

func SearchablePart(str string) string

Get first word of str. It might be more convenient to find something that way.

If splitted len == 0, returns str.

func SearchablePart2

func SearchablePart2(str string) string

Get first two words of str. It might be more convenient to find something that way.

Returns str, one or two words.

func Time

func Time(timestamp int64) time.Time

Unix ms.

func TimeNano

func TimeNano(timestamp int64) time.Time

Unix nano.

func TimeNowStr

func TimeNowStr() string

Current time to readable string.

func Timestamp

func Timestamp(time time.Time) int64

Unix ms.

func TimestampNano

func TimestampNano(time time.Time) int64

Unix nano.

func TimestampNanoNow

func TimestampNanoNow() int64

Unix nano.

func TimestampNow

func TimestampNow() int64

Unix ms.

func TokenToAuth

func TokenToAuth(tok *oauth2.Token) (string, error)

Types

type Account

type Account interface {
	// Unique ID for account.
	ID() RepositoryID

	// Account remote.
	RemoteName() RemoteName

	// Any text specified by the user so that he can distinguish one account from another.
	Alias() string

	// Set account alias.
	SetAlias(string) error

	// Account auth data like json tokens.
	Auth() string

	// Set account auth.
	SetAuth(string) error

	// Actions from remote account.
	Actions() (AccountActions, error)

	// Account settings.
	Settings() (AccountSettings, error)

	// Time when account was added.
	AddedAt() time.Time

	// Delete account from repository.
	Delete() error

	Repository() (RemoteRepository, error)
}

Remote account.

type AccountActions

type AccountActions interface {
	LikedAlbums() LikedActions
	LikedArtists() LikedActions
	LikedTracks() LikedActions
	Playlist() PlaylistActions
}

Actions for remote account.

Only ErrNotImplemented can be returned as an error. But you must implement at least LikedArtists().

type AccountSettings

type AccountSettings interface {
	// Liked albums synchronization.
	LikedAlbums() SynchronizationSettings

	// Liked artists synchronization.
	LikedArtists() SynchronizationSettings

	// Liked tracks synchronization.
	LikedTracks() SynchronizationSettings

	// Playlists synchronization.
	Playlists() SynchronizationSettings

	Playlist(playlistID EntityID) (PlaylistSyncSettings, error)
}

Account settings.

type EntityID

type EntityID RepositoryID

Example: linked artist entity ID from DB.

func (EntityID) String

func (e EntityID) String() string

type EntityType

type EntityType string
const (
	EntityTypeAlbum    EntityType = "album"
	EntityTypeArtist   EntityType = "artist"
	EntityTypeTrack    EntityType = "track"
	EntityTypePlaylist EntityType = "playlist"
)

func (*EntityType) FromString

func (e *EntityType) FromString(val string)

func (EntityType) String

func (e EntityType) String() string

type ErrAccountNotExists

type ErrAccountNotExists struct {
	Prefix,
	ID string
}

func NewErrAccountNotExists

func NewErrAccountNotExists(prefix string, id string) ErrAccountNotExists

func (ErrAccountNotExists) Error

func (e ErrAccountNotExists) Error() string

type ErrNoAvailableRemotes

type ErrNoAvailableRemotes struct {
	Prefix string
}

func NewErrNoAvailableRemotes

func NewErrNoAvailableRemotes(prefix string) ErrNoAvailableRemotes

func (ErrNoAvailableRemotes) Error

func (e ErrNoAvailableRemotes) Error() string

type ErrRemoteNotFound

type ErrRemoteNotFound struct {
	Prefix string
	Name   RemoteName
}

func NewErrRemoteNotFound

func NewErrRemoteNotFound(prefix string, name RemoteName) ErrRemoteNotFound

func (ErrRemoteNotFound) Error

func (e ErrRemoteNotFound) Error() string

type ErrSnapshotNotFound

type ErrSnapshotNotFound struct {
	Prefix,
	ID string
}

func NewErrSnapshotNotFound

func NewErrSnapshotNotFound(prefix string, id string) ErrSnapshotNotFound

func (ErrSnapshotNotFound) Error

func (e ErrSnapshotNotFound) Error() string

type Event

type Event string
const (
	// No fields.
	OnAutoSyncStart Event = "OnAutoSyncStart"
	OnConfigChanged Event = "OnConfigChanged"

	// "Msg", "Level" fields.
	OnLog Event = "OnLog"
)

func (Event) String

func (e Event) String() string

type LikedActions

type LikedActions interface {
	// Examples: get liked artists.
	Liked(context.Context) (map[RemoteID]RemoteEntity, error)

	// Examples: like artists.
	Like(ctx context.Context, ids []RemoteID) error

	// Examples: unlike artists.
	Unlike(ctx context.Context, ids []RemoteID) error
}

Actions for entities created by the remote.

Example: artist, track, album.

type PlaylistActions

type PlaylistActions interface {
	// Get user playlists.
	MyPlaylists(context.Context) (map[RemoteID]RemotePlaylist, error)

	// Create playlist.
	Create(ctx context.Context, name string, isVisible bool, description *string) (RemotePlaylist, error)

	// Delete my playlists.
	Delete(ctx context.Context, entities []RemoteID) error

	// Get playlist by ID.
	Playlist(context.Context, RemoteID) (RemotePlaylist, error)
}

Playlist actions.

type PlaylistSyncSettings

type PlaylistSyncSettings interface {
	Name() SynchronizationSettings
	Description() SynchronizationSettings
	Visibility() SynchronizationSettings
	Tracks() SynchronizationSettings
}

type Remote

type Remote interface {
	// Provides repository for interacting with various things.
	//
	// Called once, after starting the program.
	Boot(RemoteRepository) error

	// Unique remote name.
	Name() RemoteName

	// Get repository.
	Repository() RemoteRepository

	// Assign actions for account.
	AssignAccountActions(Account) (AccountActions, error)

	// Get actions for remote.
	//
	// Example: take one of the accounts, and perform this actions from it.
	Actions() (RemoteActions, error)

	// Get url to entity.
	EntityURL(etype EntityType, id RemoteID) url.URL
}

Example: music streaming service.

type RemoteActions

type RemoteActions interface {
	// Get artist by ID.
	Artist(context.Context, RemoteID) (RemoteArtist, error)

	// Get track by ID.
	Track(context.Context, RemoteID) (RemoteTrack, error)

	// Get album by ID.
	Album(context.Context, RemoteID) (RemoteAlbum, error)

	// Search albums.
	SearchAlbums(context.Context, RemoteAlbum) ([10]RemoteAlbum, error)

	// Search artists.
	SearchArtists(context.Context, RemoteArtist) ([10]RemoteArtist, error)

	// Search tracks.
	SearchTracks(context.Context, RemoteTrack) ([10]RemoteTrack, error)
}

General actions for remote.

type RemoteAlbum

type RemoteAlbum interface {
	RemoteEntity

	// https://en.wikipedia.org/wiki/Universal_Product_Code
	UPC() *string

	// https://en.wikipedia.org/wiki/International_Article_Number
	EAN() *string

	Artists() []RemoteArtist

	TrackCount() int

	// Release year.
	Year() int

	// Cover (prefer 100x100).
	CoverURL() *url.URL
}

Album from remote.

type RemoteArtist

type RemoteArtist interface {
	RemoteEntity

	// Albums names (oldest first).
	OldestAlbumsNames(ctx context.Context) ([20]string, error)

	// Singles names (oldest first).
	OldestSinglesNames(ctx context.Context) ([20]string, error)
}

Artist from remote.

type RemoteEntity

type RemoteEntity interface {
	// Example: Spotify.
	RemoteName() RemoteName

	// Example: Spotify artist ID.
	ID() RemoteID

	// Example: Spotify artist name.
	Name() string
}

Remote entity.

type RemoteID

type RemoteID string

Example: artist ID on Spotify.

func (*RemoteID) FromString

func (r *RemoteID) FromString(str string)

func (RemoteID) String

func (r RemoteID) String() string

type RemoteIDSlice

type RemoteIDSlice[T comparable] []RemoteID

func (*RemoteIDSlice[T]) FromMap

func (r *RemoteIDSlice[T]) FromMap(data map[RemoteID]T)

func (*RemoteIDSlice[T]) FromMapV

func (r *RemoteIDSlice[T]) FromMapV(data map[T]RemoteID)

func (RemoteIDSlice[T]) Len

func (r RemoteIDSlice[T]) Len() int

func (RemoteIDSlice[T]) Less

func (r RemoteIDSlice[T]) Less(i, j int) bool

func (*RemoteIDSlice[T]) Reverse

func (r *RemoteIDSlice[T]) Reverse()

func (RemoteIDSlice[T]) Swap

func (r RemoteIDSlice[T]) Swap(i, j int)

type RemoteName

type RemoteName string

Example: "Spotify".

func (*RemoteName) FromString

func (r *RemoteName) FromString(val string)

func (RemoteName) String

func (r RemoteName) String() string

type RemotePlaylist

type RemotePlaylist interface {
	RemoteEntity

	FromAccount() Account

	// Should be nil only if remote doesn't support set descriptions.
	Description() *string

	// Playlist tracks.
	Tracks(context.Context) (map[RemoteID]RemoteTrack, error)

	// Rename playlist.
	Rename(context.Context, string) error

	// Set playlist description.
	SetDescription(context.Context, string) error

	// Add tracks to playlist.
	AddTracks(context.Context, []RemoteID) error

	// Remove tracks from playlist.
	RemoveTracks(context.Context, []RemoteID) error

	// Is playlist visible?
	//
	// Can return error only if remote doesn't support vis.
	IsVisible() (bool, error)

	// Set playlist visibility. True = visible.
	SetIsVisible(context.Context, bool) error
}

User playlist from remote.

type RemoteRepository

type RemoteRepository interface {
	// Unique ID.
	ID() RepositoryID

	// Remote enabled? Not enabled remotes will be excluded from sync/linker/etc.
	Enabled() bool

	SetEnabled(bool) error

	// Remote name.
	Name() RemoteName

	// Alias: any text specified by the user so that he can distinguish one account from another.
	//
	// Auth: any auth like json tokens.
	//
	// After creating the account, the AssignActions method will be called in the remote.
	CreateAccount(alias string, auth string) (Account, error)

	// All accounts in remote.
	Accounts(ctx context.Context) ([]Account, error)

	// Get account by ID. Returns nil, nil if account not found.
	Account(id RepositoryID) (Account, error)

	// Global remote actions.
	Actions() (RemoteActions, error)
}

Remote repository.

type RemoteTrack

type RemoteTrack interface {
	RemoteEntity

	// https://en.wikipedia.org/wiki/International_Standard_Recording_Code
	ISRC() *string

	Artists() []RemoteArtist

	Album() (RemoteAlbum, error)

	// Length of track in ms.
	LengthMs() int

	// Release year.
	Year() int

	// Cover (prefer 100x100).
	CoverURL() *url.URL
}

Track from remote.

type RepositoryID

type RepositoryID string

Example: ID in database.

func (RepositoryID) String

func (e RepositoryID) String() string

type SynchronizationSettings

type SynchronizationSettings interface {
	// Synchronization enabled?
	Synchronize() bool

	// Enable/disable synchronization.
	SetSynchronize(bool) error

	// Last account synchronization (nanoseconds).
	LastSynchronization() time.Time

	// Set last account synchronization (nanoseconds).
	//
	// Do not use this method in the UI and similar places. It is only needed for the synchronizer.
	SetLastSynchronization(time.Time) error
}

Remote account synchronization settings.

Jump to

Keyboard shortcuts

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