This repository was archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
523 lines (446 loc) · 17.4 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
xj "github.com/basgys/goxml2json"
"github.com/hancheo/tistory/api"
"github.com/hancheo/tistory/common"
"github.com/hancheo/tistory/conn"
"github.com/hancheo/tistory/crd"
"github.com/hancheo/tistory/insertstockdata"
"github.com/hancheo/tistory/style"
"github.com/joho/godotenv"
"github.com/labstack/echo"
)
type stockData struct {
stockName string // 종목명
personal string // 개인
foreigner string // 외국인
institutional string // 기관계
financial string // 금융투자
insurance string // 보험
investment string // 투신
bank string // 은행
otherFinance string // 기타금융
pension string // 연기금등
privateFund string // 사모펀드
corporations string // 기타법인
}
type writeData struct {
topData api.ContentsStock
buyType string
}
type newPost struct {
Tistory tistory `json:",omitempty"`
}
type tistory struct {
Status string `json:",omitempty"`
PostId string `json:",omitempty"`
Url string `json:",omitempty"`
}
func main() {
godotenv.Load(".env")
e := echo.New()
e.GET("/", authLogin)
e.GET("/api", newContent)
e.GET("/modify", modify)
e.GET("/stockDiscussion", stockDiscussionApi)
e.GET("/api2", newContentMonthly)
e.Logger.Fatal(e.Start(":1323"))
}
// "/" 로 접근시 자동으로 티스토리 인증센터로 redierect
func authLogin(c echo.Context) error {
common.Openbrowser("https://www.tistory.com/oauth/authorize?client_id=" + os.Getenv("CLIENT_ID") + "&redirect_uri=" + os.Getenv("REDIRECT_URL") + "&response_type=code")
return nil
}
// 3일이상 연속 순매수 종목 작성
func continueBuyDaily(c echo.Context) error {
var market, buyType, order string
code := api.GetAccessToken(c.QueryParam("code"))
db, err := conn.DbConn()
common.CheckErr(err)
api.GetContinueBuy(market, buyType, order, db)
tistoryContents := "https://www.tistory.com/apis/post/write"
res, err := http.PostForm(tistoryContents, url.Values{
"access_token": {code},
"postId": {"189"},
"blogName": {os.Getenv("BLOG_NAME")},
"title": {"개인,외국인,기관별 3일 이상 연속 순매수 종목"},
"content": {"종목"},
"category": {"465733"},
"visibility": {"0"},
"tag": {"연속매수, 기관별 주식, 주식매매현황, 기관주식, 외국인주식, 개인주식, 기관매수종목, 기관매도종목, 개인, 외국인, 기관종합, 금융투자, 보험, 투신, 은행, 기타금융, 연기금등, 사모펀드, 기타법인"},
"password": {"12345"},
})
fmt.Println("글 작성 완료")
//get url in response
var e newPost
body, _ := ioutil.ReadAll(res.Body)
xml := strings.NewReader(string(body))
js, _ := xj.Convert(xml)
json.Unmarshal([]byte(js.String()), &e)
//Url
fmt.Println(e.Tistory.Url)
stockDiscussion(e.Tistory.Url, common.GetDateFormat("", "mm", "dd"))
return nil
}
//코스피 코스닥 순서로 진행
//글 수정
func modify(c echo.Context) error {
insertstockdata.CsvInsertToDB("daily")
code := api.GetAccessToken(c.QueryParam("code"))
db, err := conn.DbConn()
common.CheckErr(err)
tistoryContents := "https://www.tistory.com/apis/post/modify"
var m stockData
var keys []string
keys = common.GetStructKeys(m)
str2 := []string{"코스피", "코스닥"}
str3 := []string{"개인", "외국인", "기관종합", "금융투자", "보험", "투신", "은행", "기타금융", "연기금등", "사모펀드", "기타법인"}
month := common.GetDateFormat("", "m", "")
// monthDay := common.GetDateFormat("", "mm", "dd")
monthDay := "0407"
var tableHTML string = ""
tableHTML += style.NavUl()
for idx, k := range keys {
if idx == 0 {
continue
}
if k == "personal" {
tableHTML += "<div class='tab active'>"
} else {
tableHTML += "<div class='tab'>"
}
for i, s := range str2 {
data := api.GetStockDataDaily(strconv.Itoa(i), k, "desc", monthDay, db)
data2 := api.GetStockDataDaily(strconv.Itoa(i), k, "", monthDay, db)
fmt.Println(s + " " + str3[idx-1] + " 상위 매수/매도 50 종목 가져오기 완료")
tableHTML += "<h3>" + str3[idx-1] + "_" + s + "<h3>"
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #ef5369;'>순매수 종목</h1>"
tableHTML += style.DataToHTML(data)
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #006dd7'>순매도 종목</h1>"
tableHTML += style.DataToHTML(data2)
}
tableHTML += "</div>"
}
_, err1 := http.PostForm(tistoryContents, url.Values{
"access_token": {code},
"postId": {"203"},
"blogName": {os.Getenv("BLOG_NAME")},
"title": {"2021년 " + monthDay[:2] + "월 " + monthDay[2:] + "일 개인,외국인,기관별 순매수/매도 상위 50 종목"},
"content": {style.BasicTheme(tableHTML, monthDay, "daily")},
"category": {"465734"}, // monthly 465733
"visibility": {"3"},
"tag": {month + "월 주식, 기관별 주식, 주식매매현황, 기관주식, 외국인주식, 개인주식, 기관매수종목, 기관매도종목, 개인, 외국인, 기관종합, 금융투자, 보험, 투신, 은행, 기타금융, 연기금등, 사모펀드, 기타법인"},
})
common.CheckErr(err1)
return c.String(http.StatusOK, code)
}
//일간 글 작성 (매수 상위 50종목)
func newContent(c echo.Context) error {
// dkosdaqcost, dkosdaqstock, dkospicost, dkospistock
insertstockdata.CodeNameCsv()
insertstockdata.CsvInsertToDB("daily")
fmt.Println("일간 데이터 입력 완료")
code := api.GetAccessToken(c.QueryParam("code"))
db, err := conn.DbConn()
common.CheckErr(err)
tistoryContents := "https://www.tistory.com/apis/post/write"
var m stockData
var keys []string
keys = common.GetStructKeys(m)
str2 := []string{"코스피", "코스닥"}
str3 := []string{"개인", "외국인", "기관종합", "금융투자", "보험", "투신", "은행", "기타금융", "연기금등", "사모펀드", "기타법인"}
month := common.GetDateFormat("", "m", "")
monthDay := common.GetDateFormat("", "mm", "dd")
var tableHTML string = ""
tableHTML += style.NavUl()
for idx, k := range keys {
if idx == 0 {
continue
}
if k == "personal" {
tableHTML += "<div class='tab active'>"
} else {
tableHTML += "<div class='tab'>"
}
for i, s := range str2 {
data := api.GetStockDataDaily(strconv.Itoa(i), k, "desc", monthDay, db)
data2 := api.GetStockDataDaily(strconv.Itoa(i), k, "", monthDay, db)
fmt.Println(s + " " + str3[idx-1] + " 상위 매수/매도 50 종목 가져오기 완료")
tableHTML += "<h3>" + str3[idx-1] + "_" + s + "<h3>"
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #ef5369;'>순매수 종목</h1>"
tableHTML += style.DataToHTML(data)
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #006dd7'>순매도 종목</h1>"
tableHTML += style.DataToHTML(data2)
}
tableHTML += "</div>"
}
resp2, err1 := http.PostForm(tistoryContents, url.Values{
"access_token": {code},
"blogName": {os.Getenv("BLOG_NAME")},
"title": {"2021년 " + monthDay[:2] + "월 " + monthDay[2:] + "일 개인,외국인,기관별 순매수/매도 상위 50 종목"},
"content": {style.BasicTheme(tableHTML, monthDay, "daily")},
"category": {"465734"}, // monthly 465733
"visibility": {"3"}, // 0: 비공개 1: 보호글 3: 공개글
"tag": {month + "월 주식, 기관별 주식, 주식매매현황, 기관주식, 외국인주식, 개인주식, 기관매수종목, 기관매도종목, 개인, 외국인, 기관종합, 금융투자, 보험, 투신, 은행, 기타금융, 연기금등, 사모펀드, 기타법인"},
})
common.CheckErr(err1)
defer resp2.Body.Close()
fmt.Println("글 작성 완료")
//get url in response
var e newPost
body, _ := ioutil.ReadAll(resp2.Body)
xml := strings.NewReader(string(body))
js, _ := xj.Convert(xml)
json.Unmarshal([]byte(js.String()), &e)
//Url
fmt.Println(e.Tistory.Url)
stockDiscussion(e.Tistory.Url, monthDay)
return c.String(http.StatusOK, code)
}
//월간 글 작성 (매수 상위 100종목)
func newContentMonthly(c echo.Context) error {
// dkosdaqcost, dkosdaqstock, dkospicost, dkospistock
insertstockdata.CsvInsertToDB("monthly")
fmt.Println("월간 데이터 입력 완료")
code := api.GetAccessToken(c.QueryParam("code"))
db, err := conn.DbConn()
common.CheckErr(err)
tistoryContents := "https://www.tistory.com/apis/post/write"
var m stockData
var keys []string
keys = common.GetStructKeys(m)
str2 := []string{"코스피", "코스닥"}
str3 := []string{"개인", "외국인", "기관종합", "금융투자", "보험", "투신", "은행", "기타금융", "연기금등", "사모펀드", "기타법인"}
month := common.GetDateFormat("", "m", "")
monthDay := common.GetDateFormat("", "mm", "dd")
var tableHTML string = ""
tableHTML += style.NavUl()
for idx, k := range keys {
if idx == 0 {
continue
}
if k == "personal" {
tableHTML += "<div class='tab active'>"
} else {
tableHTML += "<div class='tab'>"
}
for i, s := range str2 {
data := api.GetStockDataMonthly(strconv.Itoa(i), k, "desc", monthDay, db)
data2 := api.GetStockDataMonthly(strconv.Itoa(i), k, "", monthDay, db)
fmt.Println(s + " " + str3[idx-1] + " 상위 매수/매도 100 종목 가져오기 완료")
tableHTML += "<h3>" + str3[idx-1] + "_" + s + "<h3>"
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #ef5369;'>순매수 종목</h1>"
tableHTML += style.DataToHTML(data)
tableHTML += "<h1 style='font-weight: bold; text-aling:center; color: #006dd7'>순매도 종목</h1>"
tableHTML += style.DataToHTML(data2)
}
tableHTML += "</div>"
}
resp2, err1 := http.PostForm(tistoryContents, url.Values{
"access_token": {code},
"blogName": {os.Getenv("BLOG_NAME")},
"title": {"2021년 " + monthDay[:2] + "월 개인,외국인,기관별 순매수/매도 상위 100 종목"},
"content": {style.BasicTheme(tableHTML, monthDay, "monthly")},
"category": {"465733"},
"visibility": {"3"},
"tag": {month + "월 주식, 기관별 주식, 주식매매현황, 기관주식, 외국인주식, 개인주식, 기관매수종목, 기관매도종목, 개인, 외국인, 기관종합, 금융투자, 보험, 투신, 은행, 기타금융, 연기금등, 사모펀드, 기타법인"},
})
common.CheckErr(err1)
defer resp2.Body.Close()
fmt.Println("글 작성 완료")
//get url in response
var e newPost
body, _ := ioutil.ReadAll(resp2.Body)
xml := strings.NewReader(string(body))
js, _ := xj.Convert(xml)
json.Unmarshal([]byte(js.String()), &e)
fmt.Println(e.Tistory.Url)
stockDiscussionMonthly(e.Tistory.Url, monthDay)
return c.String(http.StatusOK, code)
}
func stockDiscussionApi(c echo.Context) error {
stockDiscussionMonthly(os.Getenv("BLOG_URL"), common.GetDateFormat("", "mm", "dd"))
return nil
}
//네이버 종목토론방 글 작성 (각 매수주체별 매매 상위 1등 종목에 한함.)
func stockDiscussion(url, monthDay string) error {
// stockName string
db, err := conn.DbConn()
common.CheckErr(err)
str2 := []string{"코스피", "코스닥"}
str3 := []string{"개인", "외국인", "기관종합", "금융투자", "보험", "투신", "은행", "기타금융", "연기금", "사모펀드", "기타법인"}
var keys []string
var m stockData
var tmp [4][]writeData
keys = common.GetStructKeys(m)
for idx := range str2 {
for i, s := range keys {
if i == 0 {
continue
}
var tmpType writeData
tmpType.topData = api.GetTopDataDaily(strconv.Itoa(idx), s, "desc", monthDay, db)
tmpType.buyType = str3[i-1]
if idx == 0 {
tmp[0] = append(tmp[0], tmpType) // 코스피 s 매수 상위 1
fmt.Println("Append 0 : " + str3[i-1] + " 코스피 매수")
tmpType.topData = api.GetTopDataDaily(strconv.Itoa(idx), s, "", monthDay, db)
tmp[1] = append(tmp[1], tmpType) // 코스피 s 매도 상위 1
fmt.Println("Append 1 : " + str3[i-1] + " 코스피 매도")
} else {
tmp[2] = append(tmp[2], tmpType) // 코스닥 s 매수 상위 1
fmt.Println("Append 2 : " + str3[i-1] + " 코스닥 매수")
tmpType.topData = api.GetTopDataDaily(strconv.Itoa(idx), s, "", monthDay, db)
tmp[3] = append(tmp[3], tmpType) // 코스닥 s 매도 상위 1
fmt.Println("Append 3 : " + str3[i-1] + " 코스닥 매도")
}
}
}
var title string
var content string
var sN string
for i, s := range tmp {
for j := 0; j < len(s); j++ {
if s[j].buyType == "" {
continue
} else {
sN = s[j].topData.StockName
if i%2 == 0 {
content = sN + ` 금일 종목 주체별 매수 정보 !`
} else {
content = sN + ` 금일 종목 주체별 매도 정보 !`
}
title = sN + " " + s[j].buyType
tmp1, _ := strconv.ParseInt(s[j].topData.CostVolume, 10, 64)
tmp2, _ := strconv.ParseInt(s[j].topData.CountVolume, 10, 64)
tmp3, _ := strconv.ParseInt(s[j].topData.OneStock, 10, 64)
content += `\n\n` + s[j].buyType + ` : \n 금액(100만 단위) : ` + common.Comma(tmp1) + `\n 수량(1 단위) : ` + common.Comma(tmp2) + `\n 평균단가 : ` + common.Comma(tmp3)
}
// 같은 종목이 있으면 내용 추가
for k := j + 1; k < len(s); k++ {
if s[k].topData.StockName == sN {
title += `, ` + s[k].buyType
tmp1, _ := strconv.ParseInt(s[k].topData.CostVolume, 10, 64)
tmp2, _ := strconv.ParseInt(s[k].topData.CountVolume, 10, 64)
tmp3, _ := strconv.ParseInt(s[k].topData.OneStock, 10, 64)
content += `\n\n` + s[k].buyType + ` : \n 금액(100만 단위) : ` + common.Comma(tmp1) + `\n 수량(1 단위) : ` + common.Comma(tmp2) + `\n 평균단가 : ` + common.Comma(tmp3)
s[k].buyType = ""
}
}
if i < 2 {
if i == 0 {
title += " 코스피 종목중 순매수 1위 !!"
} else {
title += " 코스피 종목중 순매도 1위 !!"
}
} else {
if i == 2 {
title += " 코스닥 종목중 순매수 1위 !!"
} else {
title += " 코스닥 종목중 순매도 1위 !!"
}
}
content += `\n\n금일 각 매수주체별 상위 50종목 매매 정보 블로그\n`
content += os.Getenv("BLOG_URL")
crd.WriteStockDiscussion(i, sN, title, content)
fmt.Println(title)
}
}
return nil
}
//네이버 종목토론방 월간 글 작성 (각 매수주체별 매매 상위 1등 종목에 한함.)
func stockDiscussionMonthly(url, tmpDate string) error {
// stockName string
db, err := conn.DbConn()
common.CheckErr(err)
str2 := []string{"코스피", "코스닥"}
str3 := []string{"개인", "외국인", "기관종합", "금융투자", "보험", "투신", "은행", "기타금융", "연기금", "사모펀드", "기타법인"}
var keys []string
var m stockData
var tmp [4][]writeData
keys = common.GetStructKeys(m)
for idx := range str2 {
for i, s := range keys {
if i == 0 {
continue
}
var tmpType writeData
tmpType.topData = api.GetTopDataMonthly(strconv.Itoa(idx), s, "desc", tmpDate, db)
tmpType.buyType = str3[i-1]
if idx == 0 {
tmp[0] = append(tmp[0], tmpType) // 코스피 s 매수 상위 1
fmt.Println("Append 0 : 코스피 매수")
tmpType.topData = api.GetTopDataMonthly(strconv.Itoa(idx), s, "", tmpDate, db)
tmpType.buyType = str3[i-1]
tmp[1] = append(tmp[1], tmpType) // 코스피 s 매도 상위 1
fmt.Println("Append 1 : 코스피 매도")
} else {
tmp[2] = append(tmp[2], tmpType) // 코스닥 s 매수 상위 1
fmt.Println("Append 2 : 코스닥 매수")
tmpType.topData = api.GetTopDataMonthly(strconv.Itoa(idx), s, "", tmpDate, db)
tmpType.buyType = str3[i-1]
tmp[3] = append(tmp[3], tmpType) // 코스닥 s 매도 상위 1
fmt.Println("Append 3 : 코스닥 매도")
}
fmt.Println(i)
}
}
var title string
var content string
var sN string
for i, s := range tmp {
for j := 0; j < len(s); j++ {
if s[j].buyType == "" {
continue
} else {
sN = s[j].topData.StockName
if i%2 == 0 {
content = sN + ` 월간 종목 주체별 매수 정보 !`
} else {
content = sN + ` 월간 종목 주체별 매도 정보 !`
}
title = sN + " " + s[j].buyType
tmp1, _ := strconv.ParseInt(s[j].topData.CostVolume, 10, 64)
tmp2, _ := strconv.ParseInt(s[j].topData.CountVolume, 10, 64)
tmp3, _ := strconv.ParseInt(s[j].topData.OneStock, 10, 64)
content += `\n\n` + s[j].buyType + ` : \n 금액(100만 단위) : ` + common.Comma(tmp1) + `\n 수량(1 단위) : ` + common.Comma(tmp2) + `\n 평균단가 : ` + common.Comma(tmp3)
}
// 같은 종목이 있으면 내용 추가
for k := j + 1; k < len(s); k++ {
if s[k].topData.StockName == sN {
title += `, ` + s[k].buyType
tmp1, _ := strconv.ParseInt(s[k].topData.CostVolume, 10, 64)
tmp2, _ := strconv.ParseInt(s[k].topData.CountVolume, 10, 64)
tmp3, _ := strconv.ParseInt(s[k].topData.OneStock, 10, 64)
content += `\n\n` + s[k].buyType + ` : \n 금액(100만 단위) : ` + common.Comma(tmp1) + `\n 수량(1 단위) : ` + common.Comma(tmp2) + `\n 평균단가 : ` + common.Comma(tmp3)
s[k].buyType = ""
}
}
if i < 2 {
if i == 0 {
title += " 월간 코스피 종목중 순매수 1위 !!"
} else {
title += " 월간 코스피 종목중 순매도 1위 !!"
}
} else {
if i == 2 {
title += " 월간 코스닥 종목중 순매수 1위 !!"
} else {
title += " 월간 코스닥 종목중 순매도 1위 !!"
}
}
content += `\n\n월간 각 매수주체별 상위 100종목 매매 정보 블로그\n`
content += url
crd.WriteStockDiscussion(i, sN, title, content)
}
}
return nil
}