Ejemplo n.º 1
0
func (client *UsenetClient) FindStart(group string, retention int) (int64, int64, error) {
	_, low, originalHigh, _ := client.Group(group)
	target_date := time.Now().AddDate(0, 0, 0-retention)

	high := originalHigh

	for high > low {
		mid := (low + high) / 2
		art_text, err := client.overviewForArticleId(mid)
		if err != nil {
			return 0, 0, err
		}
		art := article.ParseArticle(art_text)

		parsedDate, err := art.ParsedDate()
		if err != nil {
			log.Fatal(err)
		}
		if parsedDate.Before(target_date) {
			low = mid + 1
		} else {
			high = mid
		}
	}
	return high, originalHigh, nil
}
Ejemplo n.º 2
0
func main() {

	addr := ""
	user := ""
	pass := ""
	group := ""
	retention := 0
	useTls := false

	flag.StringVar(&addr, "addr", "", "Address of usenet server. Example: news.example.com:119")
	flag.StringVar(&user, "user", "", "Username")
	flag.StringVar(&pass, "pass", "", "Password")
	flag.StringVar(&group, "group", "", "Newsgroup to get headers for")
	flag.BoolVar(&useTls, "use-tls", false, "Use a secure connection")
	flag.IntVar(&retention, "retention", 0, "Number of days to download")

	flag.Parse()

	if pass == "" || user == "" || addr == "" || group == "" || retention == 0 {
		flag.PrintDefaults()
		os.Exit(1)
	}

	cl, err := client.NewUsenetClient(addr, useTls)
	if err != nil {
		fmt.Println(err)
	}
	log.Println("Connected")
	cl.Authenticate(user, pass)

	log.Println("Authenticated")
	log.Println("Finding start")
	start, high, err := cl.FindStart(group, retention)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Start: %d, Num: %d", start, high-start)

	log.Println("Getting overview")
	overview, err := cl.OverviewStartingAt(group, start)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	buf_overview := bufio.NewReader(overview)

	for counter := 0; ; counter++ {
		line, err := buf_overview.ReadString('\n')
		if err == io.EOF {
			break
		}

		art := article.ParseArticle(line)
		date, err := art.ParsedDate()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %s %s %d %d %s\n", art.MessageId, art.Subject, art.Filename, art.NumParts, art.PartSequence, date)

		if counter%1000 == 0 {
			log.Println(counter)
		}
	}

	log.Println("Done")
}