all repos — telegram-bot-api @ 5d6f84e9b2505cf86f8ae4c02c9eb0b415895c8f

Golang bindings for the Telegram Bot API

plugin_fa.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"github.com/PuerkitoBio/goquery"
 6	"github.com/ddliu/go-httpclient"
 7	"io"
 8	"net/http"
 9	"os"
10	"strconv"
11	"strings"
12)
13
14type FAPlugin struct {
15}
16
17func (plugin *FAPlugin) GetName() string {
18	return "FA Mirrorer"
19}
20
21func (plugin *FAPlugin) GetCommands() []string {
22	return []string{"/fa"}
23}
24
25func (plugin *FAPlugin) GetHelpText() []string {
26	return []string{"/fa [link] - mirrors an image from FurAffinity"}
27}
28
29func (plugin *FAPlugin) Setup() {
30	a, ok := config.Plugins["fa_a"]
31	if !ok {
32		fmt.Print("FurAffinity Cookie a: ")
33		fmt.Scanln(&a)
34
35		config.Plugins["fa_a"] = a
36	}
37
38	b, ok := config.Plugins["fa_b"]
39	if !ok {
40		fmt.Print("FurAffinity Cookie b: ")
41		fmt.Scanln(&b)
42
43		config.Plugins["fa_b"] = b
44	}
45}
46
47func (plugin *FAPlugin) GotCommand(command string, message Message, args []string) {
48	if len(args) == 0 {
49		bot.sendMessage(NewMessage(message.Chat.Id, "You need to include a link!"))
50
51		return
52	}
53
54	bot.sendChatAction(NewChatAction(message.Chat.Id, CHAT_UPLOAD_PHOTO))
55
56	_, err := strconv.Atoi(args[0])
57	if err == nil {
58		args[0] = "http://www.furaffinity.net/view/" + args[0]
59	}
60
61	resp, err := httpclient.WithCookie(&http.Cookie{
62		Name:  "b",
63		Value: config.Plugins["fa_b"],
64	}).WithCookie(&http.Cookie{
65		Name:  "a",
66		Value: config.Plugins["fa_a"],
67	}).Get(args[0], nil)
68	if err != nil {
69		bot.sendMessage(NewMessage(message.Chat.Id, "ERR : "+err.Error()))
70	}
71
72	defer resp.Body.Close()
73
74	doc, err := goquery.NewDocumentFromReader(resp.Body)
75	if err != nil {
76		bot.sendMessage(NewMessage(message.Chat.Id, "ERR : "+err.Error()))
77	}
78
79	sel := doc.Find("#submissionImg")
80	for i := range sel.Nodes {
81		single := sel.Eq(i)
82
83		val, _ := single.Attr("src")
84
85		tokens := strings.Split(val, "/")
86		fileName := tokens[len(tokens)-1]
87
88		output, _ := os.Create(fileName)
89		defer output.Close()
90		defer os.Remove(output.Name())
91
92		resp, _ := http.Get("http:" + val)
93		defer resp.Body.Close()
94
95		io.Copy(output, resp.Body)
96
97		bot.sendPhoto(NewPhotoUpload(message.Chat.Id, output.Name()))
98	}
99}