Documentation
¶
Overview ¶
Package cronweibo 提供简单的 API 便于快速开发定时发送微博的应用
比如定时抓取图片后发送到微博、定时获取特定数据并将其保存到微博等。
使用 cronweibo 创建一个定时微博应用只需 4 个步骤:
- 传入配置实例化cronweibo
- 编写生成微博内容的函数实例化微博任务
- 注册任务
- 运行服务
微博任务(WeiboJob),包含任务名称(Name),执行周期(Schedule)和生成具体微博内容的函数(Run)等信息。
将微博任务注册到 cronweibo 服务后,cronweibo 启动后会将所有注册的任务按其执行周期定时执行该任务中的任务函数,并将其返回的内容发送到微博。
可以通过配置开启HTTP接口来调用任务便于调试。
Example (HelloWorld) ¶
定时发送hello world文字到微博示例
package main import ( "io" "log" "os" "time" "github.com/axiaoxin-com/cronweibo" ) func main() { // 从环境变量获取配置信息 appkey := os.Getenv("weibo_app_key") appsecret := os.Getenv("weibo_app_secret") username := os.Getenv("weibo_username") passwd := os.Getenv("weibo_passwd") redirecturi := os.Getenv("weibo_redirect_uri") securityURL := os.Getenv("weibo_security_url") // 创建配置 loc, _ := time.LoadLocation("Asia/Shanghai") config := &cronweibo.Config{ AppName: "example", WeiboAppkey: appkey, WeiboAppsecret: appsecret, WeiboUsername: username, WeiboPasswd: passwd, WeiboRedirecturi: redirecturi, WeiboSecurityURL: securityURL, Location: loc, HTTPServerAddr: ":2222", BasicAuthUsername: "admin", BasicAuthPasswd: "admin", } // 创建定时微博服务 c, err := cronweibo.New(config) if err != nil { log.Fatal(err) } // 定义helloworld_job的任务函数 f := func() (string, io.Reader) { return "hello world", nil } // 创建任务 helloWorldJob := cronweibo.WeiboJob{ Name: "helloworld", Schedule: "@every 2m", // 每2分钟一次 Run: f, } // 将任务注册到cronweibo c.RegisterWeiboJobs(helloWorldJob) // 启动 c.Start() }
Output:
Index ¶
- func HandlerAuth(handler http.HandlerFunc, username, passwd string) http.HandlerFunc
- type Config
- type CronJob
- type CronWeibo
- func (c *CronWeibo) Now() time.Time
- func (c *CronWeibo) RegisterCronJobs(cronJobs ...CronJob)
- func (c *CronWeibo) RegisterWeiboJobs(weiboJobs ...WeiboJob)
- func (c *CronWeibo) Start()
- func (c *CronWeibo) Token() *weibo.RespToken
- func (c *CronWeibo) UpdateToken() error
- func (c *CronWeibo) WeiboClient() *weibo.Weibo
- type WeiboJob
- type WeiboJobFunc
- type WeiboPinCrackFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandlerAuth ¶
func HandlerAuth(handler http.HandlerFunc, username, passwd string) http.HandlerFunc
HandlerAuth 为 http.HandlerFunc 包一层 basic auth
Types ¶
type Config ¶
type Config struct { AppName string // 定时微博app名称(非必填) // 微博相关配置 WeiboUsername string // 要发微博的微博登录账号(必填参数,用于模拟登录自动获取授权码) WeiboPasswd string // 要发微博的微博登录密码(必填参数,用于模拟登录 WeiboPinCrackFuncs []weibo.CrackPinFunc // 登录验证码破解函数(非必填) WeiboAppkey string // 微博应用的 appkey (必填参数) WeiboAppsecret string // 微博应用的 appsecret (必填参数) WeiboRedirecturi string // 微博应用的回调地址(必填参数) WeiboSecurityURL string // 微博应用的安全链接(必填参数,http:// + 微博应用中配置的安全域名) // cron server 相关配置 Location *time.Location // 指定定时服务的时区(非必填) // HTTP server 相关配置 HTTPServerAddr string // HTTP 服务运行地址 (非必填),设置后会运行HTTP服务提供 GET 方式请求 http://host:port/jobname 可立即执行任务 BasicAuthUsername string // 和 BasicAuthPasswd 同时配置时,会对所有的HTTP接口进行基础认证(非必填) BasicAuthPasswd string // 和 BasicAuthUsername 同时配置时,会对所有的HTTP接口进行基础认证(非必填) RetryCount int // 任务失败重试次数 RetryDuration time.Duration // 任务失败重试时间间隔 }
Config CronWeibo配置定义,New函数的参数
type CronJob ¶ added in v1.0.3
type CronJob struct { Schedule string // 定时任务表达式,同WeiboJob Name string // 任务名称 Run cron.FuncJob // 需要执行的普通任务函数 }
CronJob 默认的普通定时任务
type CronWeibo ¶
type CronWeibo struct {
// contains filtered or unexported fields
}
CronWeibo 定时微博服务定义
func (*CronWeibo) RegisterCronJobs ¶ added in v1.0.3
RegisterCronJobs 注册普通的cron任务
func (*CronWeibo) RegisterWeiboJobs ¶
RegisterWeiboJobs 注册微博任务
func (*CronWeibo) UpdateToken ¶
UpdateToken 检查access_token是否过期,过期则更新 一般情况无需使用到,默认在注册任务后执行任务时会自动检查
func (*CronWeibo) WeiboClient ¶ added in v1.0.1
WeiboClient 返回当前 weibo client
type WeiboJob ¶
type WeiboJob struct { /* Schedule 格式参考: Entry | Description | Equivalent To @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * @monthly | Run once a month, midnight, first of month | 0 0 0 1 * * @weekly | Run once a week, midnight between Sat/Sun | 0 0 0 * * 0 @daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * @hourly | Run once an hour, beginning of hour | 0 0 * * * * */ Schedule string // 定时任务表达式 Name string // 任务名称 Run WeiboJobFunc // 需要执行的微博任务函数 }
WeiboJob 微博任务定义,任务名 + 定时表达式 + 任务函数组成☝️任务
type WeiboJobFunc ¶
WeiboJobFunc 微博任务函数类型声明 不接收参数,返回微博文本内容和微博图片内容