自动领直播间银瓜子

Tweet | 查看源代码 | 评论

Go源码如下:

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"time"

	"github.com/hanbang-wang/bilibili-go"
	"github.com/hanbang-wang/bilibili-go/util"
	"github.com/otiai10/gosseract"
)

const (
	DOC = `用法:
account.txt 你的登录信息文件。
其中内容:第一行登录名,第二行密码

按 Ctrl+C 退出`
	SERVICE = `autograb`
	VERSION = `1.1`
)

type CurrentTask struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data struct {
		Minute    int `json:"minute"`
		Silver    int `json:"silver"`
		TimeStart int `json:"time_start"`
		TimeEnd   int `json:"time_end"`
	} `json:"data"`
}

type Award struct {
	Data struct {
		AwardSilver int `json:"awardSilver"`
		IsEnd       int `json:"isEnd"`
	} `json:"data"`
}

type UserLogin struct {
	Status bool `json:"status"`
}

var (
	letterRunes = []rune("0123456789")
	evalRegex   *regexp.Regexp
	session     *http.Client
	user, pass  string
	biliclient  *biligo.Bilibili
)

func ReadFile(path string) {
	file, err := os.Open(path)
	if os.IsNotExist(err) {
		log.Fatal("文件不存在!")
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	scanner.Scan()
	user = scanner.Text()
	scanner.Scan()
	pass = scanner.Text()
}

func RandString() string {
	b := make([]rune, 8)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}

func Eval(out string) int {
	ret := evalRegex.FindStringSubmatch(out)
	if len(ret) != 4 {
		return -1
	}
	num1, err := strconv.Atoi(ret[1])
	if err != nil {
		return -1
	}
	num2, err := strconv.Atoi(ret[3])
	if err != nil {
		return -1
	}
	switch ret[2] {
	case "+":
		return num1 + num2
	case "-":
		return num1 - num2
	default:
		return -1
	}
}

func GiveNew() *http.Client {
	jar, _ := cookiejar.New(nil)
	ret := &http.Client{Jar: jar}
	return ret
}

func GetCurrentTask() *CurrentTask {
	ret := new(CurrentTask)
	for {
		req, _ := util.Network("http://live.bilibili.com/FreeSilver/getCurrentTask", "GET", "")
		if resp, err := biliclient.Client.Do(req); err == nil {
			if err = util.JsonProc(resp, ret); err == nil {
				break
			}
		}
		time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
	}
	return ret
}

func GetCapture() int {
	var ans int
	for {
		req, _ := util.Network("http://live.bilibili.com/freeSilver/getCaptcha", "GET", "ts="+strconv.FormatInt(time.Now().Unix(), 10))
		if resp, err := biliclient.Client.Do(req); err == nil {
			filename := filepath.Join(os.TempDir(), RandString()+".jpg")
			file, err := os.Create(filename)
			if err != nil {
				log.Fatal(err)
			}
			_, err = io.Copy(file, resp.Body)
			file.Close()
			if err != nil {
				os.Remove(filename)
				time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
				continue
			}
			out := gosseract.Must(gosseract.Params{
				Src:       filename,
				Languages: "eng",
				Whitelist: "0123456789+-",
			})
			os.Remove(filename)
			if ans = Eval(out); ans != -1 {
				break
			}
		}
		time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
	}
	return ans
}

func GetAward(cur *CurrentTask) *Award {
	data := url.Values{}
	data.Add("time_start", strconv.Itoa(cur.Data.TimeStart))
	data.Add("time_end", strconv.Itoa(cur.Data.TimeEnd))
	data.Add("captcha", strconv.Itoa(GetCapture()))
	ret := new(Award)
	for {
		req, _ := util.Network("http://live.bilibili.com/FreeSilver/getAward", "GET", data.Encode())
		if resp, err := biliclient.Client.Do(req); err == nil {
			if err = util.JsonProc(resp, ret); err == nil {
				break
			}
		}
		time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
		data.Set("captcha", strconv.Itoa(GetCapture()))
	}
	return ret
}

func init() {
	if len(os.Args) < 2 {
		fmt.Println(DOC)
		os.Exit(0)
	}
	var err error
	log.Println("任务开始!")
	evalRegex, _ = regexp.Compile(`([\d]+)([+-])([\d]+)`)
	session = GiveNew()
	ReadFile(os.Args[1])
	biliclient, err = biligo.NewFromLogin(user, pass)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("登陆成功!")
}

func main() {
	for {
		req, _ := util.Network("http://live.bilibili.com/sign/doSign", "GET", "")
		biliclient.Client.Do(req)
		for {
			currentTask := GetCurrentTask()
			if currentTask.Code != 0 {
				log.Println(currentTask.Msg)
				time.Sleep(time.Hour)
				continue
			}
			log.Printf("%s:%d分钟后领取%d个银瓜子……", user, currentTask.Data.Minute, currentTask.Data.Silver)
			time.Sleep(time.Minute*time.Duration(currentTask.Data.Minute) + time.Second*2)
			award := GetAward(currentTask)
			log.Printf("%s:已领取%d个银瓜子……", user, award.Data.AwardSilver)
			if award.Data.IsEnd == 1 {
				break
			}
		}
		log.Printf("%s: 已完成!", user)
		time.Sleep(12 * time.Hour)
	}
}

这里找到源码。

用法

autograb account.txt

account.txt 你的登录信息文件。  
其中内容:第一行登录名,第二行密码。

本程序无编译好的版本,请自行编译。

使用js版吧!

只要将下面图片拖动到浏览器的书签栏
再在任意直播房间页面点击书签即可
注:请不要在多个页面同时使用(并不能加快瓜子领取)

❤领瓜子