yrs

package module
v0.0.0-...-dce030a Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 12 Imported by: 0

README

Go client library for searching tweets (X/Twitter posts) via Yahoo! Japan Realtime Search. Scrapes the __NEXT_DATA__ JSON from Yahoo! Realtime Search pages — no API keys or authentication required.

Note: This library relies on web scraping. Yahoo! may change their page structure at any time, which could break functionality.

Installation

go get github.com/cnosuke/go-yahoo-realtime-search

Library Usage

package main

import (
	"context"
	"fmt"
	"log"

	yrs "github.com/cnosuke/go-yahoo-realtime-search"
)

func main() {
	client := yrs.NewClient()

	result, err := client.Search(context.Background(), "golang")
	if err != nil {
		log.Fatal(err)
	}

	for _, tw := range result.Tweets {
		fmt.Printf("@%s: %s\n", tw.ScreenName, tw.Text)
	}
}
Options
client := yrs.NewClient(
	yrs.WithRequestTimeout(10 * time.Second),
	yrs.WithUserAgent("my-app/1.0"),
	yrs.WithHTTPClient(customHTTPClient),
)

// Limit number of results
result, err := client.SearchWithLimit(ctx, "query", 5)
Types
type SearchResult struct {
	Query  string  `json:"query"`
	Tweets []Tweet `json:"tweets"`
}

type Tweet struct {
	ID         string    `json:"id"`
	URL        string    `json:"url"`
	Text       string    `json:"text"`
	AuthorName string    `json:"author_name"`
	ScreenName string    `json:"screen_name"`
	CreatedAt  time.Time `json:"created_at"`
	ReplyCount int       `json:"reply_count"`
	RTCount    int       `json:"rt_count"`
	LikeCount  int       `json:"like_count"`
	Images     []Image   `json:"images,omitempty"`
}
Error Handling
if errors.Is(err, yrs.ErrScrapeFailed) {
	// Scraping failed (HTTP error, parsing error, etc.)
}
if errors.Is(err, yrs.ErrInvalidParameter) {
	// Invalid parameter (empty query, negative limit)
}

CLI

A command-line tool yrs is included.

Build
make build
# Binary: bin/yrs
Usage
# Basic search
bin/yrs "golang"

# Limit results
bin/yrs -l 5 "golang"

# JSON output
bin/yrs --json "golang"

# Custom timeout
bin/yrs -t 10s "golang"

Testing

# Unit tests
go test ./...

# Integration tests (hits live Yahoo)
go test -tags integration ./...

License

MIT

Documentation

Overview

Package yrs provides a Go client library for searching tweets (X/Twitter posts) via Yahoo! Japan Realtime Search. It scrapes the __NEXT_DATA__ JSON from Yahoo! Realtime Search pages, providing a clean, typed API without requiring any API keys or authentication.

Basic Usage:

client := yrs.NewClient()
result, err := client.Search(context.Background(), "golang")
if err != nil {
    log.Fatal(err)
}
for _, tw := range result.Tweets {
    fmt.Printf("@%s: %s\n", tw.ScreenName, tw.Text)
}

Index

Constants

View Source
const (
	// LibraryName is the name of this library.
	LibraryName = "go-yahoo-realtime-search"

	// LibraryVersion is the current version of this library.
	LibraryVersion = "0.1.0"

	// DefaultUserAgent is the default User-Agent header sent with HTTP requests.
	DefaultUserAgent = LibraryName + "/" + LibraryVersion

	// DefaultRequestTimeout is the default timeout for HTTP requests.
	DefaultRequestTimeout = 30 * time.Second
)

Variables

View Source
var (
	// ErrScrapeFailed is returned when the HTML scraping fails
	// (e.g., __NEXT_DATA__ not found, unexpected structure, non-200 HTTP status).
	ErrScrapeFailed = errors.New("yrs: scraping failed")

	// ErrInvalidParameter is returned for invalid input parameters.
	ErrInvalidParameter = errors.New("yrs: invalid parameter")
)

Functions

This section is empty.

Types

type Client

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

Client is the Yahoo Realtime Search client.

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient creates a new Yahoo Realtime Search client.

func (*Client) Search

func (c *Client) Search(ctx context.Context, query string) (*SearchResult, error)

Search performs a keyword search and returns tweets.

func (*Client) SearchWithLimit

func (c *Client) SearchWithLimit(ctx context.Context, query string, limit int) (*SearchResult, error)

SearchWithLimit performs a search with a maximum number of results. A limit of 0 means no limit (return all results from the page).

func (*Client) SearchWithQuery

func (c *Client) SearchWithQuery(ctx context.Context, q *Query) (*SearchResult, error)

SearchWithQuery performs a search using a Query builder.

func (*Client) SearchWithQueryAndLimit

func (c *Client) SearchWithQueryAndLimit(ctx context.Context, q *Query, limit int) (*SearchResult, error)

SearchWithQueryAndLimit performs a search using a Query builder with a result limit.

type ClientConfig

type ClientConfig struct {
	HTTPClient     *http.Client
	UserAgent      string
	RequestTimeout time.Duration
}

ClientConfig holds the resolved configuration for the client.

type ClientOption

type ClientOption func(*ClientConfig)

ClientOption configures the Client.

func WithHTTPClient

func WithHTTPClient(client *http.Client) ClientOption

WithHTTPClient sets a custom HTTP client.

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) ClientOption

WithRequestTimeout sets the timeout for HTTP requests.

func WithUserAgent

func WithUserAgent(ua string) ClientOption

WithUserAgent sets a custom User-Agent header.

type Image

type Image struct {
	URL string `json:"url"`
}

Image represents an image attached to a tweet.

type Query

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

Query builds a Yahoo Realtime Search query string.

func NewQuery

func NewQuery(keywords ...string) *Query

NewQuery creates a new Query with the given AND keywords.

func (*Query) And

func (q *Query) And(keywords ...string) *Query

And adds additional AND keywords.

func (*Query) Build

func (q *Query) Build() (string, error)

Build generates the query string. Returns an error if validation fails.

func (*Query) FromUser

func (q *Query) FromUser(username string) *Query

FromUser filters posts from a specific account (ID:username).

func (*Query) Hashtag

func (q *Query) Hashtag(tags ...string) *Query

Hashtag filters posts containing specific hashtags.

func (*Query) Not

func (q *Query) Not(keywords ...string) *Query

Not excludes posts containing the given keywords.

func (*Query) Or

func (q *Query) Or(keywords ...string) *Query

Or adds an OR group. Keywords within the group are OR-ed together.

func (*Query) ToUser

func (q *Query) ToUser(username string) *Query

ToUser filters posts mentioning/replying to a specific account (@username).

func (*Query) URL

func (q *Query) URL(urlOrDomain string) *Query

URL filters posts containing a specific URL or domain (URL:xxx, prefix match).

type SearchResult

type SearchResult struct {
	Query  string  `json:"query"`
	Tweets []Tweet `json:"tweets"`
}

SearchResult holds the result of a Yahoo Realtime Search query.

type Tweet

type Tweet struct {
	ID         string    `json:"id"`
	URL        string    `json:"url"`
	Text       string    `json:"text"`
	AuthorName string    `json:"author_name"`
	ScreenName string    `json:"screen_name"`
	CreatedAt  time.Time `json:"created_at"`

	ReplyCount int `json:"reply_count"`
	RTCount    int `json:"rt_count"`
	LikeCount  int `json:"like_count"`

	Images []Image `json:"images,omitempty"`
}

Tweet represents a single tweet extracted from Yahoo Realtime Search.

Directories

Path Synopsis
cmd
yrs command
internal
errors
Package errors provides error wrapping utilities.
Package errors provides error wrapping utilities.

Jump to

Keyboard shortcuts

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