Beispiel #1
0
// FillStockData 抓取数据
func FillPingGuData() {
	// http://stockData.stock.hexun.com/600028.shtml
	var stockID int64
	var pinggu *PingGuStruct
	var err error

	ist := db.NewInsertBuilder("pinggu")
	exists := db.NewExistsBuilder("pinggu")

	dataset, _ := db.NewQueryBuilder("stock").Select("id,code,ctype").Limit(0).Query()

	for _, data := range dataset {
		if exists.Where("stock_id=? and date=?", data.GetInt64("id"), util.StockQuarter()).Exists() {
			continue
		}
		pinggu, err = FetchPingGuData(data.GetString("code"))
		if err != nil {
			gos.DoError(err)
			continue
		}
		stockID = data.GetInt64("id")
		for _, row := range pinggu.Content {
			row.StockID = stockID
			ist.Insert(row)
		}
		fmt.Println("insert ", data.GetString("code"))
		time.Sleep(1 * time.Second)
	}
}
Beispiel #2
0
func (a *PublicApi) IsExists(args util.MapData) (int, error) {
	table := "users"
	field := args.GetString("field")
	val := args.GetString("value")

	exists := db.NewExistsBuilder(table).Where(field+"=?", val)

	if isEx := exists.Exists(); isEx {
		return 1, nil
	} else {
		return 0, nil
	}
}
Beispiel #3
0
func main() {
	c := conf.Load("../../app/app.conf")
	db.Init("app", c.GetConf("db").MapData())
	db.Current().Log = log.NewEmpty()

	lastDate := "2015-09-30"
	ist := db.NewInsertBuilder("fin_dubang")
	exist := db.NewExistsBuilder("fin_dubang")
	dataset, _ := db.NewQueryBuilder("stock").Select("id,code,ctype").Limit(0).Query()
	isIst := false

	for i, data := range dataset {
		fmt.Println("--", i, "--", data.GetString("code"))
		if exist.Where("stock_id=? and date=?", data.GetInt64("id"), lastDate).Exists() {
			fmt.Println("exists", lastDate, "return")
			continue
		}

		dubangList, err := dfcf.FetchDubang(data.GetString("code"), data.GetString("ctype"))
		if err != nil {
			fmt.Println(err)
			continue
		}

		if len(dubangList) == 0 {
			continue
		}

		isIst = false
		for _, dubang := range dubangList {
			dubang.StockId = data.GetInt64("id")

			if exist.Where("stock_id=? and date=?", dubang.StockId, dubang.Date).Exists() {
				fmt.Println("exists", dubang.Date, "return")
				continue
			}

			fmt.Println("insert", dubang.Date)
			ist.Insert(dubang)
			isIst = true
		}

		if isIst {
			time.Sleep(1 * time.Second)
		}
	}

}
Beispiel #4
0
func fetchAndFillFundRank(page int, quarter string) (int, error) {
	// http://stockdata.stock.hexun.com/jgcc/data/outdata/orgrank.ashx?count=50&date=2015-09-30&orgType=&stateType=null&titType=null&page=2&callback=hxbase_json7
	formt := "http://stockdata.stock.hexun.com/jgcc/data/outdata/orgrank.ashx?count=%s&date=%s&orgType=&stateType=null&titType=null&page=%d&callback=hxbase_json7"
	pageLimit := 100
	body, err := wget.GetBody(fmt.Sprintf(formt, pageLimit, quarter, page))
	if err != nil {
		return -1, err
	}

	exists := db.NewExistsBuilder("funds")
	ist := db.NewInsertBuilder("funds")
	query := db.NewQueryBuilder("funds")

	var fund *Fund
	var fundRank *FundRank
	var row db.DataRow

	// hxbase_json7(
	str, err := iconv.ConvertString(string(body), "gb2312", "utf-8")
	if err != nil {
		return -1, err
	}

	src := []byte(str)
	src = src[13 : len(src)-1]
	src = bytes.Replace(src, []byte(":'"), []byte(`":"`), -1)
	src = bytes.Replace(src, []byte("',"), []byte(`","`), -1)
	src = bytes.Replace(src, []byte("'}"), []byte(`"}`), -1)
	src = bytes.Replace(src, []byte("{"), []byte(`{"`), -1)
	src = bytes.Replace(src, []byte("sum:"), []byte(`sum":`), 1)
	src = bytes.Replace(src, []byte("list:"), []byte(`"list":`), 1)

	v := &JsonFund{}
	err = json.Unmarshal(src, v)
	if err != nil {
		fmt.Println(string(src))
		return -1, err
	}
	// {RankTd:'51',OrgName:'法国巴黎银行',OrgNameLink:'o-QF000031.shtml',OrgType:'QFII',ShareHoldingNum:'3',ShareHoldingNumLink:'otherDetail.aspx?OrgNo=QF000031',TotalHoldings:'48,388.00',TotalMarketValue:'1,100,735.00',OrgAlt:'法国巴黎银行'}
	for _, item := range v.List {
		fund = &Fund{Code: item["OrgNameLink"], Name: string(item["OrgName"]), TypeID: FundType(item["OrgType"])}
		fund.Code = fund.Code[2 : len(fund.Code)-6]
		fmt.Println("----")
		if !exists.Table("funds").Where("code=? and type_id=?", fund.Code, fund.TypeID).Exists() {
			fmt.Println("insert", fund)
			ist.Table("funds").Insert(fund)
		}

		row, _ = query.Table("funds").Where("code=? and type_id=?", fund.Code, fund.TypeID).QueryOne()
		if row.Empty() {
			return -1, fmt.Errorf("code %s not found", fund.Code)
		}

		fundRank = &FundRank{
			FundID: row.GetInt64("id"),
			Date:   quarter,
			Rank:   int(util.ParseMoney(item["RankTd"])),
			Count:  int(util.ParseMoney(item["ShareHoldingNum"])),
			MH:     int64(util.ParseMoney(item["TotalHoldings"])),
			MV:     int64(util.ParseMoney(item["TotalMarketValue"])),
		}

		if !exists.Table("fund_rank").Where("fund_id=? and date=?", fundRank.FundID, fundRank.Date).Exists() {
			fmt.Println("insert", fundRank)
			ist.Table("fund_rank").Insert(fundRank)
		}

	}
	return int(math.Ceil(float64(v.Sum / pageLimit))), nil
}
Beispiel #5
0
// fetchAndFillYyb 抓取数据
func fetchAndFillYyb(page int) (int, error) {
	// http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=2&ps=50&js=var%20XvAVhGPE={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}&rt=48257541
	formt := `http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=%d&ps=%d&js=var%%20XvAVhGPE={"data":[(x)],"pages":"(pc)","update":"(ud)"}&rt=%d`
	pageLimit := 100

	body, err := wget.GetBody(fmt.Sprintf(formt, page, pageLimit, time.Now().Unix()))
	if err != nil {
		return -1, err
	}

	exists := db.NewExistsBuilder("yyb")
	ist := db.NewInsertBuilder("yyb")
	upd := db.NewUpdateBuilder("yyb")
	query := db.NewQueryBuilder("yyb")
	var yyb *yybStruct
	var yybLub *yybLhbStruct
	var arr []string
	var row db.DataRow
	src := body[bytes.Index(body, []byte("={"))+1:]

	v := &jsonYybData{}
	err = json.Unmarshal(src, v)
	if err != nil {
		return -1, err
	}
	// "80136686,2,216,135,七喜控股|协鑫集成|恒信移动,2991067145.735,广东,80000073,440000,105,6089657316.805,3098590171.07,华泰证券股份有限公司广州天河东路证券营业部,002027.SZ|002506.SZ|300081.SZ"
	for i, item := range v.Data {
		arr = strings.Split(item, ",")
		yyb = &yybStruct{Code: arr[0], Name: arr[12], Area: arr[6]}

		if !exists.Table("yyb").Where("code=?", yyb.Code).Exists() {
			gos.Log.Info("insert", yyb.Code)
			ist.Table("yyb").Insert(yyb)
		}

		row, _ = query.Table("yyb").Where("code=?", yyb.Code).QueryOne()
		if row.Empty() {
			return -1, fmt.Errorf("code %s not found", yyb.Code)
		}

		yybLub = &yybLhbStruct{
			YybID:   row.GetInt64("id"),
			Updated: v.Update,
			Rank:    pageLimit*(page-1) + i + 1,
			Amount:  util.ParseMoney(arr[10]),
			Buy:     util.ParseMoney(arr[11]),
			Sell:    util.ParseMoney(arr[5]),
			Num:     int(util.ParseMoney(arr[2])),
			NumBuy:  int(util.ParseMoney(arr[3])),
			NumSell: int(util.ParseMoney(arr[9])),
		}

		if exists.Table("yyb_lhb").Where("yyb_id=?", yybLub.YybID).Exists() {
			gos.Log.Info("update", yyb.Code, yybLub.YybID)
			upd.Table("yyb_lhb").Where("yyb_id=?", yybLub.YybID).Update(yybLub)
		} else {
			gos.Log.Info("insert", yyb.Code, yybLub.YybID)
			ist.Table("yyb_lhb").Insert(yybLub)
		}

	}
	return int(util.ParseMoney(v.Pages)), nil
}
Beispiel #6
0
// FillCwfx 抓取并且填充数据
func FillCwfx(lastDate string) {
	ist := db.NewInsertBuilder("fin_cwzb")
	exist := db.NewExistsBuilder("fin_cwzb")
	dataset, _ := db.NewQueryBuilder("stock").Select("id,code,ctype").Limit(0).Query()

	for i, data := range dataset {
		fmt.Println("--", i, "--", data.GetString("code"), data.GetString("ctype"))
		if exist.Where("stock_id=? and date=?", data.GetInt64("id"), lastDate).Exists() {
			gos.Log.Info("exists", lastDate, "return")
			continue
		}

		cwzbList, zcfzbList, lrbList, xjllbList, finPercentList, err := FetchCwfx(data.GetString("code"), data.GetString("ctype"))
		if err != nil {
			gos.DoError(err)
			continue
		}

		if len(cwzbList) == 0 {
			continue
		}

		for _, row := range cwzbList {
			row.StockID = data.GetInt64("id")

			if exist.Table("fin_cwzb").Where("stock_id=? and date=?", row.StockID, row.Date).Exists() {
				gos.Log.Info("exists fin_cwzb", row.Date, "return")
				continue
			}

			gos.Log.Info("insert fin_cwzb", row.Date)
			ist.Table("fin_cwzb").Insert(row)
		}
		for _, row := range zcfzbList {
			row.StockID = data.GetInt64("id")

			if exist.Table("fin_zcfzb").Where("stock_id=? and date=?", row.StockID, row.Date).Exists() {
				gos.Log.Info("exists fin_zcfzb", row.Date, "return")
				continue
			}

			gos.Log.Info("insert fin_zcfzb", row.Date)
			ist.Table("fin_zcfzb").Insert(row)
		}
		for _, row := range lrbList {
			row.StockID = data.GetInt64("id")

			if exist.Table("fin_lrb").Where("stock_id=? and date=?", row.StockID, row.Date).Exists() {
				fmt.Println("exists fin_lrb", row.Date, "return")
				continue
			}

			gos.Log.Info("insert fin_lrb", row.Date)
			ist.Table("fin_lrb").Insert(row)
		}
		for _, row := range xjllbList {
			row.StockID = data.GetInt64("id")

			if exist.Table("fin_xjllb").Where("stock_id=? and date=?", row.StockID, row.Date).Exists() {
				gos.Log.Info("exists fin_xjllb", row.Date, "return")
				continue
			}

			gos.Log.Info("insert fin_xjllb", row.Date)
			ist.Table("fin_xjllb").Insert(row)
		}
		for _, row := range finPercentList {
			row.StockID = data.GetInt64("id")

			if exist.Table("fin_percent").Where("stock_id=? and date=?", row.StockID, row.Date).Exists() {
				gos.Log.Info("exists fin_percent", row.Date, "return")
				continue
			}

			gos.Log.Info("insert fin_percent", row.Date)
			ist.Table("fin_percent").Insert(row)
		}

		if len(cwzbList) > 0 {
			time.Sleep(1 * time.Second)
		}
	}
}