@@ -15,6 +15,12 @@ import (
15
15
16
16
var RQuantity * regexp.Regexp = regexp .MustCompile (`^[0-9]+` )
17
17
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
+
18
24
type Summary struct {
19
25
TwitterId string `json:"twitter_id" csv:"twitter_id"`
20
26
CreatedAt time.Time `json:"created_at" csv:"created_at"`
@@ -33,96 +39,76 @@ type Details struct {
33
39
TotalQuantity int `json:"total_quantity" csv:"total_quantity"`
34
40
}
35
41
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 ) {
37
43
lines := replaceLines (strings .Split (text , "\n " ))
38
44
lastWords := lines [len (lines )- 2 ]
39
45
40
46
switch {
41
47
// summary
42
48
case strings .HasSuffix (lastWords , "次へ" ), strings .HasSuffix (lastWords , "Next" ):
43
- csvFile , err = createCsvSummary (twitterId , createdAt , url , lines )
49
+ csvFile , err = tweetInfo . createCsvSummary (lines )
44
50
45
51
// details
46
52
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 )
48
54
}
49
55
return
50
56
}
51
57
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
75
60
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
80
69
}
81
- rLineSplited := strings .Split (rLine , " " )
82
- rLines = append (rLines , rLineSplited ... )
83
70
}
84
- return
85
- }
86
71
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
+ )
89
77
csvName := fmt .Sprintf ("summary_%s.csv" , prefix )
90
78
csvFile , err = ioutil .TempFile ("" , csvName )
91
- defer csvFile .Close ()
92
79
if err != nil {
93
80
return
94
81
}
82
+ defer csvFile .Close ()
95
83
96
- summary := setSummary (twitterId , createdAt , url , lines )
97
84
gocsv .MarshalFile (& summary , csvFile )
98
85
return
99
86
}
100
87
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 ) {
112
89
var totalCaloriesBurned float64 = 0
113
90
var totalDistanceRun float64 = 0
114
91
115
- totalTimeExcercising , _ := time .ParseDuration (replaceTimeUnit (lines [i ]))
92
+ totalTimeExcercising , err := time .ParseDuration (replaceTimeUnit (lines [i ]))
93
+ if err != nil {
94
+ return
95
+ }
116
96
117
97
if strings .HasSuffix (lines [i + 2 ], "kcal" ) {
118
98
totalCaloriesSlice := RQuantity .FindAllString (lines [i + 2 ], 1 )
119
99
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
+ }
121
104
}
122
105
123
106
totalDistanceRunSlice := RQuantity .FindAllString (lines [i + 4 ], 1 )
124
107
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
+ }
126
112
}
127
113
} else {
128
114
// 4分31秒
@@ -133,17 +119,21 @@ func makeSummary(twitterId string, createdAt time.Time, url string, lines []stri
133
119
// 合計走行距離
134
120
totalCaloriesInt := RQuantity .FindAllString (lines [i + 2 ], 1 )[0 ]
135
121
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
+ }
137
126
}
138
127
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 ,
143
132
TotalTimeExcercising : totalTimeExcercising ,
144
133
TotalCaloriesBurned : totalCaloriesBurned ,
145
134
TotalDistanceRun : totalDistanceRun ,
146
135
})
136
+ return
147
137
}
148
138
149
139
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
202
192
203
193
return details
204
194
}
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
+ }
0 commit comments