<commit_before>package main

import (
	"fmt"
	"log"
	"math/rand"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type animeList []anime

type anime struct {
	ID         string    `bson:"id"`
	Name       string    `bson:"name"`
	Href       string    `bson:"href"`
	Episode    int       `bson:"ep"`
	Subs       []string  `bson:"subs"`
	LastUpdate time.Time `bson:"lastUpdate"`
	Show       bool      `bson:"show"`
}

//LIMIT is a time constant for 15 Days
const LIMIT = 15 * 24 * time.Hour

//Gets every anime in animeList db and returns it as AnimeList type
func getAnimeList() (result animeList) {
	defer panicRecovery()

	err := DBanimeList.Find(nil).Sort("lastUpdate").All(&result)
	if err != nil {
		panic(fmt.Sprintf("Error querying MongoDB in function: %s - %s", "getAnimeList", err))
	}

	return
}

//Get every anime this user.id is subscribed to
func getAnimeListForUser(userID string) (result animeList) {
	defer panicRecovery()

	err := DBanimeList.Find(bson.M{"subs": userID}).All(&result)
	if err != nil {
		panic(fmt.Sprintf("Error querying MongoDB in function: %s - %s", "getAnimeListForUser", err))
	}

	return
}

// maintainAnimeList db maintanance function
// changes the 'show' database field of all entries over LIMIT days old to false
// tries to update the 'href' database field for entries that don't have one yet
// TODO: Potentially merge this with rssReader
func maintainAnimeList() {
	// Number of hidden and/or updated entries
	hidden, updated := 0, 0
	newAnimeList := getAnimeList()
	now := time.Now()

	for _, a := range newAnimeList {
		if len(a.Href) < 5 {
			updated += a.GetHref() //TODO: Limit HS scraping to maximum 1 per maintanance, instead of 1 per empty href per maintanance
		}
		if now.Sub(a.LastUpdate) > LIMIT {
			a.Remove()
			hidden++
		}
	}
	log.Printf("AUTO-MAINTANANCE: animeList updated! (hidden: %d | updated: %d)\n",
		hidden, updated)
}

// Insert inserts a new anime entry to db
// generates a unique ID if it isn't present in the object yet
func (a *anime) Insert() {
	if a.ID == "" {
		a.GenID()
	}
	a.LastUpdate = time.Now()
	a.Show = true
	DBanimeList.Insert(a)
}

//Remove anime from db by Anime.Name or Anime.Id
func (a anime) Remove() (success int) {
	success = 0
	if a.Name != "" {
		DBanimeList.Remove(bson.M{"name": a.Name})
		success = 1
	} else if a.ID != "" {
		DBanimeList.Remove(bson.M{"id": a.ID})
		success = 1
	}
	return
}

// UpdateEp updates the db entry with the new episode number
func (a *anime) UpdateEp() {
	updateQuery := bson.M{
		"$set": bson.M{
			"ep":         a.Episode,<commit_msg>changed anime method Remove() to Hide()<commit_after>package main

import (
	"fmt"
	"log"
	"math/rand"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type animeList []anime

type anime struct {
	ID         string    `bson:"id"`
	Name       string    `bson:"name"`
	Href       string    `bson:"href"`
	Episode    int       `bson:"ep"`
	Subs       []string  `bson:"subs"`
	LastUpdate time.Time `bson:"lastUpdate"`
	Show       bool      `bson:"show"`
}

//LIMIT is a time constant for 15 Days
const LIMIT = 15 * 24 * time.Hour

//Gets every anime in animeList db and returns it as AnimeList type
func getAnimeList() (result animeList) {
	defer panicRecovery()

	err := DBanimeList.Find(nil).Sort("lastUpdate").All(&result)
	if err != nil {
		panic(fmt.Sprintf("Error querying MongoDB in function: %s - %s", "getAnimeList", err))
	}

	return
}

//Get every anime this user.id is subscribed to
func getAnimeListForUser(userID string) (result animeList) {
	defer panicRecovery()

	err := DBanimeList.Find(bson.M{"subs": userID}).All(&result)
	if err != nil {
		panic(fmt.Sprintf("Error querying MongoDB in function: %s - %s", "getAnimeListForUser", err))
	}

	return
}

// maintainAnimeList db maintanance function
// changes the 'show' database field of all entries over LIMIT days old to false
// tries to update the 'href' database field for entries that don't have one yet
// TODO: Potentially merge this with rssReader
func maintainAnimeList() {
	// Number of hidden and/or updated entries
	hidden, updated := 0, 0
	newAnimeList := getAnimeList()
	now := time.Now()

	for _, a := range newAnimeList {
		if len(a.Href) < 5 {
			updated += a.GetHref() //TODO: Limit HS scraping to maximum 1 per maintanance, instead of 1 per empty href per maintanance
		}
		if now.Sub(a.LastUpdate) > LIMIT {
			hidden += a.Hide()
		}
	}
	log.Printf("AUTO-MAINTANANCE: animeList updated! (hidden: %d | updated: %d)\n",
		hidden, updated)
}

// Insert inserts a new anime entry to db
// generates a unique ID if it isn't present in the object yet
func (a *anime) Insert() {
	if a.ID == "" {
		a.GenID()
	}
	a.LastUpdate = time.Now()
	a.Show = true
	DBanimeList.Insert(a)
}

// Hide changes database field 'show' for this anime to false
func (a anime) Hide() (success int) {
	defer panicRecovery()

	success = 0

	if a.Name != "" {
		err := DBanimeList.Update(bson.M{"name": a.Name}, bson.M{"show": false})

		if err != nil {
			panic(fmt.Sprintf("Error updating MongoDB document in anime method: %s - %s", "Hide", err))
		}

		success = 1
	} else if a.ID != "" {
		err := DBanimeList.Update(bson.M{"id": a.ID}, bson.M{"show": false})

		if err != nil {
			panic(fmt.Sprintf("Error updating MongoDB document in anime method: %s - %s", "Hide", err))
		}

		success = 1
	}

	return
}

// UpdateEp updates the db entry with the new episode number
func (a *anime) UpdateEp() {
	updateQuery := bson.M{
		"$set": bson.M{
			"ep":         a.Episode,
{'fraction_non_alphanumeric': 0.1384367733857986, 'fraction_numerical': 0.005723484170989089, 'mean_word_length': 3.379013312451057, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5859524', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2000, 'n_words': 624}