Beispiel #1
0
// Notify updates the authorized user's status. Returns the tweet ID on success.
func (n *Notifier) Notify(r *scuttlebutt.Repository) (*scuttlebutt.Message, error) {
	text := NotifyText(r)

	// Construct request.
	req, err := http.NewRequest("POST", "/1.1/statuses/update.json", strings.NewReader((url.Values{"status": {text}}).Encode()))
	if err != nil {
		return nil, fmt.Errorf("notify request: %s", err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	// Send request.
	resp, err := n.Client.SendRequest(req)
	if err != nil {
		return nil, fmt.Errorf("send request: %s", err)
	}
	defer resp.Body.Close()

	// Parse the response.
	var tweet twittergo.Tweet
	if err := resp.Parse(&tweet); err != nil && strings.Contains(err.Error(), "Status is over 140 characters") {
		return nil, ErrTweetTooLong
	} else if err != nil {
		return nil, fmt.Errorf("parse: %s", err)
	}

	// Update last tweet time cache.
	n.lastTweetTime = tweet.CreatedAt()

	return &scuttlebutt.Message{ID: tweet.Id(), Text: text, RepositoryID: r.ID}, nil
}
Beispiel #2
0
func newChannelConverter(c chan *Tweet, client *twittergo.Client) func([]byte) {
	rateLimitActive := false
	timer := time.AfterFunc(10*time.Second, func() {
		rateLimitActive = false
	})
	timer.Stop()

	return func(data []byte) {
		if rateLimitActive {
			return
		}

		rateLimitActive = true
		timer.Reset(10 * time.Second)

		var tweet twittergo.Tweet
		err := json.Unmarshal(data, &tweet)
		if err != nil {
			log.Printf("Received invalid tweet: %s", err)
			return
		}

		profileImageUrl, err := getUserProfileImageURL(client, tweet.User().Id())
		if err != nil {
			log.Printf("Could not get users profile image url: %s", err)
			return
		}
		profileImage, err := getImage(profileImageUrl)
		if err != nil {
			log.Printf("Could not get users profile image: %s", err)
			return
		}
		c <- &Tweet{
			Text: tweet.Text(),
			Date: tweet.CreatedAt(),
			Author: TwitterUser{
				ScreenName:     tweet.User().ScreenName(),
				Name:           tweet.User().Name(),
				ProfilePicture: profileImage,
			},
		}
	}
}