github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/klauspost/cpuid/testdata/getall.go (about)

     1  package main
     2  
     3  import (
     4  	"archive/zip"
     5  	_ "bytes"
     6  	"fmt"
     7  	"github.com/insionng/yougam/libraries/x/net/html"
     8  	"io"
     9  	"net/http"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  // Download all CPUID dumps from http://users.atw.hu/instlatx64/
    15  func main() {
    16  	resp, err := http.Get("http://users.atw.hu/instlatx64/?")
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	node, err := html.Parse(resp.Body)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	file, err := os.Create("cpuid_data.zip")
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  	defer file.Close()
    31  	gw := zip.NewWriter(file)
    32  
    33  	var f func(*html.Node)
    34  	f = func(n *html.Node) {
    35  		if n.Type == html.ElementNode && n.Data == "a" {
    36  			for _, a := range n.Attr {
    37  				if a.Key == "href" {
    38  					err := ParseURL(a.Val, gw)
    39  					if err != nil {
    40  						panic(err)
    41  					}
    42  					break
    43  				}
    44  			}
    45  		}
    46  		for c := n.FirstChild; c != nil; c = c.NextSibling {
    47  			f(c)
    48  		}
    49  	}
    50  
    51  	f(node)
    52  	err = gw.Close()
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  }
    57  
    58  func ParseURL(s string, gw *zip.Writer) error {
    59  	if strings.Contains(s, "CPUID.txt") {
    60  		fmt.Println("Adding", "http://users.atw.hu/instlatx64/"+s)
    61  		resp, err := http.Get("http://users.atw.hu/instlatx64/" + s)
    62  		if err != nil {
    63  			fmt.Println("Error getting ", s, ":", err)
    64  		}
    65  		defer resp.Body.Close()
    66  		w, err := gw.Create(s)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		_, err = io.Copy(w, resp.Body)
    72  		if err != nil {
    73  			return err
    74  		}
    75  	}
    76  	return nil
    77  }