<commit_before>package news_getter

import(
	"fmt"
	"encoding/json"
	"time"

	"web_apps/news_aggregator/modules/database"
)

var (
	loop_counter_delay = 10
	hacker_news_provider = "https://news.ycombinator.com"
	hacker_news_name	= "hackernews"
)

type HackerNewsTopStoriesId []int

func StartHackerNews() {
	top_stories_ids := topStoriesId()
	content_out := make(chan jsonNewsBody)
	time_profiler := make(chan string)

	go func() {
		for t := range time.Tick(time.Duration(loop_counter_delay) * time.Second) {
			fmt.Println("running the loop")
			_ = t
			for _, id := range top_stories_ids {
				go func(id int, content_out chan jsonNewsBody, time_profiler chan string) {
					start := time.Now()
					news_content := hackerNewsReader(id)
					content_out <- news_content
					time_profiler <- fmt.Sprintf("%v", time.Since(start))
				}(id, content_out, time_profiler)
			}
		}
	}()

	go func() {
		for {
			content_out_msg := <- content_out
			time_profiler_out := <- time_profiler
			//fmt.Println(content_out_msg.Title)
			//fmt.Println(content_out_msg.Url)
			time_f := content_out_msg.Time
			content_out_msg.CreatedAt = fmt.Sprintf("%v", time.Now().Local())
			content_out_msg.ProviderUrl = hacker_news_provider
			content_out_msg.ProviderName = hacker_news_name
			//fmt.Println(time.Unix(int64(time_f), 0))
			_ = time_f

			// check if can save
			// then save
			can_save := database.HackerNewsFindIfExist(content_out_msg.Title)
			if can_save {
				database.HackerNewsInsert(content_out_msg)
			} else {
				//fmt.Println("did not save!")
			}
			_ = time_profiler_out
			//fmt.Println(time_profiler_out)
			//fmt.Println("----------------------------")
		}
	}()

}

func topStoriesId() []int {
	var top_stories_id_url string = "https://hacker-news.firebaseio.com/v0/topstories.json"
	var id_containers HackerNewsTopStoriesId
	response, _ := httpGet(top_stories_id_url)
	defer response.Body.Close()

	contents, _ := responseReader(response)
	if err := json.Unmarshal(contents, &id_containers); err != nil {
		return id_containers
	}

	// make error handler
	return id_containers
}

func hackerNewsReader(id int) jsonNewsBody{
	news_url := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id)
	var news_content jsonNewsBody
	response, err := httpGet(news_url)
	if err != nil {
		//fmt.Println(err)
		return news_content
	}
	defer response.Body.Close()

	contents, _ := responseReader(response)
	if err := json.Unmarshal(contents, &news_content); err != nil {
		return news_content
	}
	return news_content
}
<commit_msg>update timer and top stories id<commit_after>package news_getter

import(
	"fmt"
	"encoding/json"
	"time"

	"web_apps/news_aggregator/modules/database"
)

var (
	loop_counter_delay = 300
	hacker_news_provider = "https://news.ycombinator.com"
	hacker_news_name	= "hackernews"
)

type HackerNewsTopStoriesId []int

func StartHackerNews() {
	content_out := make(chan jsonNewsBody)
	time_profiler := make(chan string)

	go func() {
		for t := range time.Tick(time.Duration(loop_counter_delay) * time.Second) {
			top_stories_ids := topStoriesId()
			fmt.Println("running the loop")
			_ = t
			for _, id := range top_stories_ids {
				go func(id int, content_out chan jsonNewsBody, time_profiler chan string) {
					start := time.Now()
					news_content := hackerNewsReader(id)
					content_out <- news_content
					time_profiler <- fmt.Sprintf("%v", time.Since(start))
				}(id, content_out, time_profiler)
			}
		}
	}()

	go func() {
		for {
			content_out_msg := <- content_out
			time_profiler_out := <- time_profiler
			//fmt.Println(content_out_msg.Title)
			//fmt.Println(content_out_msg.Url)
			time_f := content_out_msg.Time
			content_out_msg.CreatedAt = fmt.Sprintf("%v", time.Now().Local())
			content_out_msg.ProviderUrl = hacker_news_provider
			content_out_msg.ProviderName = hacker_news_name
			//fmt.Println(time.Unix(int64(time_f), 0))
			_ = time_f

			// check if can save
			// then save
			can_save := database.HackerNewsFindIfExist(content_out_msg.Title)
			if can_save {
				database.HackerNewsInsert(content_out_msg)
			} else {
				//fmt.Println("did not save!")
			}
			_ = time_profiler_out
			//fmt.Println(time_profiler_out)
			//fmt.Println("----------------------------")
		}
	}()

}

func topStoriesId() []int {
	var top_stories_id_url string = "https://hacker-news.firebaseio.com/v0/topstories.json"
	var id_containers HackerNewsTopStoriesId
	response, _ := httpGet(top_stories_id_url)
	defer response.Body.Close()

	contents, _ := responseReader(response)
	if err := json.Unmarshal(contents, &id_containers); err != nil {
		return id_containers
	}

	// make error handler
	return id_containers
}

func hackerNewsReader(id int) jsonNewsBody{
	news_url := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id)
	var news_content jsonNewsBody
	response, err := httpGet(news_url)
	if err != nil {
		//fmt.Println(err)
		return news_content
	}
	defer response.Body.Close()

	contents, _ := responseReader(response)
	if err := json.Unmarshal(contents, &news_content); err != nil {
		return news_content
	}
	return news_content
}

{'fraction_non_alphanumeric': 0.13406292749658003, 'fraction_numerical': 0.00293140512018761, 'mean_word_length': 3.9022988505747125, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 6, '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': '2116242', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1837, 'n_words': 333}