Skip to content

Commit

Permalink
parse lichess link
Browse files Browse the repository at this point in the history
  • Loading branch information
aimerneige committed May 12, 2024
1 parent 067e619 commit 79668cf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/plugin/chess/chess.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ func init() {
}
}
})
engine.OnRegex("^https://lichess.org/([0-9]|[a-z]|[A-Z])+$").
SetPriority(2).
SetBlock(true).
Handle(func(ctx *zero.Ctx) {
senderUin := ctx.Event.Sender.ID
url := ctx.Event.Message.String()
replyMessage := ParseLichessLink(senderUin, url)
if replyMessage == nil {
return
}
messageID := ctx.Event.MessageID
ctx.Send(message.ReplyWithMessage(messageID, replyMessage...))
})
engine.OnFullMatch("cheese").
SetPriority(2).
SetBlock(true).
Expand Down
47 changes: 47 additions & 0 deletions internal/plugin/chess/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
_ "embed"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -398,6 +401,25 @@ func GenerateGIF(senderUin int64, pgnStr string) message.Message {
return message.Message{message.Image("base64://" + gif)}
}

func ParseLichessLink(senderUin int64, lichessUrl string) message.Message {
pgnStr, err := parseLichessPgn(lichessUrl)
if (err != nil) || (pgnStr == "") {
log.Errorln("[chess]", "Fail to parse lichess Url", err)
return nil
}
cacheFileName := fmt.Sprintf("gen_%d", senderUin)
gif, msg, err := generateGIF(cacheFileName, pgnStr)
if err != nil {
log.Errorln("[chess]", "Fail to generate GIF.", msg, err)
return nil
}
cacheTempGIFName := cacheFileName + ".gif"
cacheTempPGNName := cacheFileName + ".pgn"
os.Remove(path.Join(tempFileDir, cacheTempGIFName))
os.Remove(path.Join(tempFileDir, cacheTempPGNName))
return message.Message{message.Image("base64://" + gif)}
}

// createGame 创建游戏
func createGame(isBlindfold bool, groupCode int64, senderUin int64, senderName string) message.Message {
if room, ok := instance.gameRooms[groupCode]; ok {
Expand Down Expand Up @@ -646,6 +668,31 @@ func generateGIF(cacheGIFName, pgnStr string) (string, string, error) {
return gifB64, "", err
}

func parseLichessPgn(url string) (string, error) {
var response *http.Response
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode == http.StatusNotFound {
return "", nil
}
data, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
html := string(data)
r := regexp.MustCompile(`<div class="pgn">([\S\s]*?)</div>`)
matched := r.FindStringSubmatch(html)
if len(matched) != 2 {
return "", err
}
pgn := matched[1]
pgn = strings.Replace(pgn, "&quot;", "\"", -1)
return pgn, nil
}

func cleanTempFiles(groupCode int64) error {
svgFilePath := path.Join(tempFileDir, fmt.Sprintf("%d.svg", groupCode))
if err := os.Remove(svgFilePath); err != nil {
Expand Down

0 comments on commit 79668cf

Please sign in to comment.