-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (151 loc) · 4.05 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
package main
import (
"database/sql"
"github.com/PuerkitoBio/goquery"
"github.com/eyasuyuki/twitter_fund_of_the_year_analyze/config"
"github.com/eyasuyuki/twitter_fund_of_the_year_analyze/report"
"github.com/eyasuyuki/twitter_fund_of_the_year_analyze/tweet"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
"os"
"strings"
)
// extract fund and comment
func fundComment(cf *config.Config, tweet string) (string, string) {
var fund, comment string
if tweet == "" {
return "", ""
}
lines := strings.Split(tweet, "\n")
commentFound := false
for i, line := range lines {
if line == "" {
continue
}
line = strings.TrimSpace(line)
if fund == "" && strings.Index(line, "🥇") > -1 {
fund = lines[i+1]
}
if strings.Index(line, "→") > -1 {
commentFound = true
ls := strings.Split(line, "→")
if len(ls) > 1 && ls[1] != "" && strings.Index(line, cf.PageUrl) == -1 {
comment = ls[1]
}
} else if commentFound && line != "" && strings.Index(line, cf.PageUrl) == -1 {
if comment != "" {
comment = comment + "%0D%0A"
}
comment = comment + line
}
}
return fund, comment
}
const PageTweet = 25
// tweets table
const CreateTweets = `create table tweets (id integer not null primary key, ticker text, twitter_id text, name text, fund text, comment text, tweet_at text)`
const InsertTweets = `insert into tweets (id, ticker, twitter_id, name, fund, comment, tweet_at) values (?, ?, ?, ?, ?, ?, ?)`
// ticker table
const CreateTickers = `create table tickers (ticker not null primary key, name text)`
const SelectTickers = `select * from tickers where ticker = ?`
const InsertTickers = `insert into tickers (ticker, name) values (?, ?)`
func main() {
cf := config.NewConfig("")
urls := []string{cf.TogetterUrl,
cf.TogetterUrl + "?page=2",
cf.TogetterUrl + "?page=3",
cf.TogetterUrl + "?page=4",
}
n := 0
// delete db
os.Remove(cf.DatabaseName)
// open
db, err := sql.Open("sqlite3", cf.DatabaseName)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// create table
_, err = db.Exec(CreateTweets)
if err != nil {
log.Printf("%q: %s\n", err, CreateTweets)
return
}
_, err = db.Exec(CreateTickers)
if err != nil {
log.Printf("%q: %s\n", err, CreateTweets)
return
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
s1, err := tx.Prepare(InsertTweets)
if err != nil {
log.Fatal(err)
}
s2, err := tx.Prepare(InsertTickers)
if err != nil {
log.Fatal(err)
}
tweets := []tweet.Tweet{}
for _, url := range urls {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code err: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// fundComment fund name, comment
doc.Find(".tweet").Each(func(i int, s *goquery.Selection) {
fund, comment := fundComment(cf, s.Text())
t := tweet.Tweet{Fund: fund, Comment: comment}
tweets = append(tweets, t)
})
doc.Find(".user_link").Each(func(i int, s *goquery.Selection) {
name := s.Find("strong").Text()
id := s.Find(".status_name").Text()
if name != "" && id != "" {
tweets[n*PageTweet+i].Name = name
tweets[n*PageTweet+i].Id = id
}
})
doc.Find(".status").Each(func(i int, s *goquery.Selection) {
ts := s.Find("a").Text()
index := n*PageTweet + i
tweets[index].Timestamp = strings.TrimSpace(ts)
// insert
_, err = s1.Exec(index, tweets[index].Ticker(), tweets[index].Id, tweets[index].Name, tweets[index].Fund, tweets[index].Comment, tweets[index].Timestamp)
if err != nil {
log.Fatal(err)
}
rows, err := db.Query(SelectTickers, tweets[index].Ticker())
if err != nil {
log.Fatal(err)
}
defer rows.Close()
if !rows.Next() {
s2.Exec(tweets[index].Ticker(), tweets[index].Fund)
}
})
n = n + 1
}
// commit
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
report.Output(cf, cf.DatabaseName) //TEST
//jsonText, err := json.Marshal(tweets)
//if err != nil {
// log.Fatal(err)
//}
//fmt.Printf("%v", string(jsonText))
}