Example #1
0
func main() {
	page_count := 1
	download_count := 0

	for {
		offset := page_count * 50
		param := map[string]string{
			"AppId":        appid,
			"Version":      "2.2",
			"Market":       "ja-JP",
			"Sources":      "Image",
			"Image.Count":  strconv.Itoa(50),
			"Image.Offset": strconv.Itoa(offset),
			"Adult":        "off",
			"Query":        "おっぱい",
		}

		var sr *json_root
		res, err := http.Get(get_request_uri(param))
		if err != nil {
			break
		}
		reader := bufio.NewReader(res.Body)
		line, err := reader.ReadBytes('\n')
		if err == nil {
			break
		}
		json.Unmarshal(line, &sr)

		for i := 0; i < len(sr.SearchResponse.Image.Results); i++ {
			result := sr.SearchResponse.Image.Results[i]
			if regexp.MustCompile(".jpg$").FindString(result.MediaUrl) == "" {
				continue
			}
			download_count++

			filename := md5hex(encode_utf8(result.MediaUrl)) + ".jpg"
			filepath := dir + filename

			if _, err := os.Stat(filepath); err == nil {
				continue
			}
			fmt.Printf("%d : Download... %s\n", download_count, result.MediaUrl)

			res, err := http.Get(result.MediaUrl)
			if err != nil {
				runtime.Goexit()
			}
			data, err := ioutil.ReadAll(res.Body)
			if err != nil {
				runtime.Goexit()
			}
			if regexp.MustCompile("^image").FindString(http.DetectContentType(data)) != "" {
				ioutil.WriteFile(filepath, data, 0666)
			}
		}
		page_count++
	}
}
func getMediaType(media io.Reader) (io.Reader, string) {
	if typer, ok := media.(ContentTyper); ok {
		return media, typer.ContentType()
	}

	typ := "application/octet-stream"
	buf := make([]byte, 1024)
	n, err := media.Read(buf)
	buf = buf[:n]
	if err == nil {
		typ = http.DetectContentType(buf)
	}
	return io.MultiReader(bytes.NewBuffer(buf), media), typ
}