diff --git a/README.md b/README.md index 55c8f05..05ec119 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ desktop500px will udpate Mac desktop picture every hour. ### Install * Register your application at [https://500px.com/settings/applications]() -* Download and unpack install package like [desktop500px_v0.2.7z](https://github.com/pihao/desktop500px/releases/download/v0.2/desktop500px_v0.2.7z) +* Download and unpack install package from [https://github.com/pihao/desktop500px/releases/latest]() * Edit `key.json` with your 500px information * Execute commands in terminal: ``` diff --git a/app/app.go b/app/app.go index a642dce..2c6e880 100644 --- a/app/app.go +++ b/app/app.go @@ -6,7 +6,7 @@ import ( ) const ( - VERSION = `desktop500px 0.2` + VERSION = `desktop500px 0.3` ) var ( diff --git a/app/helper.go b/app/helper.go index 6f887f5..bed7e5a 100644 --- a/app/helper.go +++ b/app/helper.go @@ -38,20 +38,33 @@ func Osascript(script string) { Trace("Complete.") } -func ApplyDesktop(path string) { - scpt := fmt.Sprintf(`tell application "System Events" +func ApplyDesktop(paths ...string) { + if len(paths) < 2 { + log.Fatal("ERROR::ApplyDesktop(): paths length less than 2.") + } + scpt := fmt.Sprintf(`set arr to {"%v", "%v"} +tell application "System Events" set desktopCount to count of desktops repeat with i from 1 to desktopCount + if i <= count of arr then + set img to item i of arr + else + set img to item 1 of arr + end if tell desktop i - set picture to "%v" + set picture to img end tell end repeat -end tell`, path) +end tell`, paths[0], paths[1]) Osascript(scpt) } // Fix Mac desktop picture cache by dynamic picture name -func imageFile() string { - name := fmt.Sprintf("%v.jpg", time.Now().Second()) - return path.Join(PictureDir, name) +func ImageFiles(count int) *[]string { + arr := []string{} + name := time.Now().Second() + for i := 0; i < count; i++ { + arr = append(arr, path.Join(PictureDir, fmt.Sprintf("%v_%v.jpg", name, i))) + } + return &arr } diff --git a/app/scraper.go b/app/scraper.go index a9234b4..3feb90c 100644 --- a/app/scraper.go +++ b/app/scraper.go @@ -10,23 +10,19 @@ import ( ) // "https://500px.com/photo/179916915/matera-blue-hour-ii-by-pier-luigi-girola" -func Scrape(pageUrl string) { +func Scrape(pageUrl string, imageFile string) { if pageUrl == "" { - Trace("pageUrl is none.") - return + log.Fatal("pageUrl is none.") } Trace(pageUrl) imgUrl := getImageUrl(pageUrl) if imgUrl == "" { - Trace("imgUrl is none.") - return + log.Fatal("imgUrl is none.") } Trace(imgUrl) - f := imageFile() - saveImage(imgUrl, f) - ApplyDesktop(f) + saveImage(imgUrl, imageFile) } // https://drscdn.500px.org/photo/180711743/q%3D80_m%3D2000/90e17dd62445eed988029bdf528906b2 diff --git a/bin/desktop500px.7z b/bin/desktop500px.7z new file mode 100644 index 0000000..d871094 Binary files /dev/null and b/bin/desktop500px.7z differ diff --git a/px500/px500.go b/px500/px500.go index 6205e79..5e43f6c 100644 --- a/px500/px500.go +++ b/px500/px500.go @@ -15,10 +15,12 @@ func Run() { api := API{*client, *accessToken} photos := api.GetPhotos() - p := randPhoto(photos) - pageUrl := fmt.Sprintf("%v%v", "https://www.500px.com", p.Url) - - app.Scrape(pageUrl) + f := *app.ImageFiles(2) + page1 := fmt.Sprintf("%v%v", "https://www.500px.com", randPhoto(photos).Url) + page2 := fmt.Sprintf("%v%v", "https://www.500px.com", randPhoto(photos).Url) + app.Scrape(page1, f[0]) + app.Scrape(page2, f[1]) + app.ApplyDesktop(f[0], f[1]) } func randPhoto(photos *Photos) *Photo {