예제 #1
0
파일: bot.go 프로젝트: Pent/bot
func (bot *Bot) Google(query, channel string) {
	r, err := http.Get("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q=" + query)
	defer r.Body.Close()

	if err != nil {
		log.Println(err)
	}
	if r.StatusCode != http.StatusOK {
		log.Println(r.Status)
	}

	//create a custom struct for the json response
	//somehow Go magically transplants the response data into this
	var google struct {
		ResponseData struct {
			Results []struct {
				TitleNoFormatting string
				Content           string
				URL               string
			}
		}
	}

	//parse response body json to Go
	dec := json.NewDecoder(r.Body)
	dec.Decode(&google)

	//output results to channel
	for _, item := range google.ResponseData.Results {
		//fixme: sending commands
		content := sanitize.Accents(sanitize.HTML(item.Content))
		bot.SendMessage(item.TitleNoFormatting+" "+item.URL+" "+content, channel)
	}
}
예제 #2
0
func (Lst_ball *All_ball) InsertBallon(eball *list.Element, base *db.Env) (executed bool, err error) {
	NewBall := eball.Value.(*Ball)
	var IdC int64
	err = base.Db.QueryRow("SELECT insertcontainer($1, $2, $3, $4, $5, $6, $7, $8)",
		NewBall.Creator.Value.(*users.User).Id,
		NewBall.Coord.Value.(Checkpoint).Coord.Lat,
		NewBall.Coord.Value.(Checkpoint).Coord.Lon,
		NewBall.Wind.Degress, NewBall.Wind.Speed,
		sanit.Accents(strings.Trim(NewBall.Title, "\x00")),
		NewBall.Id_ball,
		NewBall.Stats.CreationDate).Scan(&IdC)
	if err != nil {
		Lst_ball.Logger.Println("Error on QueryRow: ", err)
		return false, err
	}
	Lst_ball.SetStatsBallon(IdC, NewBall.Stats, base.Db)
	err = Lst_ball.InsertMessages(NewBall.Messages, IdC, base)
	if err != nil {
		Lst_ball.Logger.Println("Insert Ball fail")
		return false, err
	}
	Lst_ball.SetFollowerBalls(NewBall, base)
	Lst_ball.SetItinerary(base.Db, eball)
	executed = true
	return executed, err
}
예제 #3
0
// Clean a string from special characters, white spaces and
// transform upper case letters to lower case.
func CleanString(s string) string {
	s = strings.Trim(strings.ToLower(s), " ")
	s = sanitize.Accents(s)

	// Remove all other unrecognised characters
	s = regexp.MustCompile(`[^[:alnum:]]`).ReplaceAllString(s, "")

	return s
}
예제 #4
0
func (place *Place) checkBody(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	create := CreatePlace{}
	err := request.ReadEntity(&create)
	if err != nil {
		helpers.PrintLog(request, response, place.user.Name)

		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}
	place.City = helpers.RemoveManySpaces(sanitize.Accents(create.City))
	place.Country = helpers.RemoveManySpaces(sanitize.Accents(create.Country))
	place.Name = helpers.RemoveManySpaces(sanitize.Accents(create.Name))
	place.Type = helpers.RemoveManySpaces(create.Type)
	place.Recurring, _ = strconv.ParseBool(create.Recurring)
	place.StartAt = create.StartAt
	place.EndAt = create.EndAt
	place.Address = helpers.RemoveManySpaces(create.Address)
	place.PostalCode = helpers.RemoveManySpaces(create.PostalCode)
	chain.ProcessFilter(request, response)
}
예제 #5
0
func (place *Place) checkFormParameter(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	place.Name = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("name")))
	place.City = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("city")))
	place.Country = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("country")))
	place.Type = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("type")))
	place.Recurring, _ = strconv.ParseBool(request.Request.FormValue("recurring"))
	place.Address = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("address")))
	place.PostalCode = helpers.RemoveManySpaces(sanitize.Accents(request.Request.FormValue("postalcode")))
	place.StartAt, _ = time.Parse("2006-01-02T15:04:05.000Z", request.Request.FormValue("startAt"))
	place.EndAt, _ = time.Parse("2006-01-02T15:04:05.000Z", request.Request.FormValue("endAt"))
	chain.ProcessFilter(request, response)
}
예제 #6
0
func (Lst_ball *All_ball) InsertMessages(messages *list.List, idBall int64, base *db.Env) (err error) {
	i := 0
	for e := messages.Front(); e != nil; e = e.Next() {
		err = base.Transact(base.Db, func(tx *sql.Tx) error {
			stm, err := tx.Prepare("INSERT INTO message(content, containerid, index_m, size) VALUES ($1, $2, $3, $4)")
			if err != nil {
				Lst_ball.Logger.Println("Erreur tx prepare: ", err)
				return err
			}
			defer stm.Close()
			row, err := stm.Query(sanit.Accents(strings.Trim(e.Value.(Message).Content, "\x00")), idBall, i, e.Value.(Message).Size)
			if err != nil {
				Lst_ball.Logger.Println("Erreur Query: ", err)
				return err
			}
			defer row.Close()
			i++
			return err
		})
	}
	return nil
}