예제 #1
0
func (story *Story) PreInsert(s gorp.SqlExecutor) error {
	story.Created = milli.Timestamp(time.Now())
	story.Updated = milli.Timestamp(time.Now())
	story.LastDecayTimestamp = milli.Timestamp(time.Now())
	story.CalculateScore(s)
	return story.Validate()
}
예제 #2
0
func (t *Token) PreInsert(s gorp.SqlExecutor) error {
	t.Created = milli.Timestamp(time.Now())
	t.Updated = milli.Timestamp(time.Now())
	t.Value = uniuri.NewLen(30)
	ex := time.Now().AddDate(0, 0, 14)
	t.Expiration = milli.Timestamp(ex)
	return t.Validate()
}
예제 #3
0
func (m *Member) PreInsert(s gorp.SqlExecutor) error {
	m.Created = milli.Timestamp(time.Now())
	m.Updated = milli.Timestamp(time.Now())
	if m.Email == "" {
		m.Email = fmt.Sprintf("*****@*****.**", uniuri.NewLen(8))
	}
	if err := m.updateCategoires(s); err != nil {
		return err
	}
	return m.Validate()
}
예제 #4
0
func (m *Member) PreUpdate(s gorp.SqlExecutor) error {
	m.Updated = milli.Timestamp(time.Now())
	if err := m.updateCategoires(s); err != nil {
		return err
	}
	return m.Validate()
}
예제 #5
0
func TestTime(t *testing.T) {
	now := time.Now()
	timestamp := milli.Timestamp(now)
	final := milli.Time(timestamp)
	if now.Unix() != final.Unix() {
		t.Error("incorrect conversion")
	}
}
예제 #6
0
func (f *Feed) PreInsert(s gorp.SqlExecutor) error {
	f.Created = milli.Timestamp(time.Now())
	f.Updated = milli.Timestamp(time.Now())

	icon := ""
	switch FeedType(f.Type) {
	case FeedTypeFacebook:
		// get user from facebook api
		session := facebookSession()
		route := fmt.Sprintf("/%s", f.Identifier)
		result, err := session.Api(route, facebook.GET, nil)
		if err != nil {
			return httperr.New(http.StatusBadRequest, "invalid facebook id", err)
		}
		// decode response
		user := &fbookUser{}
		if err := result.Decode(user); err != nil {
			return err
		}
		icon = user.Cover.Source
	case FeedTypeTwitter:
		api := twitterAPI()
		user, err := api.GetUsersShow(f.Identifier, url.Values{})
		if err != nil {
			return err
		}
		icon = user.ProfileImageURL
	}

	if icon != "" {
		member := &Member{}
		if err := sqlutil.SelectOneRelation(s, TableNameMember, f.MemberID, member); err != nil {
			return err
		}
		member.Icon = icon

		if _, err := s.Update(member); err != nil {
			return err
		}
	}

	return f.Validate()
}
예제 #7
0
func DecayScores(s gorp.SqlExecutor) error {
	current := milli.Timestamp(time.Now())
	yesterday := milli.Timestamp(time.Now().Add(time.Hour * -24))
	tenDaysAgo := milli.Timestamp(time.Now().Add((time.Hour * 24) * 10))
	query := squirrel.Select("*").From(TableNameStory).
		Where("!(LastDecayTimestamp between ? and ?)", yesterday, current).
		Where("Timestamp > ?", tenDaysAgo)

	stories := []*Story{}
	sqlutil.Select(s, query, &stories)
	for _, story := range stories {
		story.Score /= 2.0
		story.LastDecayTimestamp = milli.Timestamp(time.Now())
		if _, err := s.Update(story); err != nil {
			return err
		}
	}
	return nil
}
예제 #8
0
func NewStoryRSS(member *Member, feed *Feed, item *feeder.Item) *Story {
	// parse pub date
	itemTime, err := item.ParsedPubDate()
	if err != nil {
		itemTime = time.Now()
	}
	// form links
	links := []string{}
	for _, link := range item.Links {
		links = append(links, link.Href)
	}

	isAtom := func() bool {
		return item.Id != ""
	}

	sourceID := ""
	body := ""
	if isAtom() {
		sourceID = item.Id
		body = item.Content.Text
	} else { // is RSS
		sourceID = *item.Guid
		// use description or title for body
		body = item.Description
		if body == "" {
			body = item.Title
		}
	}

	// parse html for images
	images := []string{}
	doc := xmlx.New()
	doc.LoadString(strings.ToLower(body), nil)
	imgNodes := doc.SelectNodesRecursive("", "img")
	for _, img := range imgNodes {
		images = append(images, img.As("", "src"))
	}

	return &Story{
		MemberID:       member.ID,
		MemberName:     member.Name,
		FeedID:         feed.ID,
		FeedIdentifier: feed.Identifier,
		Timestamp:      milli.Timestamp(itemTime),
		Body:           body,
		FeedType:       string(FeedTypeRSS),
		SourceURL:      "",
		SourceID:       sourceID,
		Latitude:       0.0,
		Longitude:      0.0,
		LinksRaw:       strings.Join(links, ","),
		ImagesRaw:      strings.Join(images, ","),
	}
}
예제 #9
0
func NewFacebookStory(member *Member, feed *Feed, post *FacebookPost) *Story {
	t, err := time.Parse(time.RFC3339Nano, post.CreatedAt)
	if err != nil {
		t = time.Now()
	}

	// if message is blank, forget the post
	if post.Message == "" {
		return nil
	}

	urlParts := strings.Split(post.Link, "?")
	if len(urlParts) > 0 {
		post.Link = urlParts[0]
	}

	post.Picture = ""
	if post.Type == "photo" {
		session := facebookSession()
		route := fmt.Sprintf("/%s?fields=images", post.ObjectId)
		result, err := session.Api(route, facebook.GET, nil)
		if err == nil {
			image := &FacebookPhoto{}
			if err := result.Decode(image); err == nil {
				if len(image.Images) > 0 && !strings.Contains(image.Images[0].Source, "?") {
					post.Picture = image.Images[0].Source
				}
			}
		}
	}

	return &Story{
		MemberID:       member.ID,
		MemberName:     member.Name,
		FeedID:         feed.ID,
		FeedIdentifier: feed.Identifier,
		Timestamp:      milli.Timestamp(t),
		Body:           strings.TrimSpace(post.Message),
		FeedType:       string(FeedTypeFacebook),
		SourceURL:      post.Link,
		SourceID:       post.Id,
		Latitude:       0.0,
		Longitude:      0.0,
		Score:          2,
		LinksRaw:       "",
		HashtagsRaw:    "",
		ImagesRaw:      post.Picture,
	}
}
예제 #10
0
func NewStoryTwitter(member *Member, feed *Feed, tweet anaconda.Tweet) *Story {
	hashtags := []string{}
	for _, hashtag := range tweet.Entities.Hashtags {
		hashtags = append(hashtags, hashtag.Text)
	}

	urls := []string{}
	for _, url := range tweet.Entities.Urls {
		urls = append(urls, url.Url)
	}

	t, err := tweet.CreatedAtTime()
	if err != nil {
		t = time.Now()
	}

	images := []string{}
	for _, media := range tweet.Entities.Media {
		if media.Type == "image" {
			images = append(images, media.Media_url)
		}
	}
	score := tweet.FavoriteCount + (2 * tweet.RetweetCount)
	sourceURL := fmt.Sprintf("http://twitter.com/%s/status/%s", tweet.User.ScreenName, tweet.IdStr)
	return &Story{
		MemberID:       member.ID,
		MemberName:     member.Name,
		FeedID:         feed.ID,
		FeedIdentifier: feed.Identifier,
		Timestamp:      milli.Timestamp(t),
		Body:           tweet.Text,
		FeedType:       string(FeedTypeTwitter),
		SourceURL:      sourceURL,
		SourceID:       tweet.IdStr,
		Latitude:       0.0,
		Longitude:      0.0,
		Score:          float64(score),
		LinksRaw:       strings.Join(urls, ","),
		HashtagsRaw:    strings.Join(hashtags, ","),
		ImagesRaw:      strings.Join(images, ","),
	}
}
예제 #11
0
func (f *Feed) PreUpdate(s gorp.SqlExecutor) error {
	f.Updated = milli.Timestamp(time.Now())
	return f.Validate()
}
예제 #12
0
func (t *Token) PreUpdate(s gorp.SqlExecutor) error {
	t.Updated = milli.Timestamp(time.Now())
	return t.Validate()
}
예제 #13
0
func (story *Story) PreUpdate(s gorp.SqlExecutor) error {
	story.Updated = milli.Timestamp(time.Now())
	return story.Validate()
}
예제 #14
0
func (c *Category) PreUpdate(s gorp.SqlExecutor) error {
	c.Updated = milli.Timestamp(time.Now())
	return c.Validate()
}
예제 #15
0
func (c *Community) PreInsert(s gorp.SqlExecutor) error {
	c.Created = milli.Timestamp(time.Now())
	c.Updated = milli.Timestamp(time.Now())
	return c.Validate()
}