<commit_before>package crawler

import (
	"sync"

	"github.com/pkg/errors"
	"github.com/utahta/go-atomicbool"
	"github.com/utahta/momoclo-channel/appengine/lib/log"
	"github.com/utahta/momoclo-crawler"
	"golang.org/x/net/context"
	"google.golang.org/appengine/urlfetch"
)

func Crawl(ctx context.Context) error {
	var workQueue = make(chan bool, 20)
	defer close(workQueue)

	glog := log.NewGaeLogger(ctx)
	clients := crawlChannelClients(ctx)

	errFlg := atomicbool.New(false)
	var wg sync.WaitGroup
	wg.Add(len(clients))
	for _, cli := range clients {
		workQueue <- true
		go func(ctx context.Context, cli *crawler.ChannelClient) {
			defer func() {
				<-workQueue
				wg.Done()
			}()

			ch, err := cli.Fetch()
			if err != nil {
				errFlg.Set(true)
				glog.Error(err)
				return
			}
			glog.Infof("%+v", ch)

			q := NewQueueTask(glog)
			if err := q.PushTweet(ctx, ch); err != nil {
				errFlg.Set(true)
				glog.Error(err)
			}
			if err := q.PushLine(ctx, ch); err != nil {
				errFlg.Set(true)
				glog.Error(err)
			}
		}(ctx, cli)
	}
	wg.Wait()

	if errFlg.Enabled() {
		return errors.New("Errors occured in crawler.Crawl.")
	}
	return nil
}

func crawlChannelClients(ctx context.Context) []*crawler.ChannelClient {
	option := crawler.WithHTTPClient(urlfetch.Client(ctx))
	return []*crawler.ChannelClient{
		retrieveChannelClient(crawler.NewTamaiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewMomotaBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewAriyasuBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewSasakiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewTakagiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewAeNewsChannelClient(option)),
		retrieveChannelClient(crawler.NewYoutubeChannelClient(option)),<commit_msg>Remove debug log<commit_after>package crawler

import (
	"sync"

	"github.com/pkg/errors"
	"github.com/utahta/go-atomicbool"
	"github.com/utahta/momoclo-channel/appengine/lib/log"
	"github.com/utahta/momoclo-crawler"
	"golang.org/x/net/context"
	"google.golang.org/appengine/urlfetch"
)

func Crawl(ctx context.Context) error {
	var workQueue = make(chan bool, 20)
	defer close(workQueue)

	glog := log.NewGaeLogger(ctx)
	clients := crawlChannelClients(ctx)

	errFlg := atomicbool.New(false)
	var wg sync.WaitGroup
	wg.Add(len(clients))
	for _, cli := range clients {
		workQueue <- true
		go func(ctx context.Context, cli *crawler.ChannelClient) {
			defer func() {
				<-workQueue
				wg.Done()
			}()

			ch, err := cli.Fetch()
			if err != nil {
				errFlg.Set(true)
				glog.Error(err)
				return
			}

			q := NewQueueTask(glog)
			if err := q.PushTweet(ctx, ch); err != nil {
				errFlg.Set(true)
				glog.Error(err)
			}
			if err := q.PushLine(ctx, ch); err != nil {
				errFlg.Set(true)
				glog.Error(err)
			}
		}(ctx, cli)
	}
	wg.Wait()

	if errFlg.Enabled() {
		return errors.New("Errors occured in crawler.Crawl.")
	}
	return nil
}

func crawlChannelClients(ctx context.Context) []*crawler.ChannelClient {
	option := crawler.WithHTTPClient(urlfetch.Client(ctx))
	return []*crawler.ChannelClient{
		retrieveChannelClient(crawler.NewTamaiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewMomotaBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewAriyasuBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewSasakiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewTakagiBlogChannelClient(1, "", option)),
		retrieveChannelClient(crawler.NewAeNewsChannelClient(option)),
		retrieveChannelClient(crawler.NewYoutubeChannelClient(option)),
{'fraction_non_alphanumeric': 0.14246724890829696, 'fraction_numerical': 0.003820960698689956, 'mean_word_length': 4.595419847328245, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1337971', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1345, 'n_words': 213}