Пример #1
1
/*
   github.com/buger/jsonparser
*/
func BenchmarkJsonParserLarge(b *testing.B) {
	for i := 0; i < b.N; i++ {
		jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
			jsonparser.Get(value, "username")
			nothing()
		}, "users")

		jsonparser.ArrayEach(largeFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
			jsonparser.GetInt(value, "id")
			jsonparser.Get(value, "slug")
			nothing()
		}, "topics", "topics")
	}
}
Пример #2
0
/*
   github.com/buger/jsonparser
*/
func BenchmarkJsonParserSmall(b *testing.B) {
	for i := 0; i < b.N; i++ {
		jsonparser.Get(smallFixture, "uuid")
		jsonparser.GetInt(smallFixture, "tz")
		jsonparser.Get(smallFixture, "ua")
		jsonparser.GetInt(smallFixture, "st")

		nothing()
	}
}
Пример #3
0
func (m *Message) Load() error {
	var err error

	m.File, err = os.Open(m.Filename)
	if err != nil {
		return errors.Wrap(err, "opening file")
	}

	m.Message, err = mail.ReadMessage(m.File)
	if err != nil {
		return errors.Wrap(err, "parsing message")
	}

	if m.ReturnPath == nil {
		err = m.SetReturnPath(m.Message.Header.Get("Return-Path"))
		if err != nil {
			return errors.Wrap(err, "setting return path")
		}
	}

	m.MSFBL = strings.Replace(m.Message.Header.Get("X-MSFBL"), " ", "", -1)

	if m.MSFBL == "" {
		// early return if there isn't a MSFBL header
		return nil
	}

	if strings.Index(m.MSFBL, "|") >= 0 {
		// Everything before the pipe is an encoded HMAC
		// TODO: verify contents using HMAC
		m.MSFBL = strings.Split(m.MSFBL, "|")[1]
	}

	m.Json, err = base64.StdEncoding.DecodeString(m.MSFBL)
	if err != nil {
		return errors.Wrap(err, "decoding fbl")
	}

	var cid []byte
	cid, _, _, err = jsonparser.Get(m.Json, "customer_id")
	if err != nil {
		return errors.Wrap(err, "getting customer_id")
	}
	m.CustID, err = strconv.Atoi(string(cid))
	if err != nil {
		return errors.Wrap(err, "int-ifying customer_id")
	}

	m.Recipient, _, _, err = jsonparser.Get(m.Json, "r")
	if err != nil {
		return errors.Wrap(err, "getting recipient")
	}

	return nil
}
Пример #4
0
/*
   github.com/buger/jsonparser
*/
func BenchmarkJsonParserMedium(b *testing.B) {
	for i := 0; i < b.N; i++ {
		jsonparser.Get(mediumFixture, "person", "name", "fullName")
		jsonparser.GetInt(mediumFixture, "person", "github", "followers")
		jsonparser.Get(mediumFixture, "company")

		jsonparser.ArrayEach(mediumFixture, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
			jsonparser.Get(value, "url")
			nothing()
		}, "person", "gravatar", "avatars")
	}
}
Пример #5
0
Файл: main.go Проект: qband/down
func main() {
	// read configuration file
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	check(err)
	config, err := ioutil.ReadFile(path.Join(dir, "config.json"))
	check(err)
	config, _, _, _ = jsonparser.Get(config, "downloader")
	fmt.Println(string(config))
}
Пример #6
0
func facebookWorker(u utils.Utils, req *request) {
	start := time.Now()
	urls := strings.Join(req.Urls, ",")
	url := prepareFbGraphUrl(u, urls)
	utils.Verbosef(u, "Calling http.Client.Get(%s)", url)

	resp, err := u.HttpClient().Get(url)
	if err != nil {
		req.Error = err
		return
	}

	defer resp.Body.Close()
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		req.Error = err
		return
	}
	req.Response = respBody
	u.Debugf("facebookWorker(urls=%s) took %s", urls, time.Since(start))

	for _, url := range req.Urls {
		var res result

		if respUrl, _, _, err := jsonparser.Get(respBody, url); err != nil {
			res.Error = err
			res.Response = respBody
		} else {
			res.Response = respUrl
			if shareCount, err := jsonparser.GetInt(respUrl, "share", "share_count"); err != nil {
				// it's alright, for new urls Facebook does not return share.share_count at all
				res.Count = COUNT_INITIAL_VALUE
			} else {
				res.Count = shareCount
			}
		}

		req.Results[url] = res
	}

	return
}