Some small fixes / improvements

pull/10/head
Jan-Lukas Else 9 months ago
parent ec07af9469
commit d503f11aed
  1. 3
      feeds.go
  2. 41
      tts.go
  3. 73
      webmentionSending.go

@ -44,7 +44,8 @@ func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r
}
for _, p := range posts {
var contentBuf bytes.Buffer
_, _ = a.min.Write(&contentBuf, contenttype.HTML, []byte(a.feedHtml(p)))
// _, _ = a.min.Write(&contentBuf, contenttype.HTML, []byte(a.feedHtml(p)))
contentBuf.WriteString(a.feedHtml(p)) // Don't minify for now, might break some feed readers
feed.Add(&feeds.Item{
Title: p.RenderedTitle,
Link: &feeds.Link{Href: a.fullPostURL(p)},

@ -1,20 +1,16 @@
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"go.goblog.app/app/pkgs/contenttype"
"github.com/carlmjohnson/requests"
)
const ttsParameter = "tts"
@ -167,35 +163,24 @@ func (a *goBlog) createTTSAudio(lang, text, outputFile string) error {
"languageCode": lang,
},
}
jb, err := json.Marshal(body)
if err != nil {
return err
}
// Create request
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "https://texttospeech.googleapis.com/v1beta1/text:synthesize?key="+gctts.GoogleAPIKey, bytes.NewReader(jb))
if err != nil {
return err
}
req.Header.Set(contentType, contenttype.JSON)
req.Header.Set(userAgent, appUserAgent)
// Do request
res, err := a.httpClient.Do(req)
var response map[string]interface{}
err := requests.
URL("https://texttospeech.googleapis.com/v1beta1/text:synthesize").
Param("key", gctts.GoogleAPIKey).
Client(a.httpClient).
UserAgent(appUserAgent).
Post().
BodyJSON(body).
ToJSON(&response).
Fetch(context.Background())
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("got status: %s, text: %s", res.Status, text)
return errors.New("tts request failed: " + err.Error())
}
// Decode response
var content map[string]interface{}
if err = json.NewDecoder(res.Body).Decode(&content); err != nil {
return err
}
if encoded, ok := content["audioContent"]; ok {
if encoded, ok := response["audioContent"]; ok {
if encodedStr, ok := encoded.(string); ok {
if audio, err := base64.StdEncoding.DecodeString(encodedStr); err == nil {
return os.WriteFile(outputFile, audio, os.ModePerm)

@ -1,6 +1,8 @@
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
@ -9,9 +11,9 @@ import (
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/carlmjohnson/requests"
"github.com/thoas/go-funk"
"github.com/tomnomnom/linkheader"
"go.goblog.app/app/pkgs/contenttype"
)
const postParamWebmention = "webmention"
@ -70,43 +72,40 @@ func (a *goBlog) sendWebmentions(p *post) error {
}
func (a *goBlog) sendWebmention(endpoint, source, target string) error {
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(url.Values{
"source": []string{source},
"target": []string{target},
}.Encode()))
if err != nil {
return err
}
req.Header.Set(contentType, contenttype.WWWForm)
req.Header.Set(userAgent, appUserAgent)
res, err := a.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if code := res.StatusCode; code < 200 || 300 <= code {
return fmt.Errorf("response error: %v", res.StatusCode)
}
return nil
// TODO: Pass all tests from https://webmention.rocks/
return requests.URL(endpoint).Client(a.httpClient).Post().UserAgent(appUserAgent).
BodyForm(url.Values{
"source": []string{source},
"target": []string{target},
}).
AddValidator(func(r *http.Response) error {
if r.StatusCode < 200 || 300 <= r.StatusCode {
return fmt.Errorf("HTTP %d", r.StatusCode)
}
return nil
}).
Fetch(context.Background())
}
func (a *goBlog) discoverEndpoint(urlStr string) string {
doRequest := func(method, urlStr string) string {
req, err := http.NewRequest(method, urlStr, nil)
if err != nil {
return ""
}
req.Header.Set(userAgent, appUserAgent)
resp, err := a.httpClient.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
if code := resp.StatusCode; code < 200 || 300 <= code {
return ""
}
endpoint, err := extractEndpoint(resp)
if err != nil || endpoint == "" {
endpoint := ""
if err := requests.URL(urlStr).Client(a.httpClient).Method(method).UserAgent(appUserAgent).
AddValidator(func(r *http.Response) error {
if r.StatusCode < 200 || 300 <= r.StatusCode {
return fmt.Errorf("HTTP %d", r.StatusCode)
}
return nil
}).
Handle(func(r *http.Response) error {
end, err := extractEndpoint(r)
if err != nil || end == "" {
return errors.New("no webmention endpoint found")
}
endpoint = end
return nil
}).
Fetch(context.Background()); err != nil {
return ""
}
if urls, err := resolveURLReferences(urlStr, endpoint); err == nil && len(urls) > 0 && urls[0] != "" {
@ -114,12 +113,10 @@ func (a *goBlog) discoverEndpoint(urlStr string) string {
}
return ""
}
headEndpoint := doRequest(http.MethodHead, urlStr)
if headEndpoint != "" {
if headEndpoint := doRequest(http.MethodHead, urlStr); headEndpoint != "" {
return headEndpoint
}
getEndpoint := doRequest(http.MethodGet, urlStr)
if getEndpoint != "" {
if getEndpoint := doRequest(http.MethodGet, urlStr); getEndpoint != "" {
return getEndpoint
}
return ""

Loading…
Cancel
Save