Skip to content

Commit a89a756

Browse files
committed
change createCsvSummary to use struct
1 parent f782bea commit a89a756

File tree

4 files changed

+94
-60
lines changed

4 files changed

+94
-60
lines changed

bq/csv.go

+82-57
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515

1616
var RQuantity *regexp.Regexp = regexp.MustCompile(`^[0-9]+`)
1717

18+
type TweetInfo struct {
19+
TwitterId string `json:"twitter_id" csv:"twitter_id"`
20+
CreatedAt time.Time `json:"created_at" csv:"created_at"`
21+
ImageUrl string `json:"image_url" csv:"image_url"`
22+
}
23+
1824
type Summary struct {
1925
TwitterId string `json:"twitter_id" csv:"twitter_id"`
2026
CreatedAt time.Time `json:"created_at" csv:"created_at"`
@@ -33,96 +39,76 @@ type Details struct {
3339
TotalQuantity int `json:"total_quantity" csv:"total_quantity"`
3440
}
3541

36-
func CreateCsv(twitterId string, createdAt time.Time, url string, text string) (csvFile *os.File, err error) {
42+
func (tweetInfo *TweetInfo) CreateCsv(text string) (csvFile *os.File, err error) {
3743
lines := replaceLines(strings.Split(text, "\n"))
3844
lastWords := lines[len(lines)-2]
3945

4046
switch {
4147
// summary
4248
case strings.HasSuffix(lastWords, "次へ"), strings.HasSuffix(lastWords, "Next"):
43-
csvFile, err = createCsvSummary(twitterId, createdAt, url, lines)
49+
csvFile, err = tweetInfo.createCsvSummary(lines)
4450

4551
// details
4652
case strings.HasSuffix(lastWords, "とじる"), strings.HasSuffix(lastWords, "Close"):
47-
csvFile, err = createCsvDetails(twitterId, createdAt, url, lines)
53+
csvFile, err = createCsvDetails(tweetInfo.TwitterId, tweetInfo.CreatedAt, tweetInfo.ImageUrl, lines)
4854
}
4955
return
5056
}
5157

52-
func replaceTimeUnit(strTotalTime string) string {
53-
replaceTimeUnit := [][]string{
54-
{"時", "h"},
55-
{"分", "m"},
56-
{"秒", "s"},
57-
}
58-
59-
for _, unit := range replaceTimeUnit {
60-
strTotalTime = strings.ReplaceAll(strTotalTime, unit[0], unit[1])
61-
}
62-
return strTotalTime
63-
}
64-
65-
func replaceLines(lines []string) (rLines []string) {
66-
replaceStr2d := [][]string{
67-
{"Om(", "0m("},
68-
{"0(", "回("},
69-
{"押しにみ", "押しこみ"},
70-
{"スクワフット", "スクワット"},
71-
{"- ", ""},
72-
{" m", "m"},
73-
{"Im(", "1m("},
74-
}
58+
func (tweetInfo *TweetInfo) createCsvSummary(lines []string) (csvFile *os.File, err error) {
59+
var summary []*Summary
7560

76-
for _, line := range lines {
77-
rLine := strings.TrimSpace(strings.Trim(line, "*"))
78-
for _, replaceStr := range replaceStr2d {
79-
rLine = strings.Replace(rLine, replaceStr[0], replaceStr[1], 1)
61+
for i, line := range lines {
62+
// fmt.Println(line)
63+
if RQuantity.MatchString(line) {
64+
summary, err = tweetInfo.setSummary(lines, i)
65+
if err != nil {
66+
return
67+
}
68+
break
8069
}
81-
rLineSplited := strings.Split(rLine, " ")
82-
rLines = append(rLines, rLineSplited...)
8370
}
84-
return
85-
}
8671

87-
func createCsvSummary(twitterId string, createdAt time.Time, url string, lines []string) (csvFile *os.File, err error) {
88-
prefix := strings.ReplaceAll(filepath.Base(url), filepath.Ext(url), "")
72+
prefix := strings.ReplaceAll(
73+
filepath.Base(tweetInfo.ImageUrl),
74+
filepath.Ext(tweetInfo.ImageUrl),
75+
"",
76+
)
8977
csvName := fmt.Sprintf("summary_%s.csv", prefix)
9078
csvFile, err = ioutil.TempFile("", csvName)
91-
defer csvFile.Close()
9279
if err != nil {
9380
return
9481
}
82+
defer csvFile.Close()
9583

96-
summary := setSummary(twitterId, createdAt, url, lines)
9784
gocsv.MarshalFile(&summary, csvFile)
9885
return
9986
}
10087

101-
func setSummary(twitterId string, createdAt time.Time, url string, lines []string) (summary []*Summary) {
102-
for i, line := range lines {
103-
if RQuantity.MatchString(line) {
104-
summary = makeSummary(twitterId, createdAt, url, lines, i)
105-
break
106-
}
107-
}
108-
return
109-
}
110-
111-
func makeSummary(twitterId string, createdAt time.Time, url string, lines []string, i int) (summary []*Summary) {
88+
func (tweetInfo *TweetInfo) setSummary(lines []string, i int) (summary []*Summary, err error) {
11289
var totalCaloriesBurned float64 = 0
11390
var totalDistanceRun float64 = 0
11491

115-
totalTimeExcercising, _ := time.ParseDuration(replaceTimeUnit(lines[i]))
92+
totalTimeExcercising, err := time.ParseDuration(replaceTimeUnit(lines[i]))
93+
if err != nil {
94+
return
95+
}
11696

11797
if strings.HasSuffix(lines[i+2], "kcal") {
11898
totalCaloriesSlice := RQuantity.FindAllString(lines[i+2], 1)
11999
if len(totalCaloriesSlice) > 0 {
120-
totalCaloriesBurned, _ = strconv.ParseFloat(totalCaloriesSlice[0], 64)
100+
totalCaloriesBurned, err = strconv.ParseFloat(totalCaloriesSlice[0], 64)
101+
if err != nil {
102+
return
103+
}
121104
}
122105

123106
totalDistanceRunSlice := RQuantity.FindAllString(lines[i+4], 1)
124107
if len(totalDistanceRunSlice) > 0 {
125-
totalDistanceRun, _ = strconv.ParseFloat(totalDistanceRunSlice[0], 64)
108+
totalDistanceRun, err = strconv.ParseFloat(totalDistanceRunSlice[0], 64)
109+
if err != nil {
110+
return
111+
}
126112
}
127113
} else {
128114
// 4分31秒
@@ -133,17 +119,21 @@ func makeSummary(twitterId string, createdAt time.Time, url string, lines []stri
133119
// 合計走行距離
134120
totalCaloriesInt := RQuantity.FindAllString(lines[i+2], 1)[0]
135121
totalCaloriesFract := RQuantity.FindAllString(lines[i+4], 1)[0]
136-
totalCaloriesBurned, _ = strconv.ParseFloat(totalCaloriesInt+totalCaloriesFract, 64)
122+
totalCaloriesBurned, err = strconv.ParseFloat(totalCaloriesInt+totalCaloriesFract, 64)
123+
if err != nil {
124+
return
125+
}
137126
}
138127

139-
return append(summary, &Summary{
140-
TwitterId: twitterId,
141-
CreatedAt: createdAt,
142-
ImageUrl: url,
128+
summary = append(summary, &Summary{
129+
TwitterId: tweetInfo.TwitterId,
130+
CreatedAt: tweetInfo.CreatedAt,
131+
ImageUrl: tweetInfo.ImageUrl,
143132
TotalTimeExcercising: totalTimeExcercising,
144133
TotalCaloriesBurned: totalCaloriesBurned,
145134
TotalDistanceRun: totalDistanceRun,
146135
})
136+
return
147137
}
148138

149139
func createCsvDetails(twitterId string, createdAt time.Time, url string, lines []string) (csvFile *os.File, err error) {
@@ -202,3 +192,38 @@ func setDetails(details []*Details, twitterId string, createdAt time.Time, url s
202192

203193
return details
204194
}
195+
196+
func replaceTimeUnit(strTotalTime string) string {
197+
replaceTimeUnit := [][]string{
198+
{"時", "h"},
199+
{"分", "m"},
200+
{"秒", "s"},
201+
}
202+
203+
for _, unit := range replaceTimeUnit {
204+
strTotalTime = strings.ReplaceAll(strTotalTime, unit[0], unit[1])
205+
}
206+
return strTotalTime
207+
}
208+
209+
func replaceLines(lines []string) (rLines []string) {
210+
replaceStr2d := [][]string{
211+
{"Om(", "0m("},
212+
{"0(", "回("},
213+
{"押しにみ", "押しこみ"},
214+
{"スクワフット", "スクワット"},
215+
{"- ", ""},
216+
{" m", "m"},
217+
{"Im(", "1m("},
218+
}
219+
220+
for _, line := range lines {
221+
rLine := strings.TrimSpace(strings.Trim(line, "*"))
222+
for _, replaceStr := range replaceStr2d {
223+
rLine = strings.Replace(rLine, replaceStr[0], replaceStr[1], 1)
224+
}
225+
rLineSplited := strings.Split(rLine, " ")
226+
rLines = append(rLines, rLineSplited...)
227+
}
228+
return
229+
}

search/search.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ func detecter(projectID string, twitterId string, createdAt time.Time, url strin
112112
return
113113
}
114114

115-
csvFile, err := bq.CreateCsv(twitterId, createdAt, url, text)
116-
if err == nil {
115+
tweetInfo := bq.TweetInfo{
116+
TwitterId: twitterId,
117+
CreatedAt: createdAt,
118+
ImageUrl: url,
119+
}
120+
csvFile, err := tweetInfo.CreateCsv(text)
121+
if err != nil {
117122
log.Fatal(err)
118123
}
119124

twitter/twitter.go

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (cfg *CfgList) Search(user *string, count int, lastExecutedAt time.Time) (r
7070
if err != nil {
7171
return
7272
}
73+
fmt.Println("Search results:")
74+
fmt.Println(searchResult)
7375

7476
for _, tweet := range searchResult.Statuses {
7577
var urls []string

vision_texts/vision_texts.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func Detect(filename string) string {
4343
result := annotations[0].Description
4444
if contains(strings.Split(result, "\n"), "本日の運動結果") ||
4545
contains(strings.Split(result, "\n"), "Today's Results") {
46-
// fmt.Println(result)
46+
47+
fmt.Printf("Detect results: %s\n", filename)
48+
fmt.Println(result)
4749
return result
4850
}
4951

0 commit comments

Comments
 (0)