diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-05-01 14:31:26 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-05-01 14:33:15 +0200 |
commit | 8417e8d7bbf37ac0ac25fc4915131e93ea2bcdcc (patch) | |
tree | 62addea43d66e1dec1f8ebd1efc6184de8c28830 /earthporn.go | |
parent | a1b3afb58145cfcda6b206bcbab833fafbc7c55d (diff) | |
download | startpage-8417e8d7bbf37ac0ac25fc4915131e93ea2bcdcc.tar.gz startpage-8417e8d7bbf37ac0ac25fc4915131e93ea2bcdcc.tar.bz2 startpage-8417e8d7bbf37ac0ac25fc4915131e93ea2bcdcc.zip |
Added link to save current earthporn
Diffstat (limited to 'earthporn.go')
-rw-r--r-- | earthporn.go | 48 |
1 files changed, 48 insertions, 0 deletions
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 } |