forked from chkhetiani/bwall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
134 lines (117 loc) · 2.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// make_http_request.go
package main
import (
"github.com/reujab/wallpaper"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
)
// getImageLink parses Bing and gets img url
// https://www.devdungeon.com/content/web-scraping-go
func getImageURL() (imgURL string, imgFilename string) {
domain, url := "https://www.bing.com/", "https://cn.bing.com/"
// Make HTTP GET request
response, err := http.Get(domain)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Turn HTML string to document
re := regexp.MustCompile("data-ultra-definition-src=\".+?\\.jpg")
htmlData, _ := ioutil.ReadAll(response.Body)
imgURL = re.FindString(string(htmlData))
imgURL = imgURL[28:]
imgFilename = imgURL[6:]
//document, err := goquery.NewDocumentFromReader(response.Body)
//if err != nil {
// log.Fatal("Error loading HTTP response body.\n", err)
//}
// Find background url
//document.Find("link#bgLink").Each(func(index int, element *goquery.Selection) {
// imgSrc, exists := element.Attr("href")
// if exists {
// url = domain + imgSrc
// }
//})
if 0 < len(imgURL) {
log.Println("Image URL found: " + imgURL)
return url + imgURL, imgFilename
} else {
return "", ""
}
}
// exists returns whether the given file or directory exists
// https://stackoverflow.com/a/10510783
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// downloadImg saves image to ./.data directory
// and returns file path
// https://stackoverflow.com/questions/22417283/save-an-image-from-url-to-file
func downloadImg(url string, imgFileName string) string {
dir := "./.data/"
dirExists, err := exists(dir)
if err != nil {
log.Fatal("Error finding directory:\n", err)
}
if !dirExists {
err = os.Mkdir(dir, 0777)
if err != nil {
log.Fatal("Couldn't Create Directory\n", err)
}
}
imageFilePath := dir + imgFileName
fileExists, err := exists(imageFilePath)
if !fileExists {
// get image
log.Println("Downloading image file to: " + imageFilePath)
response, err := http.Get(url)
if err != nil {
log.Fatal("Couln't Download Image\n", err)
}
defer response.Body.Close()
// create and open file
i := len(url) - 1
for i >= 0 && url[i] != '.' {
i--
}
file, err := os.Create(imageFilePath)
if err != nil {
log.Fatal("Couldn't Create File\n", err)
}
defer file.Close()
// Copy to file
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal("Couldn't Save Image\n", err)
}
} else {
log.Println("Image file already existed. skip download.")
}
// Get current directory
// https://gist.github.com/arxdsilva/4f73d6b89c9eac93d4ac887521121120
dir, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
return dir + imageFilePath[1:]
}
func setImageAsWallpaper() {
url, filename := getImageURL()
file := downloadImg(url, filename)
wallpaper.SetFromFile(file)
log.Println("Enjoy, bye.")
}
func main() {
setImageAsWallpaper()
}