Example #1
0
func parseOrderListPage(s *goquery.Document) ([]Order, bool, error) {
	c := s.Find(".container").First()
	t := c.Find("div").First().Text()
	if t != ">注文情報(一覧)<" && t != ">注文情報(検索)<" {
		return nil, false, fmt.Errorf("cannot open \"注文情報(一覧)\", but %#v", t)
	}
	// タイトル行の削除
	c.Find("hr").First().Next().PrevAll().Remove()

	results := []Order{}
	c.Find("a").Each(
		func(_ int, s *goquery.Selection) {
			href, ok := s.Attr("href")
			if !ok || !strings.HasPrefix(href, "../otc/C003.html?") {
				return
			}
			u, err := url.Parse(href)
			if err != nil || u.RawQuery == "" {
				return
			}
			v, err := url.ParseQuery(u.RawQuery)
			results = append(results, Order{
				OrderId:     v.Get("order_id"),
				OrderMethod: v.Get("order_method"),
			})
		})

	return results, c.Find("a[accesskey=\"#\"]").Length() == 1, nil
}
Example #2
0
func parsePositionListPage(s *goquery.Document) ([]Position, bool, error) {
	c := s.Find(".container").First()
	t := c.Find("div").First().Text()
	if t != ">ポジション情報(一覧)<" && t != ">ポジション情報(検索)<" {
		return nil, false, fmt.Errorf("cannot open \"ポジション情報(一覧)\", but %#v", t)
	}

	results := []Position{}
	c.Find("a").Each(
		func(_ int, s *goquery.Selection) {
			href, ok := s.Attr("href")
			if !ok || !strings.HasPrefix(href, "C304.html") {
				return
			}
			u, err := url.Parse(href)
			if err != nil || u.RawQuery == "" {
				return
			}
			v, err := url.ParseQuery(u.RawQuery)
			results = append(results, Position{
				PositionId: v.Get("position_id"),
			})
		})

	return results, c.Find("a[accesskey=\"#\"]").Length() == 1, nil
}
Example #3
0
// GetMetaLanguage returns the meta language set in the source, if the article has one
func (extr *ContentExtractor) GetMetaLanguage(document *goquery.Document) string {
	language := ""
	shtml := document.Find("html")
	attr, _ := shtml.Attr("lang")
	if attr == "" {
		attr, _ = document.Attr("lang")
	}
	if attr == "" {
		selection := document.Find("meta").EachWithBreak(func(i int, s *goquery.Selection) bool {
			exists := false
			attr, exists = s.Attr("http-equiv")
			if exists && attr == "content-language" {
				return false
			}
			return true
		})
		if selection != nil {
			attr, _ = selection.Attr("content")
		}
	}
	idx := strings.LastIndex(attr, "-")
	if idx == -1 {
		language = attr
	} else {
		language = attr[0:idx]
	}

	_, ok := sw[language]

	if language == "" || !ok {
		language = extr.config.stopWords.SimpleLanguageDetector(shtml.Text())
		if language == "" {
			language = defaultLanguage
		}
	}

	extr.config.targetLanguage = language
	return language
}
Example #4
0
func parseOrderDetailsPage(s *goquery.Document) (*Order, error) {
	c := s.Find(".container").First()
	t := c.Find("div").First().Text()
	if t != ">注文情報(詳細)<" {
		return nil, fmt.Errorf("cannot open \"注文情報(詳細)\", but %#v", t)
	}

	o := &Order{}
	// タイトル行の削除
	c.Find("hr").First().Next().PrevAll().Remove()

	c.Find("a").Each(func(_ int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		// 取消リンク
		if strings.HasPrefix(href, "../otc/CT01.html?") {
			u, err := url.Parse(href)
			if err != nil || u.RawQuery == "" {
				return
			}
			v, err := url.ParseQuery(u.RawQuery)
			if err != nil {
				return
			}
			o.OrderId = v.Get("order_id")
			o.OrderMethod = v.Get("order_method")
		}
		// 指定決済リンク
		if strings.HasPrefix(href, "../common/I124.html?") {
			u, err := url.Parse(href)
			if err != nil || u.RawQuery == "" {
				return
			}
			v, err := url.ParseQuery(u.RawQuery)
			if err != nil {
				return
			}
			o.PositionId = v.Get("position_id")
		}
	})

	// メニューの削除
	c.Find("hr").First().Prev().NextAll().Remove()

	results := []string{}
	c.Find("div").Each(func(_ int, s *goquery.Selection) {
		results = append(results, s.Text())
	})

	orig := map[string]string{}
	for i := 0; i+1 < len(results); i += 2 {
		k := strings.TrimSpace(strings.Replace(results[i], ":", "", -1))
		v := strings.TrimSpace(results[i+1])
		orig[k] = v
	}

	o.Currency = orig["通貨ペア"]
	lot, err := strconv.ParseInt(orig["注文Lot数"], 10, 64)
	if err != nil {
		return nil, err
	}
	o.Amount = lot * 1000
	if orig["売買"] == "売" {
		o.Side = "sell"
	} else if orig["売買"] == "買" {
		o.Side = "buy"
	} else {
		return nil, fmt.Errorf("invalid value for 売買: %#v", orig["売買"])
	}
	if orig["執行条件"] == "指値" {
		o.IsStop = false
	} else if orig["執行条件"] == "逆指" {
		o.IsStop = true
	} else {
		return nil, fmt.Errorf("invalid value for 執行条件: %#v", orig["執行条件"])
	}
	o.Price, err = strconv.ParseFloat(orig["指定レート"], 64)
	if err != nil {
		return nil, err
	}
	if orig["注文区分"] == "指定決済" {
		o.IsSettlement = true
	} else if orig["注文区分"] == "売買" {
		o.IsSettlement = false
	} else {
		return nil, fmt.Errorf("invalid value for 注文区分: %#v", orig["注文区分"])
	}

	return o, nil
}