cronweibo

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 11 Imported by: 0

README

cronweibo

简介

提供简单的 API 便于快速开发定时发送微博的应用,比如定时抓取图片后发送到微博、定时获取特定数据并将其保存到微博等。

使用 cronweibo 创建一个定时微博应用只需 4 个步骤:

  1. 传入配置实例化cronweibo
  2. 编写生成微博内容的函数实例化微博任务
  3. 注册任务
  4. 运行服务

微博任务(WeiboJob),包含任务名称(Name),执行周期(Schedule)和生成微博内容的函数(Run)等信息。

将微博任务注册到 cronweibo 服务后,cronweibo 启动后会将所有注册的任务按其执行周期定时执行该任务中的任务函数,并将其返回的内容发送到微博。

任务函数的定义为func() (string, io.Reader),返回微博文本内容和图片。

可以通过配置开启HTTP接口来调用任务便于调试。

特性

提供友好的 API 帮助你快速实现定时发送微博的服务

支持通过 HTTP 方法调用定时任务便于调试,HTTP 支持 BasicAuth

支持注册微博登录验证码破解方法全自动获取微博 Access Token

安装

go get -u github.com/axiaoxin-com/cronweibo

在线文档

https://godoc.org/github.com/axiaoxin-com/cronweibo

使用示例

一个定时发送hello world到微博的应用:

example/hello_world.go

package main

import (
	"io"
	"log"
	"os"
	"time"

	"github.com/axiaoxin-com/cronweibo"
)

// 定时发送hello world文字到微博示例
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()
}

他们在用

Documentation

Overview

Package cronweibo 提供简单的 API 便于快速开发定时发送微博的应用

比如定时抓取图片后发送到微博、定时获取特定数据并将其保存到微博等。

使用 cronweibo 创建一个定时微博应用只需 4 个步骤:

  1. 传入配置实例化cronweibo
  2. 编写生成微博内容的函数实例化微博任务
  3. 注册任务
  4. 运行服务

微博任务(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

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 New

func New(config *Config, weiboJobs ...WeiboJob) (*CronWeibo, error)

New 创建CronWeibo实例

func (*CronWeibo) Now

func (c *CronWeibo) Now() time.Time

Now 获取CronWeibo中的当前时间 应用中需要获取当前时间请使用该方法保证时间时区正确

func (*CronWeibo) RegisterCronJobs added in v1.0.3

func (c *CronWeibo) RegisterCronJobs(cronJobs ...CronJob)

RegisterCronJobs 注册普通的cron任务

func (*CronWeibo) RegisterWeiboJobs

func (c *CronWeibo) RegisterWeiboJobs(weiboJobs ...WeiboJob)

RegisterWeiboJobs 注册微博任务

func (*CronWeibo) Start

func (c *CronWeibo) Start()

Start 启动定时微博服务

func (*CronWeibo) Token added in v1.0.2

func (c *CronWeibo) Token() *weibo.RespToken

Token 返回当前 token

func (*CronWeibo) UpdateToken

func (c *CronWeibo) UpdateToken() error

UpdateToken 检查access_token是否过期,过期则更新 一般情况无需使用到,默认在注册任务后执行任务时会自动检查

func (*CronWeibo) WeiboClient added in v1.0.1

func (c *CronWeibo) WeiboClient() *weibo.Weibo

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

type WeiboJobFunc func() (string, io.Reader)

WeiboJobFunc 微博任务函数类型声明 不接收参数,返回微博文本内容和微博图片内容

type WeiboPinCrackFunc

type WeiboPinCrackFunc weibo.CrackPinFunc

WeiboPinCrackFunc 微博验证码破解函数类型声明

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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