DMM에서 API를 모방한 Go의 구현 샘플에 액세스

2298 단어 Gotech

판자 상품 정보 취득 및 표시 제이슨


package main

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

type responsStatus struct {
	Result struct {
		Status        int `json:"status"`
		ResultCount   int `json:"result_count"`
		TotalCount    int `json:"total_count"`
		FirstPosition int `json:"first_position"`
	} `json:"result"`
}

type targetService struct {
	service string
	floor   string
}

var targetServices = []targetService{
	{
		service: "digital",
		floor:   "videoa",
	},
	{
		service: "digital",
		floor:   "videoc",
	},
	{
		service: "digital",
		floor:   "anime",
	},
	{
		service: "doujin",
		floor:   "digital_doujin",
	},
	{
		service: "ebook",
		floor:   "comic",
	},
}

func main() {

	url := "https://api.dmm.com/affiliate/v3/ItemList?api_id=%s&affiliate_id=%s&site=FANZA&service=%s&floor=%s&hits=%d&offset=%d&gte_date=%s&lte_date=%s&output=json"
	const hits = 100

	hc := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	}

	today := time.Now().Format("2006-01-02")
	for _, ts := range targetServices {
		offset := 1
		for i := 1; ; i++ {

			var result responsStatus
			var resp *http.Response
			var body []byte
			for retry := 0; retry < 3; retry++ {

				gteDate := today + "T00:00:00"
				lteDate := today + "T23:59:59"
				requestURL := fmt.Sprintf(url, "dmm_api_id", "dmm_affiliate_id", ts.service, ts.floor, hits, offset, gteDate, lteDate)
				fmt.Println(requestURL)

				req, _ := http.NewRequest("GET", requestURL, nil)
				req.Header.Set("Content-Type", "application/json")

				resp, _ = hc.Do(req)
				body, _ = ioutil.ReadAll(resp.Body)
				json.Unmarshal(body, &result)

				if result.Result.Status != http.StatusOK {
					resp.Body.Close()
					continue
				}

				fmt.Println(result)
				break
			}

			resp.Body.Close()

			if result.Result.TotalCount > hits*i {
				offset += hits
				continue
			}
			break
		}
	}
}

좋은 웹페이지 즐겨찾기