Mol improvements

This commit is contained in:
bdnugget 2025-05-28 13:15:23 +02:00
parent bd15d70d3f
commit 5df5dde506

View File

@ -4,7 +4,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"net/url"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -34,13 +34,14 @@ func (m MolCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
return
}
imgURL := fmt.Sprintf("https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?t=l&cid=%s", cid)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, imgURL)
bot.Send(msg)
photo := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FileURL(imgURL))
photo.Caption = args
bot.Send(photo)
}
func fetchPubchemCID(compound string) (string, error) {
url := "https://pubchem.ncbi.nlm.nih.gov/compound/" + compound
resp, err := http.Get(url)
apiURL := "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" + url.QueryEscape(compound) + "/cids/TXT"
resp, err := http.Get(apiURL)
if err != nil {
return "", err
}
@ -49,12 +50,11 @@ func fetchPubchemCID(compound string) (string, error) {
if err != nil {
return "", err
}
re := regexp.MustCompile(`<meta name="pubchem_uid_value" content="(\d+)"/?>`)
matches := re.FindStringSubmatch(string(body))
if len(matches) < 2 {
cid := strings.TrimSpace(string(body))
if cid == "" || strings.Contains(cid, "Status:") {
return "", fmt.Errorf("CID not found")
}
return matches[1], nil
return cid, nil
}
func init() {