Skip to content

Commit

Permalink
Improve features.
Browse files Browse the repository at this point in the history
* Landscape picture has high priority.
* Update README.
  • Loading branch information
pihao committed Nov 3, 2016
1 parent e691308 commit cab2758
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 27 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# desktop500px

desktop500px will udpate Mac desktop picture every hour.

### Install

* Register your application at [https://500px.com/settings/applications]()
* Download this repository
* Open `publish` directory
* Download and unpack install package like [desktop500px_v0.2.7z](https://github.com/pihao/desktop500px/releases/download/v0.2/desktop500px_v0.2.7z)
* Edit `key.json` with your 500px information
* Unpack `desktop500px.7z`
* Execute commands:
* Execute commands in terminal:
```
$ cd "path/to/publish/dir"
$ cd "path/to/install/package/dir"
$ ./desktop500px -i
```

Expand Down
11 changes: 7 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
)

const (
VERSION = `desktop500px 0.1`
VERSION = `desktop500px 0.2`
)

var Debug = false
var (
Debug = false

var AppDir = path.Join(os.Getenv("HOME"), ".desktop500px")
var KeyFile = path.Join(AppDir, "key.json")
AppDir = path.Join(os.Getenv("HOME"), ".desktop500px")
PictureDir = path.Join(AppDir, "img")
KeyFile = path.Join(AppDir, "key.json")
)
4 changes: 2 additions & 2 deletions app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ end tell`, path)

// Fix Mac desktop picture cache by dynamic picture name
func imageFile() string {
name := fmt.Sprintf("%v.jpg", time.Now().Hour())
return path.Join(AppDir, name)
name := fmt.Sprintf("%v.jpg", time.Now().Second())
return path.Join(PictureDir, name)
}
1 change: 1 addition & 0 deletions app/install_or_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Uninstall() {
func Install() {
fmt.Println("Generate key and plist file...")
Cmd("mkdir", "-p", AppDir)
Cmd("mkdir", "-p", PictureDir)
generateKeyFile()
generatePlistFile()

Expand Down
3 changes: 2 additions & 1 deletion app/scraper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package app

import (
"github.com/PuerkitoBio/goquery"
"io"
"log"
"net/http"
"os"

"github.com/PuerkitoBio/goquery"
)

// "https://500px.com/photo/179916915/matera-blue-hour-ii-by-pier-luigi-girola"
Expand Down
File renamed without changes.
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ package main
import (
"flag"
"fmt"

"github.com/pihao/desktop500px/app"
"github.com/pihao/desktop500px/px500"
)

func main() {
i := flag.Bool("i", false, "enable/disable install mode.")
u := flag.Bool("u", false, "enable/disable uninstall mode.")
r := flag.Bool("r", false, "enable/disable reinstall mode.")
d := flag.Bool("d", false, "enable/disable debug mode.")
v := flag.Bool("v", false, "show version.")
flag.Parse()

i, u, r, d, v := getFlag()
app.Debug = *d

if *i {
Expand All @@ -29,3 +24,13 @@ func main() {
px500.Run()
}
}

func getFlag() (i, u, r, d, v *bool) {
i = flag.Bool("i", false, "install.")
u = flag.Bool("u", false, "uninstall.")
r = flag.Bool("r", false, "reinstall.")
d = flag.Bool("d", false, "debug mode.")
v = flag.Bool("v", false, "show version.")
flag.Parse()
return i, u, r, d, v
}
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"testing"
)

func Test_main(t *testing.T) {
i, u, r, d, v := getFlag()
if *i != false {
t.Error(`"-i" default value should be false`)
}
if *u != false {
t.Error(`"-u" default value should be false`)
}
if *r != false {
t.Error(`"-r" default value should be false`)
}
if *d != false {
t.Error(`"-d" default value should be false`)
}
if *v != false {
t.Error(`"-v" default value should be false`)
}
}
Binary file removed publish/desktop500px.7z
Binary file not shown.
1 change: 1 addition & 0 deletions px500/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package px500

import (
"encoding/json"

"github.com/pihao/desktop500px/app"
"github.com/pihao/go-oauth/oauth"
)
Expand Down
8 changes: 4 additions & 4 deletions px500/api_photos.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
)

type Photos struct {
CurrentPage int
TotalPages int
TotalItems int
Photos []Photo
Feature string
Photos []Photo
}

type Photo struct {
Expand All @@ -21,6 +19,8 @@ type Photo struct {
Lens string
FocalLength string `json:"focal_length"`
ISO string
Width int
Height int
TimesViewed int `json:"times_viewed"`
Rating float32
Url string
Expand Down
5 changes: 3 additions & 2 deletions px500/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package px500

import (
"encoding/json"
"github.com/pihao/desktop500px/app"
"github.com/pihao/go-oauth/oauth"
"io/ioutil"
"log"
"path"

"github.com/pihao/desktop500px/app"
"github.com/pihao/go-oauth/oauth"
)

var accessTokenFile = path.Join(app.AppDir, "access_token.json")
Expand Down
17 changes: 15 additions & 2 deletions px500/px500.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ func Run() {
api := API{*client, *accessToken}

photos := api.GetPhotos()
i := rand.Intn(len(photos.Photos))
pageUrl := fmt.Sprintf("%v%v", "https://www.500px.com", photos.Photos[i].Url)
p := randPhoto(photos)
pageUrl := fmt.Sprintf("%v%v", "https://www.500px.com", p.Url)

app.Scrape(pageUrl)
}

func randPhoto(photos *Photos) *Photo {
return __randPhoto(photos, 0)
}
func __randPhoto(photos *Photos, randCount int) *Photo {
p := photos.Photos[rand.Intn(len(photos.Photos))]
if p.Width > p.Height || randCount > 10 {
return &p
} else {
randCount++
return __randPhoto(photos, randCount)
}
}
28 changes: 28 additions & 0 deletions px500/px500_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package px500

import (
"testing"
)

func Test_randPhoto(t *testing.T) {
photos := Photos{
Photos: []Photo{
Photo{
Width: 1,
Height: 2,
},
Photo{
Width: 3,
Height: 4,
},
Photo{
Width: 6,
Height: 5,
},
},
}
p := randPhoto(&photos)
if p.Width != 6 || p.Height != 5 {
t.Error("The width must larger than the height.")
}
}
7 changes: 7 additions & 0 deletions task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fmt:

go fmt . ./app ./px500

test:

go test . ./app ./px500

0 comments on commit cab2758

Please sign in to comment.