-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
186 lines (151 loc) · 3.73 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"net/http/cookiejar"
"golang.org/x/net/publicsuffix"
"log"
"github.com/PuerkitoBio/goquery"
"strings"
"strconv"
"os"
"io"
"bufio"
"github.com/BurntSushi/toml"
"regexp"
)
type Configuration struct {
Pagestart string
Pageend string
Loginemail string
Loginpasswd string
User string
BackupFilename string
}
func ReadConfig() Configuration {
var configfile = "config.toml"
config := Configuration{}
if _, err := toml.DecodeFile(configfile, &config); err != nil {
log.Fatal(err)
}
return config
}
type Jar struct {
cookies []*http.Cookie
}
func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
jar.cookies = cookies
}
func (jar *Jar) Cookies(u *url.URL) []*http.Cookie {
return jar.cookies
}
func parse() {
var configfile = "config.toml"
var config Configuration
if _, err := toml.DecodeFile(configfile, &config); err != nil {
log.Fatal(err)
}
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
log.Fatal(err)
}
client := http.Client{Jar: jar}
// 判断验证码
resp, err := client.Get("http://fanfou.com/login")
res, _ := ioutil.ReadAll(resp.Body)
r := regexp.MustCompile(`<img src="\/\/(.*?)" width`)
// <img src="\/\/(.*?)" width
body := string(res)
captchAddress := r.FindStringSubmatch(body)[1]
if (1>0) {
_, err := client.PostForm("https://fanfou.com/login", url.Values{
"loginname": {"" + config.Loginemail},
"loginpass": {"" + config.Loginpasswd},
"action": {"login"},
"token": {"42070970"}})
if err != nil {
log.Fatal(err)
}
} else {
// 下载图片并打开
response, err := http.Get("http://" + captchAddress)
if err != nil {
fmt.Println(err)
}
picture, _ := os.Create("captcha.png")
_, err = io.Copy(picture, response.Body)
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
picture.Close()
// TODO 读取用户输入验证码
reader := bufio.NewReader(os.Stdin)
captcha, _ := reader.ReadString('\n')
fmt.Println(captcha)
_, err = client.PostForm("http://fanfou.com/login", url.Values{
"loginname": {"[email protected]"},
"loginpass": {"password"},
"captcha": {captcha},
"action": {"login"},
"token": {"127396ab"}})
if err != nil {
log.Fatal(err)
}
}
start, _ := strconv.Atoi(config.Pagestart)
end, _ := strconv.Atoi(config.Pageend)
count := 0
// 创建文件
f, err := os.Create(config.BackupFilename)
w := bufio.NewWriter(f)
var tweets []string
var timestamps []string
for i := start; i <= end; i++ {
tweets = tweets[:0]
timestamps = timestamps[:0]
resp, err := client.Get(config.User + "/p." + strconv.Itoa(i))
if err != nil {
log.Fatal(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
target := string(data)
fmt.Println(target)
// 进行处理
doc, err := goquery.NewDocumentFromReader(strings.NewReader(target))
if err != nil {
log.Fatal(err)
}
doc.Find("div#content .content").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
tweet := strconv.Itoa(count) + ". " + s.Text()
tweets = append(tweets, tweet)
count += 1
})
doc.Find("div#content .stamp").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
timestamp := s.Text()
timestamps = append(timestamps, timestamp)
})
for index, _ := range tweets {
w.WriteString(tweets[index] + " " + timestamps[index] + "\n---\n")
}
w.Flush()
fmt.Printf("正在处理 %d 页面信息\n", i)
}
fmt.Printf("总共读取 %d 条信息", count)
}
func main() {
parse()
}