Skip to content

Commit

Permalink
Stability improvements
Browse files Browse the repository at this point in the history
- Auto fallback to create a page with random string if create page failure
- Append title to query if title is randomized
  • Loading branch information
web-flow committed May 25, 2021
1 parent 220623b commit 85ca843
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -185,7 +185,7 @@ vet:

run:
@echo "-> Running docker container"
$(DOCKER) run -ti --rm -v ${PWD}/../:${HOMEPATH} ${IMAGE} sh -c "\
$(DOCKER) run -ti --memory="500m" --rm -v ${PWD}/../:${HOMEPATH} ${IMAGE} sh -c "\
cd ${HOMEPATH}/${NAME} && \
go get -v && \
sh"
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Expand Up @@ -6,9 +6,8 @@ require (
github.com/google/uuid v1.2.0 // indirect
github.com/kallydev/telegraph-go v1.0.0
github.com/oliamb/cutter v0.2.2
github.com/wabarc/helper v0.0.0-20210420080053-35532f1f6a79
github.com/wabarc/helper v0.0.0-20210511232523-5ac25c99226f
github.com/wabarc/imgbb v1.0.0
github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee
github.com/wabarc/screenshot v1.1.0
golang.org/x/sys v0.0.0-20210419170143-37df388d1f33 // indirect
github.com/wabarc/screenshot v1.1.2-0.20210525222550-07612de838bb
)
29 changes: 15 additions & 14 deletions go.sum
@@ -1,15 +1,16 @@
github.com/chromedp/cdproto v0.0.0-20210323015217-0942afbea50e h1:UimnzLuARNkGi2XsNznUoOLFP/noktdUMrr7fcb3D4U=
github.com/chromedp/cdproto v0.0.0-20210323015217-0942afbea50e/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/chromedp v0.6.10 h1:Yd4X6ngkWbn6A+hv6mUzV9kVHrPn7L4+vf2uyNbze2s=
github.com/chromedp/chromedp v0.6.10/go.mod h1:Q8L2uDLH9YFYbThK5fqPpyWa3CT4y9dqHLxaQr+Yhl8=
github.com/chromedp/cdproto v0.0.0-20210508221054-d7cfa85db7d1/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/cdproto v0.0.0-20210526005521-9e51b9051fd0 h1:aIcgRshD5I1MfJfB92KBDKpaXrYqj3fkqI8bHdtP3zA=
github.com/chromedp/cdproto v0.0.0-20210526005521-9e51b9051fd0/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/chromedp v0.7.2 h1:uNztM7xDJex+Mu0JUbpBDS7uCN/J71ZiCiJyHUwdrHg=
github.com/chromedp/chromedp v0.7.2/go.mod h1:9BUOYV/gTXtIpGCoM+kVSmh1j6M9oG48qGW1z3h72Z0=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.4 h1:5eXU1CZhpQdq5kXbKb+sECH5Ia5KiO6CYzIzdlVx6Bs=
github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/gobwas/ws v1.1.0-rc.5 h1:QOAag7FoBaBYYHRqzqkhhd8fq5RTubvI4v3Ft/gDVVQ=
github.com/gobwas/ws v1.1.0-rc.5/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -26,18 +27,18 @@ github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/wabarc/helper v0.0.0-20210127120855-10af37cc2616/go.mod h1:N9P4r7Rn46p4nkWtXV6ztN3p5ACVnp++bgfwjTqSxQ8=
github.com/wabarc/helper v0.0.0-20210420080053-35532f1f6a79 h1:J9YG66PKaYbCpOoE9nh5aNHZ8uFXoVGT29WPR2iDjTs=
github.com/wabarc/helper v0.0.0-20210420080053-35532f1f6a79/go.mod h1:TuTZtoiOu984UWOf7FfX58JllKMjq7FCz701kB5W88E=
github.com/wabarc/helper v0.0.0-20210511232523-5ac25c99226f h1:jXJc/PEFPYqLZDRaC4CAADgbuedfZ9o2+TyJyoqtGuA=
github.com/wabarc/helper v0.0.0-20210511232523-5ac25c99226f/go.mod h1:TuTZtoiOu984UWOf7FfX58JllKMjq7FCz701kB5W88E=
github.com/wabarc/imgbb v1.0.0 h1:vNud9qY4dRGp1nikb+GHIrc/sE3WOdNq5gfT/vhWEUQ=
github.com/wabarc/imgbb v1.0.0/go.mod h1:piz6QXkVfFpQbaQHx70/rPDwaICkW5Ew3lShTzbnVt0=
github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee h1:MMIp++7eem2CI1jIYDoPByMwXeZAjsFo2ciBNtvhB80=
github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee/go.mod h1:4uYr9fnQaQoDk1ttTzLnSB3lZm3i/vrJwN8EZIB2YuI=
github.com/wabarc/screenshot v1.1.0 h1:jNZYcEqo0kn4WIqxCpO+nBZmLCU5L6oO/TFa8i02mtQ=
github.com/wabarc/screenshot v1.1.0/go.mod h1:rMg+tlBB4wTNAo3zDFtqJUsCAVe2s1ymcD4wz8doCF0=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210419170143-37df388d1f33 h1:zah5VTTvBlVRELjcDwGLLaWRHZJQsBtplweVYCii0KM=
golang.org/x/sys v0.0.0-20210419170143-37df388d1f33/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/wabarc/screenshot v1.1.2-0.20210525222550-07612de838bb h1:RPnrRcfV4gU8WrAPvrQmNJuJbyHjkDnYJ/H/vCuQw/o=
github.com/wabarc/screenshot v1.1.2-0.20210525222550-07612de838bb/go.mod h1:49mgVjU6cu00A2HbXchIofUuRGeMycJRLl3gMq3u+dI=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea h1:+WiDlPBBaO+h9vPNZi8uJ3k4BkKQB7Iow3aqwHVA5hI=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
Expand Down
66 changes: 44 additions & 22 deletions ph.go
Expand Up @@ -11,6 +11,7 @@ import (
"image/png"
"io/ioutil"
"net"
"net/url"
"os"
"strings"
"time"
Expand All @@ -24,7 +25,7 @@ import (
)

type subject struct {
title string
title []rune
source string
}

Expand Down Expand Up @@ -129,7 +130,7 @@ func (arc *Archiver) Wayback(links []string) (map[string]string, error) {
if strings.TrimSpace(shot.Title) == "" {
shot.Title = "Missing Title"
}
arc.subject = subject{title: shot.Title, source: shot.URL}
arc.subject = subject{title: []rune(shot.Title), source: shot.URL}
go arc.post(file.Name(), ch)
// Replace posted result in the map
collect[shot.URL] = <-ch
Expand All @@ -139,7 +140,7 @@ func (arc *Archiver) Wayback(links []string) (map[string]string, error) {
}

func (arc *Archiver) post(imgpath string, ch chan<- string) {
if arc.subject.title == "" {
if len(arc.subject.title) == 0 {
ch <- "Title is required"
return
}
Expand All @@ -159,22 +160,22 @@ func (arc *Archiver) post(imgpath string, ch chan<- string) {
// ch <- fmt.Sprintf("%v", err)
// return
// }
paths, err := upload(imgpath)
if err != nil {
ch <- fmt.Sprintf("%v", err)
paths, er := upload(imgpath)
if er != nil {
ch <- fmt.Sprintf("%v", er)
return
}

nodes := []telegraph.Node{}
nodes = append(nodes, "source: ")
nodes = append(nodes, telegraph.NodeElement{
Tag: "a",
Attrs: map[string]string{
"href": arc.subject.source,
"target": "_blank",
},
Children: []telegraph.Node{arc.subject.source},
})
// nodes = append(nodes, "source: ")
// nodes = append(nodes, telegraph.NodeElement{
// Tag: "a",
// Attrs: map[string]string{
// "href": arc.subject.source,
// "target": "_blank",
// },
// Children: []telegraph.Node{arc.subject.source},
// })
for _, path := range paths {
nodes = append(nodes, telegraph.NodeElement{
Tag: "img",
Expand All @@ -184,20 +185,42 @@ func (arc *Archiver) post(imgpath string, ch chan<- string) {
},
})
}

page, err := arc.client.CreatePage(arc.subject.title, []telegraph.Node{
nodes = []telegraph.Node{
telegraph.NodeElement{
Tag: "",
Children: nodes,
},
}, &telegraph.CreatePageOption{
}

var pat bool
var err error
var page *telegraph.Page
var title = string(arc.subject.title)
if page, err = arc.client.CreatePage(title, nodes, nil); err != nil {
// Create page with random path if title illegal previous
if page, err = arc.client.CreatePage(helper.RandString(6, ""), nodes, nil); err != nil {
logger.Error("[telegra.ph] create page failed: %v", err)
ch <- "FAILED"
return
}
pat = true
}

opts := &telegraph.EditPageOption{
AuthorName: "Source",
AuthorURL: arc.subject.source,
ReturnContent: false,
})
if err != nil {
ch <- fmt.Sprintf("%v", err)
}
if page, err = arc.client.EditPage(page.Path, title, nodes, opts); err != nil {
logger.Error("[telegra.ph] edit page failed: %v", err)
ch <- "FAILED"
return
}

if pat {
page.URL += "?title=" + url.PathEscape(title)
}

ch <- page.URL
}

Expand All @@ -206,7 +229,6 @@ func (arc *Archiver) newClient() (*telegraph.Client, error) {
if err != nil {
return nil, err
}
// TODO: random name
account, err := client.CreateAccount("telegraph-go", &telegraph.CreateAccountOption{
AuthorName: "Anonymous",
AuthorURL: "https://example.org",
Expand Down
2 changes: 1 addition & 1 deletion ph_test.go
Expand Up @@ -70,7 +70,7 @@ func TestPost(t *testing.T) {
t.Error(err)
}
arc.client = client
arc.subject = subject{title: "testing", source: "http://example.org"}
arc.subject = subject{title: []rune("testing"), source: "http://example.org"}

ch := make(chan string, 1)
defer close(ch)
Expand Down

0 comments on commit 85ca843

Please sign in to comment.