当前位置: 首页>>代码示例>>Golang>>正文


Golang database.MongoDB类代码示例

本文整理汇总了Golang中github.com/databr/api/database.MongoDB的典型用法代码示例。如果您正苦于以下问题:Golang MongoDB类的具体用法?Golang MongoDB怎么用?Golang MongoDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了MongoDB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: LineColor

func LineColor(uri, hex string, db database.MongoDB) {
	q := bson.M{"id": uri}

	c, _ := colorful.Hex(hex)
	r, g, b := c.RGB255()
	color := bson.M{
		"hex": hex,
		"rgb": []int{int(r), int(g), int(b)},
	}

	parser.Log.Debug("Save", uri, "with color", color)

	_, err := db.Upsert(q, bson.M{
		"$setOnInsert": bson.M{
			"createdat": time.Now(),
		},
		"$currentDate": bson.M{
			"updatedat": true,
		},
		"$set": bson.M{
			"color": color,
		},
	}, models.Line{})
	parser.CheckError(err)
}
开发者ID:databr,项目名称:metrosp-bot,代码行数:25,代码来源:line_bot.go

示例2: getData

func getData(g string, db database.MongoDB) {
	url := "http://www.apolo11.com/reservatorios.php?step=" + g
	doc, err := goquery.NewDocument(url)
	parser.CheckError(err)
	doc.Find("body > center:nth-child(1) > table > tbody > tr > td:nth-child(1) > b > table").Each(func(_ int, s *goquery.Selection) {
		trs := s.Find("tr")
		title := "Sistema " + parser.Titlelize(strings.Replace(trs.Eq(0).Text(), "SISTEMA", "", -1))
		uri := models.MakeUri(title)

		getInfo := func(i int, ss *goquery.Selection) string {
			return ss.Text()
		}

		percent := trs.Eq(1).Find("font").Map(getInfo)
		date := trs.Eq(2).Find("font").Map(getInfo)

		data := make([]bson.M, 0)
		for i, _ := range percent {
			if strings.TrimSpace(date[i]) != "" && strings.TrimSpace(date[i]) != "/" {
				data = append(data, bson.M{"percent": percent[i], "date": date[i]})
			}
		}

		query := bson.M{"uri": uri, "granularity_letter": g}

		source := models.Source{
			Url:  "http://www.apolo11.com",
			Note: "Apolo11",
		}

		_, err := db.Upsert(query, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"uri":                uri,
				"name":               title,
				"granularity_letter": g,
				"granularity":        getGranularity(g),
				"data":               data,
				"source":             []models.Source{source},
			},
		}, models.Reservoir{})
		parser.CheckError(err)

		log.Println(uri, title, data)
	})

}
开发者ID:databr,项目名称:sabesp-bot,代码行数:52,代码来源:sabesp_bot.go

示例3: Run

func (p SaveDeputiesFromTransparenciaBrasil) Run(DB database.MongoDB) {
	source := models.Source{
		Url:  "http://dev.transparencia.org.br/",
		Note: "Transparencia Brasil",
	}

	if parser.IsCached("http://dev.transparencia.org.br/") {
		parser.Log.Info("SaveDeputiesFromTransparenciaBrasil Cached")
		return
	}
	defer parser.DeferedCache("http://dev.transparencia.org.br/")

	parser.Log.Info("Starting SaveDeputiesFromTransparenciaBrasil")

	c := transparencia.New("kqOfbdNKSlpf")
	query := map[string]string{
		"casa": "1",
	}
	parliamenrians, err := c.Excelencias(query)
	parser.CheckError(err)

	for _, parliamenrian := range parliamenrians {
		uri := models.MakeUri(parliamenrian.Apelido)
		parser.Log.Info("Saving %s", parliamenrian.Nome)

		_, err := DB.Upsert(bson.M{"id": uri}, bson.M{
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"summary":          parliamenrian.MiniBio,
				"nationalidentify": parliamenrian.CPF,
			},
			"$addToSet": bson.M{
				"sources": source,
				"identifiers": bson.M{
					"$each": []bson.M{
						{
							"identifier": parliamenrian.Id,
							"scheme":     "TransparenciaBrasilID",
						},
						{
							"identifier": parliamenrian.CPF,
							"scheme":     "CPF",
						},
					},
				},
			},
		}, models.Parliamentarian{})
		parser.CheckError(err)
	}
}
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:52,代码来源:deputies_from_transparencia_brasil.go

示例4: Run

func (p SaveDeputiesFromSearch) Run(DB database.MongoDB) {
	searchURL := "http://www2.camara.leg.br/deputados/pesquisa"

	if parser.IsCached(searchURL) {
		parser.Log.Info("SaveDeputiesFromSearch Cached")
		return
	}
	defer parser.DeferedCache(searchURL)

	var doc *goquery.Document
	var e error

	if doc, e = goquery.NewDocument(searchURL); e != nil {
		parser.Log.Critical(e.Error())
	}

	source := models.Source{
		Url:  searchURL,
		Note: "Pesquisa Câmara",
	}

	doc.Find("#deputado option").Each(func(i int, s *goquery.Selection) {
		value, _ := s.Attr("value")
		if value != "" {
			info := regexp.MustCompile("=|%23|!|\\||\\?").Split(value, -1)

			name := parser.Titlelize(info[0])
			q := bson.M{
				"id": models.MakeUri(name),
			}

			_, err := DB.Upsert(q, bson.M{
				"$setOnInsert": bson.M{
					"createdat": time.Now(),
				},
				"$currentDate": bson.M{
					"updatedat": true,
				},
				"$addToSet": bson.M{
					"sources": source,
					"identifiers": models.Identifier{
						Identifier: info[2], Scheme: "nMatricula",
					},
				},
			}, models.Parliamentarian{})
			parser.CheckError(err)
		}
	})
}
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:49,代码来源:deputies_from_search.go

示例5: ParseState

func (self BasicStateBot) ParseState(db database.MongoDB, stateUrl string) {
	doc, err := goquery.NewDocument(stateUrl)
	parser.CheckError(err)
	source := models.Source{
		Url:  stateUrl,
		Note: "ibge",
	}

	data := doc.Find("#sintese tr")

	pUrl, _ := url.Parse(stateUrl)

	id := pUrl.Query().Get("sigla")
	capital := parser.ToUtf8(data.Eq(0).Find(".total").Text())
	population2014 := data.Eq(1).Find(".total").Text()
	population2010 := data.Eq(2).Find(".total").Text()
	area := data.Eq(3).Find(".total").Text()
	populationDensity := data.Eq(4).Find(".total").Text()
	numberOfMunicipalities, _ := strconv.Atoi(data.Eq(5).Find(".total").Text())

	log.Println(id, capital, population2014, population2010, area, populationDensity, numberOfMunicipalities)

	if STATES_NAME[id] == "" {
		panic(id)
	}
	q := bson.M{"id": id}
	_, err = db.Upsert(q, bson.M{
		"$setOnInsert": bson.M{
			"createdat": time.Now(),
		},
		"$currentDate": bson.M{
			"updatedat": true,
		},
		"$set": bson.M{
			"name":                   STATES_NAME[id],
			"capitalid":              models.MakeUri(capital),
			"population":             toFloat(population2014),
			"area":                   toFloat(area),
			"populationdensity":      toFloat(populationDensity),
			"numberofmunicipalities": numberOfMunicipalities,
		},
		"$addToSet": bson.M{
			"sources": source,
		},
	}, models.State{})
	doc = nil
	parser.CheckError(err)
}
开发者ID:databr,项目名称:ibge-bot,代码行数:48,代码来源:basic_state_bot.go

示例6: getCitiesData

func (self BasicCityBot) getCitiesData(db database.MongoDB, url string, stateID string) {
	doc, err := goquery.NewDocument(url)
	parser.CheckError(err)
	source := models.Source{
		Url:  url,
		Note: "ibge",
	}

	doc.Find("#municipios tbody tr").Each(func(_ int, s *goquery.Selection) {
		data := s.Find("td")

		name := data.Eq(0).Text()
		parser.Log.Debug("Salving: " + name + " (" + stateID + ")")
		id := models.MakeUri(name)

		ibgecode, _ := strconv.Atoi(data.Eq(1).Text())

		q := bson.M{"id": id, "stateid": stateID}
		_, err = db.Upsert(q, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"name":       name,
				"ibgecode":   ibgecode,
				"gentile":    data.Eq(2).Text(),
				"population": toFloat(data.Eq(3).Text()),
				"area":       toFloat(data.Eq(4).Text()),
				"density":    toFloat(data.Eq(5).Text()),
				"pib":        toFloat(data.Eq(6).Text()),
			},
			"$addToSet": bson.M{
				"sources": source,
			},
		}, models.City{})
		parser.CheckError(err)
	})

	doc = nil
}
开发者ID:databr,项目名称:ibge-bot,代码行数:43,代码来源:basic_city_bot.go

示例7: Run

func (p SaveDeputiesAbout) Run(DB database.MongoDB) {
	var ds []models.Parliamentarian

	DB.FindAll(&ds)

	var wg sync.WaitGroup

	for _, d := range ds {
		id, ok := getIdDeputado(d.Identifiers)
		if !ok {
			continue
		}
		wg.Add(1)
		go func(_id string) {
			defer wg.Done()
			saveDeputies(_id, d, DB)
		}(id)
	}

	wg.Wait()
}
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:21,代码来源:deputies_about.go

示例8: saveStatus

func saveStatus(db database.MongoDB, lineName, status string, source models.Source) {
	uri := models.MakeUri(lineName)
	result := re.FindStringSubmatch(lineName)
	lineNumber, _ := strconv.Atoi(result[0])

	q := bson.M{"id": uri}

	_, err := db.Upsert(q, bson.M{
		"$setOnInsert": bson.M{
			"createdat": time.Now(),
		},
		"$currentDate": bson.M{
			"updatedat": true,
		},
		"$set": bson.M{
			"name":       lineName,
			"linenumber": lineNumber,
		},
		"$addToSet": bson.M{
			"sources": source,
		},
	}, models.Line{})

	log.Println(uri)
	parser.CheckError(err)
	var statusOld models.Status
	err = db.FindOne(bson.M{"line_id": uri}, &statusOld)

	statusQ := bson.M{"line_id": uri, "_id": bson.NewObjectId()}
	if err == nil && statusOld.Status == status {
		statusQ = bson.M{"_id": statusOld.Id, "line_id": uri}
	}

	_, err = db.Upsert(statusQ, bson.M{
		"$setOnInsert": bson.M{
			"createdat": time.Now(),
		},
		"$currentDate": bson.M{
			"updatedat": true,
		},
		"$set": bson.M{
			"status":  status,
			"line_id": uri,
		},
		"$addToSet": bson.M{
			"sources": source,
		},
	}, models.Status{})
	parser.CheckError(err)

	parser.Log.Debug(lineName + " - " + status)
	parser.Log.Info("-- Created Status to " + lineName)
	parser.Log.Info("Status: " + status)
	parser.Log.Info("------")

	if uri == "linha11coral" {
		saveStatus(db, "Linha 11-Coral-Expresso", status, source)
	}
}
开发者ID:databr,项目名称:metrosp-bot,代码行数:59,代码来源:status_bot.go

示例9: pagination

func pagination(resourceURI string,
	database database.MongoDB,
	limit,
	currentPage int,
	resourceClass interface{},
	query bson.M,
) map[string]interface{} {
	total, _ := database.Count(resourceClass, query)

	pagination := map[string]interface{}{}

	if currentPage > 1 {
		pagination["previous"] = fmt.Sprintf("%s/%s/?page=%d", config.ApiRoot, resourceURI, currentPage-1)
	}

	if total > (limit * currentPage) {
		pagination["next"] = fmt.Sprintf("%s/%s/?page=%d", config.ApiRoot, resourceURI, currentPage+1)
	}

	return pagination
}
开发者ID:zannet,项目名称:api,代码行数:21,代码来源:service.go

示例10: saveDeputies

func saveDeputies(id string, d models.Parliamentarian, DB database.MongoDB) {

	bioURL := "http://www2.camara.leg.br/deputados/pesquisa/layouts_deputados_biografia?pk=" + id

	if parser.IsCached(bioURL) {
		parser.Log.Info("SaveDeputiesAbout(%s) Cached", id)
		return
	}

	source := models.Source{
		Url:  bioURL,
		Note: "Pesquisa Câmara",
	}

	var doc *goquery.Document
	var e error

	if doc, e = goquery.NewDocument(bioURL); e != nil {
		parser.Log.Critical(e.Error())
	}

	bio := doc.Find("#bioDeputado .bioOutros")

	biographyItems := make([]string, 0)
	bio.Each(func(i int, s *goquery.Selection) {
		title := s.Find(".bioOutrosTitulo").Text()
		if title != "" {
			title = strings.TrimSpace(title)
			title = strings.Replace(title, ":", "", -1)

			body := s.Find(".bioOutrosTexto").Text()

			biographyItems = append(biographyItems, title)
			biographyItems = append(biographyItems, body)
			biographyItems = append(biographyItems, "")
		}
	})

	bioDetails := doc.Find("#bioDeputado .bioDetalhes strong")

	birthdateA := strings.Split(bioDetails.Eq(1).Text(), "/")

	var year int
	switch id {
	case "123756", "160635":
		year = 1970
	case "74230", "129618":
		year = 1952
	case "74665", "141387":
		year = 1953
	case "73933":
		year = 1959
	case "73786":
		year = 1939
	case "74124":
		year = 1964
	case "74447":
		year = 1936
	case "74474":
		year = 1940
	default:
		parser.Log.Debug("(%s) %s", id, birthdateA)
		if len(birthdateA) != 3 {
			parser.Log.Debug("Error, deputies without year %s", bioURL)
			year = 0
		} else {
			year, _ = strconv.Atoi(birthdateA[2])
		}
	}

	birthDate := popolo.Date{}

	if len(birthdateA) > 1 {
		month, _ := strconv.Atoi(birthdateA[1])
		day, _ := strconv.Atoi(birthdateA[0])
		loc, _ := time.LoadLocation("America/Sao_Paulo")
		birthDate = popolo.Date{time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc)}
	}

	_, err := DB.Upsert(bson.M{"id": d.Id}, bson.M{
		"$setOnInsert": bson.M{
			"createdat": time.Now(),
		},
		"$currentDate": bson.M{
			"updatedat": true,
		},
		"$set": bson.M{
			"summary":   bio.Eq(0).Find(".bioOutrosTexto").Text(),
			"biography": strings.Join(biographyItems, "\n"),
			"link":      "http://www.camara.gov.br/internet/Deputado/dep_Detalhe.asp?id=" + id,
			"birthdate": birthDate,
		},
		"$addToSet": bson.M{
			"sources": source,
		},
	}, models.Parliamentarian{})

	parser.CheckError(err)
	parser.CacheURL(bioURL)
}
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:100,代码来源:deputies_about.go

示例11: Run

func (self SaveSenatorsFromIndex) Run(DB database.MongoDB) {
	indexURL := "http://www.senado.gov.br"

	if parser.IsCached(indexURL) {
		parser.Log.Info("SaveSenatorsFromIndex Cached")
		return
	}
	defer parser.DeferedCache(indexURL)

	source := models.Source{
		Url:  indexURL,
		Note: "senado.gov.br website",
	}

	var doc *goquery.Document
	var e error

	if doc, e = goquery.NewDocument(indexURL + "/senadores/"); e != nil {
		parser.Log.Critical(e.Error())
	}

	doc.Find("#senadores tbody tr").Each(func(i int, s *goquery.Selection) {
		data := s.Find("td")
		name := data.Eq(0).Text()
		link, okLink := data.Eq(0).Find("a").Attr("href")
		if !okLink {
			parser.CheckError(errors.New("link not found"))
		} else {
			link = indexURL + link
		}

		email, okEmail := data.Eq(6).Find("a").Attr("href")
		if !okEmail {
			email = ""
		} else {
			email = strings.Replace(email, "mailto:", "", -1)
		}

		partyId := models.MakeUri(data.Eq(1).Text())
		DB.Upsert(bson.M{"id": partyId}, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"id":             partyId,
				"classification": "party",
			},
		}, &models.Party{})

		parliamenrianId := models.MakeUri(name)
		q := bson.M{
			"id": parliamenrianId,
		}

		re := regexp.MustCompile("paginst/senador(.+)a.asp")
		senatorId := re.FindStringSubmatch(link)[1]

		_, err := DB.Upsert(q, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$addToSet": bson.M{
				"sources": source,
				"contactdetails": bson.M{
					"$each": []models.ContactDetail{
						{
							Label:   "Telefone",
							Type:    "phone",
							Value:   data.Eq(4).Text(),
							Sources: []models.Source{source},
						},
						{
							Label:   "Fax",
							Type:    "fax",
							Value:   data.Eq(5).Text(),
							Sources: []models.Source{source},
						},
					},
				},
				"identifiers": bson.M{
					"$each": []models.Identifier{
						{Identifier: senatorId, Scheme: "CodSenador"},
					},
				},
			},
			"$set": bson.M{
				"name":      name,
				"email":     email,
				"link":      link,
				"shortname": models.MakeUri(name),
			},
		}, models.Parliamentarian{})
		parser.CheckError(err)

//.........这里部分代码省略.........
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:101,代码来源:save_senator_from_index.go

示例12: mapFromJSON

	var result interface{}
	json.Unmarshal(data, &result)
	return result.([]interface{})
}

/*
Convert JSON data into a map.
*/
func mapFromJSON(data []byte) map[string]interface{} {
	var result interface{}
	json.Unmarshal(data, &result)
	return result.(map[string]interface{})
}

var _ = Describe("Service", func() {
	var databaseDB database.MongoDB
	var request *http.Request
	var recorder *httptest.ResponseRecorder
	var r *gin.Engine

	BeforeEach(func() {
		databaseDB = database.NewMongoDB("test")
		r = gin.Default()

		parliamentarians := service.ParliamentariansService{r}
		parliamentarians.Run(databaseDB)

		parties := service.PartiesService{r}
		parties.Run(databaseDB)

		states := service.StatesService{r}
开发者ID:zannet,项目名称:api,代码行数:31,代码来源:service_test.go

示例13: Run

func (_ StationBot) Run(db database.MongoDB) {
	doc, err := goquery.NewDocument("http://www.metro.sp.gov.br/app/trajeto/xt/estacoesTipoXML.asp")
	parser.CheckError(err)
	doc.Find("estacao").Each(func(_ int, s *goquery.Selection) {
		id, _ := s.Attr("estacaoid")
		name, _ := s.Attr("nome")

		lineId, _ := s.Attr("linhaid")
		_lineName, _ := s.Attr("linha")

		typeId, _ := s.Attr("tipoid")
		typeName, _ := s.Attr("tipo")

		if typeId == "3" {
			return
		}

		lineName := "Linha " + strings.Split(_lineName, " ")[0]
		uri := models.MakeUri(lineName)
		cannonicaluri := uri
		names := strings.Split(lineName, "-")
		if len(names) == 3 {
			cannonicaluri = models.MakeUri(strings.Replace(lineName, names[2], "", -1))
		}

		lineQ := bson.M{"id": uri}
		_, err := db.Upsert(lineQ, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"name":          lineName,
				"cannonicaluri": cannonicaluri,
				"metroid":       lineId,
				"type": bson.M{
					"id":   typeId,
					"name": typeName,
				},
			},
		}, models.Line{})
		parser.CheckError(err)

		q := bson.M{"id": models.MakeUri(name)}
		_, err = db.Upsert(q, bson.M{
			"$setOnInsert": bson.M{
				"createdat": time.Now(),
			},
			"$currentDate": bson.M{
				"updatedat": true,
			},
			"$set": bson.M{
				"metroid": id,
				"name":    name,
			},
		}, models.Station{})
		parser.CheckError(err)

		log.Println(id, name, lineId, lineName, typeName)
	})
}
开发者ID:databr,项目名称:metrosp-bot,代码行数:63,代码来源:station_bot.go

示例14: Run

func (_ SavePartiesFromTSE) Run(DB database.MongoDB) {
	url := "http://www.tse.jus.br/partidos/partidos-politicos/registrados-no-tse"

	if parser.IsCached(url) {
		parser.Log.Info("SavePartiesFromTSE Cached")
		return
	}
	defer parser.DeferedCache(url)

	source := models.Source{
		Url:  url,
		Note: "Tribunal Superior Eleitoral",
	}

	var doc *goquery.Document
	var e error

	if doc, e = goquery.NewDocument(url); e != nil {
		parser.Log.Critical(e.Error())
	}

	const (
		IDX = iota
		SIGLA_IDX
		NAME_IDX
		DEFERIMENTO_IDX
		PRESIDENT_IDX
		N_IDX
	)

	doc.Find("#textoConteudo table tr").Each(func(i int, s *goquery.Selection) {
		if s.Find(".titulo_tabela").Length() < 6 && s.Find("td").Length() > 1 {
			info := s.Find("td")
			parser.Log.Info("%s - %s - %s - %s - %s - %s",
				info.Eq(IDX).Text(),
				info.Eq(SIGLA_IDX).Text(),
				info.Eq(NAME_IDX).Text(),
				info.Eq(DEFERIMENTO_IDX).Text(),
				info.Eq(PRESIDENT_IDX).Text(),
				info.Eq(N_IDX).Text(),
			)

			partyId := models.MakeUri(info.Eq(SIGLA_IDX).Text())
			DB.Upsert(bson.M{"id": partyId}, bson.M{
				"$setOnInsert": bson.M{
					"createdat": time.Now(),
				},
				"$currentDate": bson.M{
					"updatedat": true,
				},
				"$set": bson.M{
					"id":   partyId,
					"name": parser.Titlelize(info.Eq(NAME_IDX).Text()),
					"othernames": []bson.M{{
						"name": info.Eq(SIGLA_IDX).Text(),
					}},
					"classification": "party",
				},
				"$addToSet": bson.M{
					"sources": []models.Source{source},
				},
			}, &models.Party{})

			urlDetails, b := info.Eq(SIGLA_IDX).Find("a").Attr("href")
			if b {
				docDetails, err := goquery.NewDocument(urlDetails)
				if err != nil {
					parser.Log.Critical(err.Error())
				}

				sourceDetails := models.Source{
					Url:  urlDetails,
					Note: "Tribunal Superior Eleitoral",
				}

				contactdetails := make([]bson.M, 0)

				details := docDetails.Find("#ancora-text-um p")

				address := strings.Split(details.Eq(3).Text(), ":")[1]
				contactdetails = append(contactdetails, bson.M{
					"label":   "Endereço",
					"type":    "address",
					"value":   address,
					"sources": []models.Source{sourceDetails},
				})

				contactdetails = append(contactdetails, bson.M{
					"label":   "CEP",
					"type":    "zipcode",
					"value":   findZipcode(0, details),
					"sources": []models.Source{sourceDetails},
				})

				phoneString := strings.Split(details.Eq(5).Text(), ":")[1]
				phone := strings.Split(phoneString, "/")[0]
				contactdetails = append(contactdetails, bson.M{
					"label":   "Telefone",
					"type":    "phone",
					"value":   phone,
//.........这里部分代码省略.........
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:101,代码来源:parties_from_tse.go

示例15: getQuotaPage

func getQuotaPage(id, url string, DB database.MongoDB) {
	if parser.IsCached(url) {
		return
	}
	defer parser.DeferedCache(url)

	<-time.After(2 * time.Second)
	doc, err := goquery.NewDocument(url)
	if err != nil {
		parser.Log.Error(err.Error(), url)
		return
	}

	var p models.Parliamentarian
	DB.FindOne(bson.M{
		"id": id,
	}, &p)

	doc.Find(".espacoPadraoInferior2 tr:not(.celulasCentralizadas)").Each(func(i int, s *goquery.Selection) {
		data := s.Find("td")
		cnpj := data.Eq(0).Text()

		if cnpj == "TOTAL" {
			return
		}
		suplier := data.Eq(1).Text()
		orderN := strings.TrimSpace(data.Eq(2).Text())
		companyUri := models.MakeUri(suplier)

		if cnpj == "" {
			cnpj = companyUri
		}

		_, err := DB.Upsert(bson.M{"id": cnpj}, bson.M{
			"$set": bson.M{
				"name": suplier,
				"uri":  companyUri,
			},
		}, models.Company{})
		parser.CheckError(err)

		switch len(data.Nodes) {
		case 4:
			// value :=  data.Eq(3).Text()
			// log.Println("normal:", cnpj, "|", suplier, "|", orderN, value)
			// log.Println("skip")
		case 7:
			sendedAt, _ := time.Parse("2006-01-02", strings.Split(data.Eq(3).Text(), " ")[0])
			value := strings.Replace(data.Eq(6).Text(), "R$", "", -1)
			value = strings.Replace(value, ".", "", -1)
			value = strings.Replace(value, "-", "", -1)
			value = strings.TrimSpace(strings.Replace(value, ",", ".", -1))
			valueF, _ := strconv.ParseFloat(value, 64)

			parser.Log.Debug(orderN)

			orderNS := strings.Split(orderN, ":")
			var ticket string
			if len(orderNS) == 1 {
				ticket = strings.TrimSpace(orderNS[0])
			} else {
				ticket = strings.TrimSpace(orderNS[1])
			}

			_, err = DB.Upsert(bson.M{"order": orderN, "parliamentarian": p.Id}, bson.M{
				"$set": bson.M{
					"company":        cnpj,
					"date":           sendedAt,
					"passenger_name": data.Eq(4).Text(),
					"route":          data.Eq(5).Text(),
					"value":          valueF,
					"ticket":         ticket,
				},
			}, models.Quota{})
			parser.CheckError(err)

		default:
			panic(data.Text())
		}
	})
}
开发者ID:databr,项目名称:parliamentarian-bot,代码行数:81,代码来源:deputies_quotas.go


注:本文中的github.com/databr/api/database.MongoDB类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。