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")
	}
}
Esempio n. 2
0
func (story *Story) CalculateScore(s gorp.SqlExecutor) error {
	f := &Feed{}
	if err := sqlutil.SelectOneRelation(s, TableNameFeed, story.FeedID, f); err != nil {
		return err
	}

	score := 0.0
	if len(story.ImagesSlice()) > 0 {
		score += 10.0
	}
	if len(story.LinksSlice()) > 0 {
		score += 2.0
	}
	if story.Latitude != 0.0 {
		score += 10.0
	}

	switch FeedType(f.Type) {
	case FeedTypeFacebook:
		score += 3.0
	case FeedTypeTwitter:
		score += 3.0
	}

	// randomize score
	score += (10.0 * rand.Float64())

	// increase score for timely posts
	dur := milli.Time(story.Timestamp).Sub(milli.Time(f.LastRetrieved))
	durScore := float64(dur / time.Hour)
	if durScore > 12.0 {
		durScore = 12.0
	} else if durScore < 0.0 {
		durScore = 0.0
	}
	score += durScore

	story.Score += score

	return nil
}
Esempio n. 3
0
func (t *Token) IsExpired() bool {
	exp := milli.Time(t.Expiration)
	return time.Now().After(exp)
}
Esempio n. 4
0
func (ft FeedType) GetStories(s gorp.SqlExecutor, m *Member, f *Feed) error {
	switch ft {
	case FeedTypeRSS:
		itemHandler := func(
			fe *feeder.Feed,
			ch *feeder.Channel,
			newitems []*feeder.Item) {

			for _, item := range newitems {
				story := NewStoryRSS(m, f, item)
				s.Insert(story)
			}
		}
		feed := feeder.New(1, true, nil, itemHandler)
		if err := feed.Fetch(f.Identifier, nil); err != nil {
			fmt.Println(err)
		}
	case FeedTypeTwitter:
		v := url.Values{}
		v.Set("screen_name", f.Identifier)
		v.Set("include_rts", "false")

		anaconda.SetConsumerKey(os.Getenv("twitterApiKey"))
		anaconda.SetConsumerSecret(os.Getenv("twitterApiSecret"))
		api := anaconda.NewTwitterApi("", "")

		tweets, err := api.GetUserTimeline(v)
		if err != nil {
			fmt.Println(err)
		}

		for _, t := range tweets {
			story := NewStoryTwitter(m, f, t)
			if err := s.Insert(story); err == nil {
				fmt.Printf("Added Twitter story for %s.  Date: %s Score: %f\n", m.Name, milli.Time(story.Timestamp).String(), story.Score)
			} else {
				fmt.Printf("Failed to add Twitter story for %s.  Error: %s\n", m.Name, err)
			}
		}
	case FeedTypeFacebook:
		session := facebookSession()
		route := fmt.Sprintf("/%s/posts", f.Identifier)
		result, err := session.Api(route, facebook.GET, nil)
		if err != nil {
			fmt.Println(err)
		}

		posts := &FacebookPosts{}
		if err := result.Decode(posts); err != nil {
			fmt.Println(err)
		}

		for _, post := range posts.Data {
			story := NewFacebookStory(m, f, post)
			if story != nil {
				if err := s.Insert(story); err == nil {
					fmt.Printf("Added Facebook story for %s.  Date: %s  Score: %f\n", m.Name, milli.Time(story.Timestamp).String(), story.Score)
				} else {
					fmt.Printf("Failed to add Facebook story for %s.  Error: %s\n", m.Name, err)
				}
			}
		}
	}
	return nil
}