批量下载 Bing 墙纸

cn.bing.com 的背景图片的质量还是很高的,值得下载。

package main

import (
	"compress/gzip"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path"
	"regexp"
	"strings"
)

var (
	httpc = &http.Client{}

	regWallpaper = regexp.MustCompile(`http://s.cn.bing.net/az/hprichbg/rb/[a-zA-z0-9_\-]*_1920x1080.jpg`)
)

//判断网页是否使用 gzip 压缩,如果是则进行解压
func ungzipRespBody(resp *http.Response) (b []byte, err error) {
	body := resp.Body
	if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
		body, err = gzip.NewReader(resp.Body)
		if err != nil {
			return nil, err
		}
	}
	defer body.Close()

	return ioutil.ReadAll(body)
}

func setHeaders(req *http.Request) {
	req.Header.Set("Accept-Encoding", "gzip, deflate")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Ubuntu) Chrome/38.0.2125.122")
}

//获取页面
func HttpGet(url string) ([]byte, error) {
	req, err := http.NewRequest("GET", url, nil) //创建 request
	if err != nil {
		return nil, err
	}
	setHeaders(req)

	resp, err := httpc.Do(req) //发送请求
	if err != nil {
		return nil, err
	}

	return ungzipRespBody(resp)
}

//下载文件
func Downfile(url, target string) (err error) {
	body, err := HttpGet(url)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(target, body, os.ModePerm)
}

func main() {
	body, err := HttpGet("http://cn.bing.com/HPImageArchive.aspx?format=js&idx=1&n=10")
	if err != nil {
		log.Panic(err)
	}

	wallpaper := regWallpaper.FindAll(body, -1)

	for i, wpaper := range wallpaper {
		wStr := string(wpaper)

		u, err := url.Parse(wStr)
		if err != nil {
			log.Print(err)
			continue
		}
		filename := path.Base(u.Path)

		err = Downfile(wStr, filename)
		if err != nil {
			log.Print(err)
			continue
		}

		fmt.Printf("%d. Downloaded %s\n", i, filename)
	}
}
posted @ 2015-01-26 23:39  Bing.L  阅读(582)  评论(0编辑  收藏  举报