imdb

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 16 Imported by: 3

README

About imdb

Package imdb provides a simple client for IMDb.

Installing

Install in the usual Go fashion:

$ go get -u github.com/kenshaw/imdb

Using

imdb can be used similarly to the following:

// _example/example.go
package main

import (
	"context"
	"flag"
	"fmt"
	"os"

	"github.com/kenshaw/imdb"
)

func main() {
	verbose := flag.Bool("v", false, "verbose")
	typ := flag.String("t", "all", "type")
	q := flag.String("q", "", "query")
	flag.Parse()
	if err := run(context.Background(), *verbose, *typ, *q); err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}
}

func run(ctx context.Context, verbose bool, typ, q string) error {
	var opts []imdb.Option
	if verbose {
		opts = append(opts, imdb.WithLogf(fmt.Printf))
	}
	opts = append(opts, imdb.WithAppCacheDir("go-imdb-example"))
	cl := imdb.New(opts...)
	// determine what to search for
	var f func(context.Context, string, ...string) ([]imdb.Result, error)
	switch typ {
	case "all":
		f = cl.Find
	case "company":
		f = cl.FindCompany
	case "keyword":
		f = cl.FindKeyword
	case "name":
		f = cl.FindName
	case "title":
		f = cl.FindTitle
	case "movie":
		f = cl.FindMovie
	case "series":
		f = cl.FindSeries
	case "episode":
		f = cl.FindEpisode
	case "game":
		f = cl.FindGame
	default:
		return fmt.Errorf("unknown -t flag: %q", typ)
	}
	// find
	res, err := f(ctx, q)
	if err != nil {
		return err
	}
	for i, r := range res {
		fmt.Printf("%d: %v\n", i, r)
		fmt.Printf("  url: %s\n", r.URL)
	}
	return nil
}

Please see the Go Reference listing for the full API.

Documentation

Overview

Package imdb is a simple imdb client.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultTransport = http.DefaultTransport

DefaultTransport is the default http transport.

Functions

This section is empty.

Types

type Client

type Client struct {
	Transport   http.RoundTripper
	UserAgent   string
	AppCacheDir string
	// contains filtered or unexported fields
}

Client is a imdb client.

func New

func New(opts ...Option) *Client

New creates a new imdb client.

Example (FindTitle)
package main

import (
	"context"
	"fmt"

	"github.com/kenshaw/imdb"
)

func main() {
	cl := imdb.New()
	res, err := cl.FindTitle(context.Background(), "luca")
	if err != nil {
		panic(err)
	}
	if len(res) == 0 {
		fmt.Println("expected at least one result")
		return
	}
	fmt.Printf("result: %s\n", res[0])
}
Output:

result: tt12801262: "Luca" (movie, 2021) https://www.imdb.com/title/tt12801262/

func (*Client) Find

func (cl *Client) Find(ctx context.Context, q string, params ...string) ([]Result, error)

Find searches for q.

func (*Client) FindCompany

func (cl *Client) FindCompany(ctx context.Context, company string, params ...string) ([]Result, error)

FindCompany searches for a company.

func (*Client) FindEpisode

func (cl *Client) FindEpisode(ctx context.Context, episode string, params ...string) ([]Result, error)

FindEpisode searches for a episode.

func (*Client) FindGame

func (cl *Client) FindGame(ctx context.Context, game string, params ...string) ([]Result, error)

FindGame searches for a game.

func (*Client) FindKeyword

func (cl *Client) FindKeyword(ctx context.Context, keyword string, params ...string) ([]Result, error)

FindKeyword searches for a keyword.

func (*Client) FindMovie

func (cl *Client) FindMovie(ctx context.Context, movie string, params ...string) ([]Result, error)

FindMovie searches for a movie.

func (*Client) FindName

func (cl *Client) FindName(ctx context.Context, name string, params ...string) ([]Result, error)

FindName searches for a name.

func (*Client) FindSeries

func (cl *Client) FindSeries(ctx context.Context, series string, params ...string) ([]Result, error)

FindSeries searches for a series.

func (*Client) FindTitle

func (cl *Client) FindTitle(ctx context.Context, title string, params ...string) ([]Result, error)

FindTitle searches for a title.

func (*Client) FindTitleSubtype added in v0.1.9

func (cl *Client) FindTitleSubtype(ctx context.Context, subtype Subtype, title string, params ...string) ([]Result, error)

FindTitleSubtype searches for subtype with title.

func (*Client) FindType added in v0.1.9

func (cl *Client) FindType(ctx context.Context, typ Type, q string, params ...string) ([]Result, error)

FindType searches for type and q.

type Option

type Option func(*Client)

Option is a imdb client option.

func WithAppCacheDir

func WithAppCacheDir(appCacheDir string) Option

WithAppCacheDir is a imdb client option to set the app cache dir.

func WithLogf

func WithLogf(logf interface{}, opts ...httplog.Option) Option

WithLogf is a imdb client option to set a log handler for http requests and responses.

func WithTransport

func WithTransport(transport http.RoundTripper) Option

WithTransport is a imdb client option to set the http transport.

type Result

type Result struct {
	URL     string
	ID      string
	Title   string
	Type    Type
	Subtype Subtype
	Year    string
}

Result is the result of a search.

func Find added in v0.1.9

func Find(ctx context.Context, q string, opts ...Option) ([]Result, error)

Find searches for q.

func FindCompany

func FindCompany(ctx context.Context, company string, opts ...Option) ([]Result, error)

FindCompany searches for a company.

func FindEpisode

func FindEpisode(ctx context.Context, episode string, opts ...Option) ([]Result, error)

FindEpisode searches for a episode.

func FindGame

func FindGame(ctx context.Context, game string, opts ...Option) ([]Result, error)

FindGame searches for a game.

func FindKeyword

func FindKeyword(ctx context.Context, keyword string, opts ...Option) ([]Result, error)

FindKeyword searches for a keyword.

func FindMovie

func FindMovie(ctx context.Context, movie string, opts ...Option) ([]Result, error)

FindMovie searches for a movie.

func FindName

func FindName(ctx context.Context, name string, opts ...Option) ([]Result, error)

FindName searches for a name.

func FindSeries

func FindSeries(ctx context.Context, series string, opts ...Option) ([]Result, error)

FindSeries searches for a series.

func FindTitle

func FindTitle(ctx context.Context, title string, opts ...Option) ([]Result, error)

FindTitle searches for a title.

func FindTitleSubtype added in v0.1.9

func FindTitleSubtype(ctx context.Context, subtype Subtype, title string, opts ...Option) ([]Result, error)

FindTitleSubtype searches for subtype with title.

func FindType added in v0.1.9

func FindType(ctx context.Context, typ Type, q string, opts ...Option) ([]Result, error)

FindType searches for type and q.

func (Result) String

func (r Result) String() string

String satisfies the fmt.Stringer interface.

func (Result) YearInt added in v0.1.9

func (r Result) YearInt() int

YearInt returns the year as an int from the selection.

type Subtype added in v0.3.2

type Subtype string

Subtype is a subtype.

const (
	SubtypeGame    Subtype = "vg"
	SubtypeMovie   Subtype = "ft"
	SubtypeSeries  Subtype = "tv"
	SubtypeEpisode Subtype = "ep"
)

Subtype values.

func (Subtype) String added in v0.3.2

func (subtype Subtype) String() string

String satisfies the fmt.Stringer interface.

type Type added in v0.3.2

type Type string

Type is a type.

const (
	TypeAll     Type = "al"
	TypeCompany Type = "co"
	TypeKeyword Type = "kw"
	TypeName    Type = "nm"
	TypeTitle   Type = "tt"
)

Type values.

func (Type) String added in v0.3.2

func (typ Type) String() string

String satisfies the fmt.Stringer interface.

Directories

Path Synopsis
_example/example.go
_example/example.go

Jump to

Keyboard shortcuts

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