forked from gocolly/colly
/
google_groups.go
95 lines (79 loc) · 2.29 KB
/
google_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"encoding/json"
"flag"
"log"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gocolly/colly"
)
// Mail is the container of a single e-mail
type Mail struct {
Title string
Link string
Author string
Date string
Message string
}
func main() {
var groupName string
flag.StringVar(&groupName, "group", "hspbp", "Google Groups group name")
flag.Parse()
threads := make(map[string][]Mail)
threadCollector := colly.NewCollector()
mailCollector := colly.NewCollector()
// Collect threads
threadCollector.OnHTML("tr", func(e *colly.HTMLElement) {
ch := e.DOM.Children()
author := ch.Eq(1).Text()
// deleted topic
if author == "" {
return
}
title := ch.Eq(0).Text()
link, _ := ch.Eq(0).Children().Eq(0).Attr("href")
// fix link to point to the pure HTML version of the thread
link = strings.Replace(link, ".com/d/topic", ".com/forum/?_escaped_fragment_=topic", 1)
date := ch.Eq(2).Text()
log.Printf("Thread found: %s %q %s %s\n", link, title, author, date)
mailCollector.Visit(link)
})
// Visit next page
threadCollector.OnHTML("body > a[href]", func(e *colly.HTMLElement) {
log.Println("Next page link found:", e.Attr("href"))
e.Request.Visit(e.Attr("href"))
})
// Extract mails
mailCollector.OnHTML("body", func(e *colly.HTMLElement) {
// Find subject
threadSubject := e.ChildText("h2")
if _, ok := threads[threadSubject]; !ok {
threads[threadSubject] = make([]Mail, 0, 8)
}
// Extract mails
e.DOM.Find("table tr").Each(func(_ int, s *goquery.Selection) {
mailLink := s.Find("td:nth-of-type(1)")
mailHref, _ := s.Attr("href")
mail := Mail{
Title: mailLink.Text(),
Link: mailHref,
Author: s.Find("td:nth-of-type(2)").Text(),
Date: s.Find("td:nth-of-type(3)").Text(),
Message: s.Find("td:nth-of-type(4)").Text(),
}
threads[threadSubject] = append(threads[threadSubject], mail)
})
// Follow next page link
if link, found := e.DOM.Find("> a[href]").Attr("href"); found {
e.Request.Visit(link)
} else {
log.Printf("Thread %q done\n", threadSubject)
}
})
threadCollector.Visit("https://groups.google.com/forum/?_escaped_fragment_=forum/" + groupName)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
// Dump json to the standard output
enc.Encode(threads)
}