-
Notifications
You must be signed in to change notification settings - Fork 1
/
comics.go
104 lines (90 loc) · 2.82 KB
/
comics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"fmt"
"github.com/TheDiscordian/onebot/onelib"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
)
const (
// NAME is same as filename, minus extension
NAME = "comics"
// LONGNAME is what's presented to the user
LONGNAME = "The XKCD comic plugin"
// VERSION of the plugin
VERSION = "v0.0.0"
)
// Load returns the Plugin object.
func Load() onelib.Plugin {
rand.Seed(time.Now().UnixNano())
return new(ComicPlugin)
}
// ComicPlugin is an object for satisfying the Plugin interface.
type ComicPlugin int
// Name returns the name of the plugin, usually the filename.
func (cp *ComicPlugin) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (cp *ComicPlugin) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (cp *ComicPlugin) Version() string {
return VERSION
}
func getComicInfo(url string) (title string, imageURL string, extraText string) {
resp, err := http.Get(url)
if err != nil {
return
}
var rawText []byte
rawText, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
text := string(rawText)
text = text[strings.Index(text, "<title>")+7:]
title = text[:strings.Index(text, "</title>")]
iText := text[strings.Index(text, "Image URL (for hotlinking/embedding): <a href= \"")+48:]
imageURL = iText[:strings.Index(iText, "\"")]
altText := "{{Title text: "
altTextEnd := "}}"
altTextPos := strings.Index(text, altText)
if altTextPos == -1 {
altText = "{{title text: "
altTextPos = strings.Index(text, altText)
if altTextPos == -1 {
altText = "{{alt-text: "
altTextPos = strings.Index(text, altText)
if altTextPos == -1 {
altText = "g\" title=\""
altTextEnd = "\""
altTextPos = strings.Index(text, altText)
}
}
}
if altTextPos == -1 {
return
}
text = text[altTextPos+len(altText):]
extraText = text[:strings.Index(text, altTextEnd)]
return
}
func comic(msg onelib.Message, sender onelib.Sender) {
min := 100
max := 2773 //TODO give link to main page for newest updates
url := fmt.Sprintf("https://www.xkcd.com/%d", rand.Intn(max-min+1)+min)
title, imageURL, extraText := getComicInfo(url)
text := fmt.Sprintf("Your comic: \"%s\": %s\n*%s*", title, url, extraText)
formattedText := fmt.Sprintf("Your comic: <a href=\"%s\">%s</a> (<a href=\"%s\">Web</a>)<br />\n<i>%s</i>", imageURL, title, url, extraText)
sender.Location().SendFormattedText(text, formattedText)
}
// Implements returns a map of commands and monitor the plugin implements.
func (cp *ComicPlugin) Implements() (map[string]onelib.Command, *onelib.Monitor) {
return map[string]onelib.Command{"comic": comic}, nil
}
// Remove is necessary to satisfy the Plugin interface, it does nothing.
func (cp *ComicPlugin) Remove() {
}