mirror of https://github.com/jlelse/GoBlog
Some small fixes / improvements
This commit is contained in:
parent
ec07af9469
commit
d503f11aed
3
feeds.go
3
feeds.go
|
@ -44,7 +44,8 @@ func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r
|
||||||
}
|
}
|
||||||
for _, p := range posts {
|
for _, p := range posts {
|
||||||
var contentBuf bytes.Buffer
|
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{
|
feed.Add(&feeds.Item{
|
||||||
Title: p.RenderedTitle,
|
Title: p.RenderedTitle,
|
||||||
Link: &feeds.Link{Href: a.fullPostURL(p)},
|
Link: &feeds.Link{Href: a.fullPostURL(p)},
|
||||||
|
|
41
tts.go
41
tts.go
|
@ -1,20 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"go.goblog.app/app/pkgs/contenttype"
|
"github.com/carlmjohnson/requests"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ttsParameter = "tts"
|
const ttsParameter = "tts"
|
||||||
|
@ -167,35 +163,24 @@ func (a *goBlog) createTTSAudio(lang, text, outputFile string) error {
|
||||||
"languageCode": lang,
|
"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
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return errors.New("tts request failed: " + err.Error())
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
if res.StatusCode != http.StatusOK {
|
|
||||||
return fmt.Errorf("got status: %s, text: %s", res.Status, text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode response
|
// Decode response
|
||||||
var content map[string]interface{}
|
if encoded, ok := response["audioContent"]; ok {
|
||||||
if err = json.NewDecoder(res.Body).Decode(&content); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if encoded, ok := content["audioContent"]; ok {
|
|
||||||
if encodedStr, ok := encoded.(string); ok {
|
if encodedStr, ok := encoded.(string); ok {
|
||||||
if audio, err := base64.StdEncoding.DecodeString(encodedStr); err == nil {
|
if audio, err := base64.StdEncoding.DecodeString(encodedStr); err == nil {
|
||||||
return os.WriteFile(outputFile, audio, os.ModePerm)
|
return os.WriteFile(outputFile, audio, os.ModePerm)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
@ -9,9 +11,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
"github.com/carlmjohnson/requests"
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
"github.com/tomnomnom/linkheader"
|
"github.com/tomnomnom/linkheader"
|
||||||
"go.goblog.app/app/pkgs/contenttype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const postParamWebmention = "webmention"
|
const postParamWebmention = "webmention"
|
||||||
|
@ -70,43 +72,40 @@ func (a *goBlog) sendWebmentions(p *post) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) sendWebmention(endpoint, source, target string) error {
|
func (a *goBlog) sendWebmention(endpoint, source, target string) error {
|
||||||
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(url.Values{
|
// TODO: Pass all tests from https://webmention.rocks/
|
||||||
"source": []string{source},
|
return requests.URL(endpoint).Client(a.httpClient).Post().UserAgent(appUserAgent).
|
||||||
"target": []string{target},
|
BodyForm(url.Values{
|
||||||
}.Encode()))
|
"source": []string{source},
|
||||||
if err != nil {
|
"target": []string{target},
|
||||||
return err
|
}).
|
||||||
}
|
AddValidator(func(r *http.Response) error {
|
||||||
req.Header.Set(contentType, contenttype.WWWForm)
|
if r.StatusCode < 200 || 300 <= r.StatusCode {
|
||||||
req.Header.Set(userAgent, appUserAgent)
|
return fmt.Errorf("HTTP %d", r.StatusCode)
|
||||||
res, err := a.httpClient.Do(req)
|
}
|
||||||
if err != nil {
|
return nil
|
||||||
return err
|
}).
|
||||||
}
|
Fetch(context.Background())
|
||||||
defer res.Body.Close()
|
|
||||||
if code := res.StatusCode; code < 200 || 300 <= code {
|
|
||||||
return fmt.Errorf("response error: %v", res.StatusCode)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) discoverEndpoint(urlStr string) string {
|
func (a *goBlog) discoverEndpoint(urlStr string) string {
|
||||||
doRequest := func(method, urlStr string) string {
|
doRequest := func(method, urlStr string) string {
|
||||||
req, err := http.NewRequest(method, urlStr, nil)
|
endpoint := ""
|
||||||
if err != nil {
|
if err := requests.URL(urlStr).Client(a.httpClient).Method(method).UserAgent(appUserAgent).
|
||||||
return ""
|
AddValidator(func(r *http.Response) error {
|
||||||
}
|
if r.StatusCode < 200 || 300 <= r.StatusCode {
|
||||||
req.Header.Set(userAgent, appUserAgent)
|
return fmt.Errorf("HTTP %d", r.StatusCode)
|
||||||
resp, err := a.httpClient.Do(req)
|
}
|
||||||
if err != nil {
|
return nil
|
||||||
return ""
|
}).
|
||||||
}
|
Handle(func(r *http.Response) error {
|
||||||
defer resp.Body.Close()
|
end, err := extractEndpoint(r)
|
||||||
if code := resp.StatusCode; code < 200 || 300 <= code {
|
if err != nil || end == "" {
|
||||||
return ""
|
return errors.New("no webmention endpoint found")
|
||||||
}
|
}
|
||||||
endpoint, err := extractEndpoint(resp)
|
endpoint = end
|
||||||
if err != nil || endpoint == "" {
|
return nil
|
||||||
|
}).
|
||||||
|
Fetch(context.Background()); err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if urls, err := resolveURLReferences(urlStr, endpoint); err == nil && len(urls) > 0 && urls[0] != "" {
|
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 ""
|
return ""
|
||||||
}
|
}
|
||||||
headEndpoint := doRequest(http.MethodHead, urlStr)
|
if headEndpoint := doRequest(http.MethodHead, urlStr); headEndpoint != "" {
|
||||||
if headEndpoint != "" {
|
|
||||||
return headEndpoint
|
return headEndpoint
|
||||||
}
|
}
|
||||||
getEndpoint := doRequest(http.MethodGet, urlStr)
|
if getEndpoint := doRequest(http.MethodGet, urlStr); getEndpoint != "" {
|
||||||
if getEndpoint != "" {
|
|
||||||
return getEndpoint
|
return getEndpoint
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
|
Loading…
Reference in New Issue