From 8417e8d7bbf37ac0ac25fc4915131e93ea2bcdcc Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 1 May 2014 14:31:26 +0200 Subject: Added link to save current earthporn --- README.markdown | 8 ++++++++ earthporn.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 20 ++++++++++++++++++++ template.html | 2 ++ 4 files changed, 78 insertions(+) diff --git a/README.markdown b/README.markdown index ccf2d1c..74f1feb 100644 --- a/README.markdown +++ b/README.markdown @@ -30,6 +30,14 @@ Example: add-link go http://www.golang.org add-link another\ example http://www.example.org +### `set-earthporn-savepath` + +Sets the diretory to save EarthPorn images to. + +Example: + + set-earthporn-savepath /home/foobar/Pictures/earthporn + ## Running If `$GOPATH/bin` is in your `$PATH`, you can run startpage with the command `startpage`. By default, startpage listens on port 25145. You can change that with a command line switch: `startpage -laddr :` \ No newline at end of file diff --git a/earthporn.go b/earthporn.go index a7800ef..bfa3e81 100644 --- a/earthporn.go +++ b/earthporn.go @@ -4,10 +4,13 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io" "log" "mime" "net/http" + "os" + "path" "strings" ) @@ -24,6 +27,7 @@ type EarthPorn struct { URL string `json:"url,omitempty"` Permalink string `json:"permalink"` Domain string `json:"domain"` + Saved bool data []byte mediatype string } @@ -98,4 +102,48 @@ func (p *EarthPorn) fetch() bool { p.data = buf.Bytes() return true } + +var extensions = map[string]string{ + "image/png": "png", + "image/jpeg": "jpg", + "image/gif": "gif", + "image/x-ms-bmp": "bmp", + "image/x-bmp": "bmp", + "image/bmp": "bmp", + "image/tiff": "tiff", + "image/tiff-fx": "tiff", + "image/x-targa": "tga", + "image/x-tga": "tga", + "image/webp": "webp", +} + +var savepath = "" + +func setSavepathCmd(params []string) error { + if len(params) != 1 { + return errors.New("set-earthporn-savepath needs one parameter") + } + + savepath = params[0] + return nil +} + +func (p *EarthPorn) save() error { + ext := extensions[p.mediatype] + pp := strings.Split(p.Permalink, "/") + threadid := pp[len(pp)-3] + title := strings.Replace(p.Title, "/", "-", -1) + f, err := os.Create(path.Join(savepath, threadid+" - "+title+"."+ext)) + if err != nil { + return fmt.Errorf("Could not save earthporn: %s", err) + } + defer f.Close() + + if _, err := f.Write(p.data); err != nil { + return fmt.Errorf("Could not save earthporn: %s", err) + } + + p.Saved = true + + return nil } diff --git a/main.go b/main.go index 3dcdc2e..d6d52b1 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "errors" "flag" + "fmt" "html/template" "log" "net/http" @@ -92,6 +93,7 @@ func loadTemplate() { func initCmds() { RegisterCommand("add-link", addLinkCmd) + RegisterCommand("set-earthporn-savepath", setSavepathCmd) RegisterCommand("set-weather-place", setPlaceCmd) } @@ -129,6 +131,7 @@ func main() { http.HandleFunc("/", startpage) http.HandleFunc("/bgimg", bgimg) + http.HandleFunc("/savebg", savebg) log.Fatal(http.ListenAndServe(*laddr, nil)) } @@ -139,6 +142,7 @@ type TplData struct { } func startpage(rw http.ResponseWriter, req *http.Request) { + defer req.Body.Close() if err := tpl.Execute(rw, &TplData{&porn, &weather, links}); err != nil { log.Printf("Failed executing template: %s\n", err) @@ -158,3 +162,19 @@ func bgimg(rw http.ResponseWriter, req *http.Request) { } } +func savebg(rw http.ResponseWriter, req *http.Request) { + defer req.Body.Close() + + if len(porn.data) == 0 { + fmt.Fprintln(rw, "No earth porn available") + return + } + + if err := (&porn).save(); err != nil { + log.Println(err) + fmt.Fprintln(rw, err) + } + + rw.Header().Add("Location", "/") + rw.WriteHeader(http.StatusFound) +} diff --git a/template.html b/template.html index d244b70..e23a7f3 100644 --- a/template.html +++ b/template.html @@ -76,6 +76,8 @@ {{ .Porn.Title }} + | + {{ if .Porn.Saved }}saved{{ else }}save{{ end }} \ No newline at end of file -- cgit v1.2.3-70-g09d2