summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown8
-rw-r--r--earthporn.go48
-rw-r--r--main.go20
-rw-r--r--template.html2
4 files changed, 78 insertions, 0 deletions
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 :<port>` \ 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 @@
</div>
<span id="earthporninfo">
<a href="http://reddit.com{{ .Porn.Permalink }}">{{ .Porn.Title }}</a>
+ |
+ {{ if .Porn.Saved }}saved{{ else }}<a href="/savebg">save</a>{{ end }}
</span>
</body>
</html> \ No newline at end of file