Go language read json and download HD sister pictures _golang

Source: Internet
Author: User
Tags mkdir pow sprintf

Learn a day Golang, while looking at the document side write go read analysis json and download pictures
JSON API for Love wallpaper HD mac 1920x1200 version, HD sister figure

Summary: Go+json=shit

Package main import ("FMT" "io" "io/ioutil" "Net/http" "OS" "Path/filepath" "StrConv"//"Encoding/json" "Strings" "Github.com/bitly/go-simplejson") const (DataRoot = "./tmp/"//Storage cover page Map root timeoutlimit = 10//Set Timeout time pageurl = "http://api.lovebizhi.com/macos_v4.php?a=category&spdy=1&tid=3&order=hot&color_id= 3&device=105&uuid=436e4ddc389027ba3aef863a27f6e6f9&mode=0&retina=0&client_id=1008& Device_id=31547324&model_id=105&size_id=0&channel_id=70001&screen_width=1920&screen_height =1200&bizhi_width=1920&bizhi_height=1200&version_code=19&language=zh-hans&jailbreak=0& MAC=&P={PID} ")//wallpaper type, numbered, long width and URL type wallpaper struct {pid int URL string Width int Height int}// Download and save the picture to the local func saveimage (paper *wallpaper) {res, err: = http. Get (paper. URL) Defer Res. Body.close () If Err!= nil {fmt. Printf ("%d HTTP error:%s", paper.)
 Pid, err) return} Save picture by resolution directory DirName: = DataRoot + StrConv. Itoa (paper. Width) + "x" + StrConv. Itoa (paper. Height) + "/" if! Isdirexist (dirname) {OS.
    Mkdir (dirname, 0755); Fmt. Printf ("dir%s created\n", dirname)}//create file based on URL filename filename: = filepath. Base (paper. URL) DST, err: = OS. Create (dirname + filename) If Err!= nil {fmt. Println ("%d HTTP error:%s", paper.) Pid, err) return}//write file IO. Copy (DST, Res. Body)} Func isdirexist (path string) bool {p, err: = OS. Stat (path) If Err!= nil {return OS. Isexist (Err)} else {return P.isdir ()}} func main () {//check and create a temporary directory if! isdirexist (DataRoot) {OS.
    Mkdir (DataRoot, 0755); Fmt. Println ("dir%s created", DataRoot)}//Generate a data sequence to get pagination pow: = make ([]int, 2) for I: = Range Pow {if (i > 0) {URL: = strings. Replace (Pageurl, "{pid}", StrConv.)
      Itoa (i),-1); Fmt.
      Println (i, URL); Response, Err: = http. Get (URL) if (err!= nil) {FMT.
PRINTLN (ERR) continue      Body, _: = Ioutil. ReadAll (response. Body) js, err: = Simplejson. Newjson (body)//traversal of data under All information: = JS. Get ("Data").
          Mustarray () for _, V: = Range Data {V: = V. (map[string]interface{}) for KK, VV: = Range v { if (KK = = "file_id") {//here VV is a []interface{} json. Number, do not know how to take out the value, here with a more silly sprintf vv: = FMT. Sprintf ("%s", vv) imgid,_: = StrConv. Atoi (vv) URL: = fmt. Sprintf ("Yun_qi_img/%d,1920,1200.jpg", Imgid) fmt.
            Println (KK, imgid, URL);
          Paper: = &wallpaper{imgid, URL, 1920, 1200} saveimage (paper); }}}} fmt.
 Println ("Oh yes, all job done.")}

The above is the entire content of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.