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