示例#1
0
文件: wowhead.go 项目: kdar/morphgen
func wowhead(options map[string]interface{}) (TMorphItems, error) {
	url := to.String(options["url"])

	// if they just put a wowhead item url in, just output that item
	if matches := wowheadUrlRe.FindStringSubmatch(url); len(matches) > 0 {
		items, err := wowapi([]string{matches[1]})
		if err != nil {
			return nil, errors.New(merry.Details(err))
		}

		if len(items) > 0 {
			return items, nil
		}
	}

	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	resp.Body.Close()

	if matches := itemIdRe.FindStringSubmatch(string(data)); len(matches) == 3 {
		//return nil, errors.New(fmt.Sprintf("%#+v", matches))
		count, _ := strconv.Atoi(matches[2])
		if count == 1 {
			items, err := wowapi([]string{matches[1]})
			if err != nil {
				return nil, errors.New(merry.Details(err))
			}

			if len(items) > 0 {
				return items, nil
			}
		} else if count > 1 {
			itemids := strings.Split(matches[1][1:len(matches[1])-1], ":")
			items, err := wowapi(itemids)
			if err != nil {
				return nil, errors.New(merry.Details(err))
			}

			if len(items) > 0 {
				return items, nil
			}
		}
	}

	return nil, errors.New(`Could not find anything to morph on that wowhead page.`)
}
示例#2
0
func TestWowarmory(t *testing.T) {
	items, err := wowarmory(map[string]interface{}{
		"url":    "http://us.battle.net/wow/en/character/tichondrius/Nahj/simple",
		"apikey": "g6pnns5wzvy9zqb7vtduvqjente6yqrx",
	})
	if err != nil {
		t.Fatal(merry.Details(err))
	}
	if len(items) == 0 {
		t.Fatal("could not retrieve wowarmory items")
	}
}