weibo

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package weibo is the library behind the weibo command: the HTTP client and the typed data models for Weibo public surfaces.

Two hosts are used: weibo.com for the hot-rank board and search suggestions, and m.weibo.cn for post detail and comments. Each needs different headers. No cookies or authentication are required for any of these surfaces.

Index

Constants

View Source
const DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"

DefaultUserAgent is a desktop Chrome string for weibo.com endpoints.

Variables

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

ErrNotFound means the post or resource does not exist.

View Source
var ErrWalled = errors.New("this surface requires a logged-in Weibo session")

ErrWalled means the API returned ok:-100, meaning the surface requires a logged-in session. User profile and post timeline are examples.

Functions

func BaseIdentity added in v0.1.1

func BaseIdentity() kit.Identity

BaseIdentity is the help and version identity shared by the binary and any host.

func Defaults added in v0.1.1

func Defaults(c *kit.Config)

Defaults seeds the framework baseline from the weibo defaults.

func MapErr added in v0.1.1

func MapErr(err error) error

MapErr converts a library error into the kit error kind with the right exit code.

func ParseStatusID added in v0.1.1

func ParseStatusID(input string) (string, error)

ParseStatusID extracts the numeric post id from a full URL, a mobile URL, or a bare id string. Accepted forms:

5309997458393240
https://m.weibo.cn/detail/5309997458393240
https://m.weibo.cn/status/5309997458393240
https://weibo.com/2656274875/R4c4VzdsQ  (bid form — rejected, needs fetch)

ParseStatusID is the exported form used by tests and ops.go.

func Register added in v0.1.1

func Register(app *kit.App)

Register is a convenience so callers don't need to name the zero-value Domain.

Types

type Client

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

Client talks to Weibo's public JSON API.

func NewClient

func NewClient(cfg Config) *Client

NewClient builds a Client from cfg.

func (*Client) Comments added in v0.1.1

func (c *Client) Comments(ctx context.Context, id string, limit int) ([]Comment, error)

Comments returns up to limit comments under the post with the given id. Weibo paginates via max_id; this fetches a single page (up to ~20 items). Pass limit=0 for no cap beyond the single page.

func (*Client) HotSearch

func (c *Client) HotSearch(ctx context.Context, limit int) ([]HotItem, error)

HotSearch returns the Weibo hot search board in rank order. If limit > 0, only the first limit items are returned.

func (*Client) StatusByID added in v0.1.1

func (c *Client) StatusByID(ctx context.Context, id string) (Status, error)

StatusByID returns one Weibo post by its numeric id string.

func (*Client) Suggest added in v0.1.1

func (c *Client) Suggest(ctx context.Context, query string, limit int) ([]Suggestion, error)

Suggest returns search autocomplete suggestions for the given query.

type Comment added in v0.1.1

type Comment struct {
	ID        string `json:"id"`
	Floor     int    `json:"floor"     table:",right"`
	Text      string `json:"text"      kit:"body"          table:",truncate"`
	CreatedAt string `json:"created_at"`
	Source    string `json:"source"`
	Likes     int    `json:"likes"     table:",right"`
	Username  string `json:"username"`
	UserID    int64  `json:"user_id"`
}

Comment is one comment under a Weibo post.

type Config

type Config struct {
	BaseURL       string
	MobileBaseURL string
	UserAgent     string
	Rate          time.Duration
	Retries       int
	Timeout       time.Duration
}

Config holds the tunable client settings.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type Domain added in v0.1.1

type Domain struct{}

Domain is the Weibo driver for the any-cli/kit framework.

func (Domain) Info added in v0.1.1

func (Domain) Info() kit.DomainInfo

Info describes the scheme and hostnames a pasted link is matched against.

func (Domain) Register added in v0.1.1

func (Domain) Register(app *kit.App)

Register installs the client factory and every Weibo operation onto app.

type HotItem

type HotItem struct {
	Rank     int    `json:"rank"     table:",right"`
	Word     string `json:"word"`
	Scheme   string `json:"scheme"`
	Heat     int    `json:"heat"     table:",right"`
	Category string `json:"category"`
	Label    string `json:"label"`
	IsAd     bool   `json:"is_ad"`
	URL      string `json:"url"      kit:"url" table:",truncate"`
}

HotItem is a single entry from the Weibo hot search board (微博热搜榜).

type Session added in v0.1.1

type Session struct {
	Client *Client
	Quiet  bool
}

Session is the per-run client kit injects into every operation.

func (*Session) Progressf added in v0.1.1

func (s *Session) Progressf(format string, args ...any)

Progressf prints a one-line progress note to stderr unless quiet.

type Status added in v0.1.1

type Status struct {
	ID        string `json:"id"`
	Bid       string `json:"bid"`
	Text      string `json:"text"      kit:"body"          table:",truncate"`
	CreatedAt string `json:"created_at"`
	Source    string `json:"source"`
	Reposts   int    `json:"reposts"   table:",right"`
	Comments  int    `json:"comments"  table:",right"`
	Likes     int    `json:"likes"     table:",right"`
	IsLong    bool   `json:"is_long"`
	PicNum    int    `json:"pic_num"   table:",right"`
	Username  string `json:"username"`
	UserID    int64  `json:"user_id"`
	URL       string `json:"url"       kit:"url"           table:",truncate"`
}

Status is a single Weibo post fetched by its numeric id.

type Suggestion added in v0.1.1

type Suggestion struct {
	Word  string `json:"word"`
	Count int    `json:"count"     table:",right"`
	IsHot bool   `json:"is_hot"`
}

Suggestion is one autocomplete entry from the Weibo search sidebar.

Jump to

Keyboard shortcuts

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