csdn

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package csdn is the library behind the csdn command: the HTTP client, request shaping, and the typed data models for CSDN (csdn.net).

The Client hits two public CSDN endpoints:

  • so.csdn.net/api/v3/search — full-text search across blog posts
  • blog.csdn.net/phoenix/web/blog/hot-rank — hourly hot article ranking

No authentication is required. It sets a real User-Agent, paces requests, and retries transient 429/5xx errors with exponential backoff.

Index

Constants

View Source
const DefaultUserAgent = "csdn/dev (+https://github.com/tamnd/csdn-cli)"

DefaultUserAgent identifies the client to CSDN.

Variables

This section is empty.

Functions

This section is empty.

Types

type Article

type Article struct {
	Rank        int    `json:"rank"`
	Title       string `json:"title"`
	Author      string `json:"author"`
	Views       string `json:"views"`
	Comments    string `json:"comments"`
	Collections string `json:"collections"`
	Date        string `json:"date"`
	URL         string `json:"url"`
}

Article is the record emitted for search results.

type Client

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

Client talks to the CSDN public APIs.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client with the given config.

func (*Client) Hot

func (c *Client) Hot(ctx context.Context, page, pageSize int) ([]HotArticle, error)

Hot returns the current hot article ranking from CSDN. page is 0-based. pageSize defaults to 20 when 0.

func (*Client) Search

func (c *Client) Search(ctx context.Context, opts SearchOpts) ([]Article, int, error)

Search returns blog articles matching query. Page is 1-based.

type Config

type Config struct {
	BaseURL    string // search base: https://so.csdn.net
	HotBaseURL string // hot rank base: https://blog.csdn.net
	UserAgent  string
	Rate       time.Duration
	Retries    int
	Timeout    time.Duration
}

Config holds constructor parameters for Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type HotArticle

type HotArticle struct {
	Rank      int    `json:"rank"`
	Score     string `json:"score"`
	Title     string `json:"title"`
	Author    string `json:"author"`
	Views     string `json:"views"`
	Comments  string `json:"comments"`
	Favorites string `json:"favorites"`
	URL       string `json:"url"`
}

HotArticle is the record emitted for hot-rank results.

type SearchOpts

type SearchOpts struct {
	Query    string // required
	Type     string // "blog" (default), "ask", "download"
	Sort     int    // 0=relevance, 1=newest, 2=hottest
	Page     int    // 1-based, default 1
	PageSize int    // default 20
}

SearchOpts controls the search request.

Jump to

Keyboard shortcuts

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