-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing_wallpaper.go
131 lines (110 loc) · 2.72 KB
/
bing_wallpaper.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
)
// BingAPIResponse represents the JSON response from Bing API
type BingAPIResponse struct {
Images []struct {
URL string `json:"url"`
} `json:"images"`
}
func getBingWallpaperURL() (string, error) {
apiURL := "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"
resp, err := http.Get(apiURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
var data BingAPIResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return "", err
}
if len(data.Images) == 0 {
return "", fmt.Errorf("no images found in the response")
}
return "https://www.bing.com" + data.Images[0].URL, nil
}
func extractFileName(fileURL string) (string, error) {
parsedURL, err := url.Parse(fileURL)
if err != nil {
return "", err
}
// Extract the query parameters
params := parsedURL.Query()
id := params.Get("id")
if id == "" {
return "", fmt.Errorf("parameter 'rf' not found in the URL")
}
return id, nil
}
func downloadWallpaper(bingURL, dirPath string) (string, error) {
resp, err := http.Get(bingURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
fileName, err := extractFileName(bingURL)
if err != nil {
return "", err
}
filePath := filepath.Join(dirPath, fileName)
out, err := os.Create(filePath)
if err != nil {
return "", err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return filePath, nil
}
func main() {
usr, err := user.Current()
if err != nil {
fmt.Printf("Error getting current user: %s\n", err)
os.Exit(1)
}
defaultDirPath := filepath.Join(usr.HomeDir, "Pictures")
// Define and parse the command line flags
dirPath := flag.String("o", defaultDirPath, "Directory to save the wallpaper")
flag.Parse()
// Check if directory exists and is readable
dirInfo, err := os.Stat(*dirPath)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Provided directory does not exist: %s", *dirPath)
os.Exit(1)
}
fmt.Printf("Error accessing provided directory: %s", err)
os.Exit(1)
}
if !dirInfo.IsDir() {
fmt.Printf("Provided path is not a directory: %s", *dirPath)
os.Exit(1)
}
// Check for read permission
if dirInfo.Mode().Perm()&(1<<(uint(7))) == 0 {
fmt.Printf("Provided directory is not readable: %s", *dirPath)
os.Exit(1)
}
wallpaperURL, err := getBingWallpaperURL()
if err != nil {
fmt.Printf("Error fetching wallpaper URL: %s\n", err)
os.Exit(1)
}
filePath, err := downloadWallpaper(wallpaperURL, *dirPath)
if err != nil {
fmt.Printf("Error downloading wallpaper: %s\n", err)
os.Exit(1)
}
fmt.Printf("Bing wallpaper download successfully: %s\n", filePath)
}