twupload

package module
v0.1.0 Latest Latest
Warning

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

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

README

twupload

Unofficial, and incomplete, implementation of uploading to twitter.

See https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload

Usage

import "github.com/nabowler/twupload"


uploader, err := twupload.UploaderConfig{
    TwitterAPIKey: os.Getenv("TWITTER_API_KEY"),
    TwitterAPISecret: os.Getenv("TWITTER_API_SECRET"),
    TwitterAccessToken: os.Getenv("TWITTER_ACCESS_TOKEN"),
    TwitterAccessSecret: os.Getenv("TWITTER_ACCESS_SECRET"),
    
    ChunkSize: 1024 * 1024, // Max 5MB
}.NewUploader()

if err != nil {
    // handle error
}

data := // retrieve data

// set your timeout based on your needs
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
defer cancel()

response, err := uploader.Upload(ctx, data)

if err != nil {
    // handle err
}

// use the response data in order to create a tweet with the media

Documentation

Index

Constants

View Source
const (
	MediaCategoryImage        MediaCategory = "tweet_image"
	MediaCategoryGif          MediaCategory = "tweet_gif"
	MediaCategoryTweetVideo   MediaCategory = "tweet_video"
	MediaCategoryAmplifyVideo MediaCategory = "amplify_video"

	ProcessingStatePending    ProcessingState = "pending"
	ProcessingStateInProgress ProcessingState = "in_progress"
	ProcessingStateSucceeded  ProcessingState = "succeeded"
	PorcessingStateFailed     ProcessingState = "failed"

	MaxChunkSize = 5 * 1024 * 1024
)
View Source
const (
	ErrBadHttpStatus = sentinelError("Non 200-level http status")
)
View Source
const (
	ErrUnknownMediaType sentinelError = "Unknown Media Type"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Image

type Image struct {
	ImageType string `json:"image_type"`
	W         int64  `json:"w"`
	H         int64  `json:"h"`
}

type MediaCategory

type MediaCategory string

type MediaUploadResponse

type MediaUploadResponse struct {
	MediaID          int64           `json:"media_id"`
	MediaIDString    string          `json:"media_id_string"`
	MediaKey         string          `json:"media_key"`
	Size             int64           `json:"size"`
	ExpiresAfterSecs int64           `json:"expires_after_secs"`
	Image            *Image          `json:"image"`
	Video            *Video          `json:"video"`
	ProcessingInfo   *ProcessingInfo `json:"processing_info"`
}

type ProcessingError

type ProcessingError struct {
	Code    int    `json:"code"`
	Name    string `json:"name"`
	Message string `json:"message"`
}

func (ProcessingError) Error

func (err ProcessingError) Error() string

type ProcessingInfo

type ProcessingInfo struct {
	State           ProcessingState  `json:"state"`
	CheckAfterSecs  int              `json:"check_after_secs"`
	ProgressPercent int              `json:"progress_percent"`
	ProcessingError *ProcessingError `json:"error"`
}

type ProcessingState

type ProcessingState string

type Uploader

type Uploader interface {
	Upload(ctx context.Context, data []byte) (MediaUploadResponse, error)
}

type UploaderConfig

type UploaderConfig struct {
	TwitterAPIKey       string
	TwitterAPISecret    string
	TwitterAccessToken  string
	TwitterAccessSecret string

	ChunkSize int
}

func (UploaderConfig) NewUploader

func (uc UploaderConfig) NewUploader() (Uploader, error)

type Video

type Video struct {
	VideoType string `json:"video_type"`
}

Jump to

Keyboard shortcuts

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