<commit_before>package twitter

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/url"
	"strings"

	"github.com/ChimeraCoder/anaconda"
	"github.com/pkg/errors"
	"github.com/utahta/momoclo-channel/crawler"
	"github.com/utahta/momoclo-channel/log"
)

const (
	maxUploadImages = 4
)

type TwitterClient struct {
	Api *anaconda.TwitterApi
	Log log.Logger
}

type mediaImage struct {
	Ids [maxUploadImages]string
}

func NewTwitterClient(consumerKey, consumerSecret, accessToken, accessTokenSecret string) *TwitterClient {
	t := &TwitterClient{}
	t.auth(consumerKey, consumerSecret, accessToken, accessTokenSecret)
	t.Log = log.NewSilentLogger()
	return t
}

func (t *TwitterClient) auth(consumerKey, consumerSecret, accessToken, accessTokenSecret string) {
	anaconda.SetConsumerKey(consumerKey)
	anaconda.SetConsumerSecret(consumerSecret)
	t.Api = anaconda.NewTwitterApi(accessToken, accessTokenSecret)
}

func (t *TwitterClient) Tweet(ch *crawler.Channel) {
	for _, item := range ch.Items {
		t.TweetItem(ch.Title, item)
	}
}

func (t *TwitterClient) TweetItem(title string, item *crawler.ChannelItem) {
	images := t.uploadImages(item)
	videos := t.uploadVideos(item)

	text := t.truncateText(title, item)
	v := url.Values{}
	if len(images) > 0 {
		v.Add("media_ids", strings.Join(images[0].Ids[:], ","))
		images = images[1:]
	} else if len(videos) > 0 {
		v.Add("media_ids", videos[0].MediaIDString)
		videos = videos[1:]
	}
	tweet, err := t.Api.PostTweet(text, v)
	if err != nil {
		t.Log.Errorf("Failed to post tweet. url:%s error:%s", item.Url, err)
		return
	}
	t.Log.Infof("Post tweet. text:%s", text)

	for _, image := range images {
		v := url.Values{}
		v.Add("in_reply_to_status_id", tweet.IdStr)
		v.Add("media_ids", strings.Join(image.Ids[:], ","))

		tweet, err = t.Api.PostTweet("", v)
		if err != nil {
			t.Log.Errorf("Failed to post tweet images. error:%v", err)
			continue
		}
	}

	for _, video := range videos {
		v := url.Values{}
		v.Add("in_reply_to_status_id", tweet.IdStr)
		v.Add("media_ids", video.MediaIDString)

		tweet, err = t.Api.PostTweet("", v)
		if err != nil {
			t.Log.Errorf("Failed to post tweet videos. error:%v", err)
			continue
		}
	}
}
<commit_msg>Add log at post image, video<commit_after>package twitter

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/url"
	"strings"

	"github.com/ChimeraCoder/anaconda"
	"github.com/pkg/errors"
	"github.com/utahta/momoclo-channel/crawler"
	"github.com/utahta/momoclo-channel/log"
)

const (
	maxUploadImages = 4
)

type TwitterClient struct {
	Api *anaconda.TwitterApi
	Log log.Logger
}

type mediaImage struct {
	Ids [maxUploadImages]string
}

func NewTwitterClient(consumerKey, consumerSecret, accessToken, accessTokenSecret string) *TwitterClient {
	t := &TwitterClient{}
	t.auth(consumerKey, consumerSecret, accessToken, accessTokenSecret)
	t.Log = log.NewSilentLogger()
	return t
}

func (t *TwitterClient) auth(consumerKey, consumerSecret, accessToken, accessTokenSecret string) {
	anaconda.SetConsumerKey(consumerKey)
	anaconda.SetConsumerSecret(consumerSecret)
	t.Api = anaconda.NewTwitterApi(accessToken, accessTokenSecret)
}

func (t *TwitterClient) Tweet(ch *crawler.Channel) {
	for _, item := range ch.Items {
		t.TweetItem(ch.Title, item)
	}
}

func (t *TwitterClient) TweetItem(title string, item *crawler.ChannelItem) {
	images := t.uploadImages(item)
	videos := t.uploadVideos(item)

	text := t.truncateText(title, item)
	v := url.Values{}
	if len(images) > 0 {
		v.Add("media_ids", strings.Join(images[0].Ids[:], ","))
		images = images[1:]
	} else if len(videos) > 0 {
		v.Add("media_ids", videos[0].MediaIDString)
		videos = videos[1:]
	}
	tweet, err := t.Api.PostTweet(text, v)
	if err != nil {
		t.Log.Errorf("Failed to post tweet. url:%s error:%s", item.Url, err)
		return
	}
	t.Log.Infof("Post tweet. text:%s", text)

	for _, image := range images {
		v := url.Values{}
		v.Add("in_reply_to_status_id", tweet.IdStr)
		v.Add("media_ids", strings.Join(image.Ids[:], ","))

		tweet, err = t.Api.PostTweet("", v)
		if err != nil {
			t.Log.Errorf("Failed to post tweet images. error:%v", err)
			continue
		}
		t.Log.Infof("Post image. url:%s", item.Url)
	}

	for _, video := range videos {
		v := url.Values{}
		v.Add("in_reply_to_status_id", tweet.IdStr)
		v.Add("media_ids", video.MediaIDString)

		tweet, err = t.Api.PostTweet("", v)
		if err != nil {
			t.Log.Errorf("Failed to post tweet videos. error:%v", err)
			continue
		}
		t.Log.Infof("Post video. url:%s", item.Url)
	}
}

{'fraction_non_alphanumeric': 0.15506117908787542, 'fraction_numerical': 0.004004449388209121, 'mean_word_length': 4.416867469879518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '186098', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1696, 'n_words': 351}