This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwavve.go
206 lines (177 loc) · 6.38 KB
/
wavve.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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/bytedance/sonic"
"github.com/chromedp/chromedp"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/bson"
)
func getWavveAccount(id, pw string) (*account, error) {
ctx, cancel := newChromedp()
defer cancel()
var account account
account.Id = id
account.Pw = pw
if err := wavveLogin(ctx, id, pw); err != nil {
return nil, err
}
defer wavveLogout(ctx)
var contents string
if err := chromedp.Run(
*ctx,
chromedp.Navigate(`https://www.wavve.com/my/subscription_ticket`),
chromedp.Text(`#contents`, &contents, chromedp.NodeVisible),
); err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
if contents == "이용권 결제 내역이 없어요." {
account.Payment = payment{}
account.Membership = membership{MEMBERSHIP_TYPE_NO, MEMBERSHIP_COST_NO}
return &account, nil
}
var rawPaymentType, rawPaymentNext, rawMembershipType, rawMembershipCost string
if err := chromedp.Run(
*ctx,
chromedp.Text(`#contents > div.mypooq-inner-wrap > section > div > div > div > table > tbody > tr > td:nth-child(6) > span > span`, &rawPaymentType, chromedp.NodeVisible),
chromedp.Text(`#contents > div.mypooq-inner-wrap > section > div > div > div > table > tbody > tr > td:nth-child(5)`, &rawPaymentNext, chromedp.NodeVisible),
chromedp.Text(`#contents > div.mypooq-inner-wrap > section > div > div > div > table > tbody > tr > td:nth-child(2) > div > p.my-pay-tit > span:nth-child(3)`, &rawMembershipType, chromedp.NodeVisible),
chromedp.Text(`#contents > div.mypooq-inner-wrap > section > div > div > div > table > tbody > tr > td:nth-child(4)`, &rawMembershipCost, chromedp.NodeVisible),
); err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var (
dummy string
year, month, day int
)
if _, err := fmt.Sscanf(strings.Split(rawPaymentNext, " ")[0], "%d-%d-%d", &year, &month, &day); err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
account.Payment = payment{
Type: rawPaymentType,
Next: time.Date(year, time.Month(month), day+1, 0, 0, 0, 0, time.FixedZone("KST", 9*60*60)).Unix(),
}
rawMembershipCost = strings.ReplaceAll(rawMembershipCost, ",", "")
if _, err := fmt.Sscanf(rawMembershipCost, "%d%s", &account.Membership.Cost, &dummy); err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
switch rawMembershipType {
case "Basic":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_BASIC
case "Standard":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_STANDARD
case "Premium":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_PREMIUM
case "Basic X FLO 무제한":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_FLO
case "Basic X Bugs 듣기":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_BUGS
case "Basic X KB 나라사랑카드":
account.Membership.Type = MEMBERSHIP_WAVVE_TYPE_KB
default:
return nil, fiber.NewError(fiber.StatusInternalServerError, rawMembershipType)
}
return &account, nil
}
func wavveLogin(c *context.Context, id, pw string) error {
if len(id) < 1 || len(pw) < 1 {
return fiber.ErrBadRequest
}
var url, msg string
if err := chromedp.Run(
*c,
chromedp.Navigate(`https://www.wavve.com/login`),
chromedp.Click(`input[title="아이디"]`, chromedp.NodeVisible),
chromedp.SendKeys(`input[title="아이디"]`, id, chromedp.NodeVisible),
chromedp.Click(`input[title="비밀번호"]`, chromedp.NodeVisible),
chromedp.SendKeys(`input[title="비밀번호"]`, pw, chromedp.NodeVisible),
chromedp.Click(`a[title="로그인"]`, chromedp.NodeVisible),
chromedp.Sleep(1*time.Second),
chromedp.Location(&url),
); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
if url == "https://www.wavve.com/login" {
if err := chromedp.Run(
*c,
chromedp.Text(`p[class="login-error-red"]`, &msg, chromedp.NodeVisible),
); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return fiber.NewError(fiber.StatusBadRequest, msg)
}
return nil
}
func wavveLogout(c *context.Context) error {
return chromedp.Run(
*c,
chromedp.Navigate(`https://www.wavve.com`),
chromedp.Click(`#app > div.body > div:nth-child(2) > header > div:nth-child(1) > div.header-nav > div > ul > li.over-parent-1depth > div > ul > li:nth-child(4) > button`, chromedp.NodeVisible),
)
}
func postWavveAccount(c *fiber.Ctx) error {
var parser struct {
OttId string `json:"ott_id"`
OttPw string `json:"ott_pw"`
}
if err := c.BodyParser(&parser); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if parser.OttId == "" || parser.OttPw == "" {
return fiber.ErrBadRequest
}
account, err := getWavveAccount(parser.OttId, parser.OttPw)
if err != nil {
return err
}
bodyByte, err := sonic.Marshal(&account)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.Send(bodyByte)
}
func putWavveAccount(c *fiber.Ctx) error {
client, ctx, cancel, err := newClient()
if err != nil {
return err
}
defer cancel()
var parser struct {
OttId string `json:"ott_id" bson:"ott_id"`
OttPw string `json:"ott_pw" bson:"ott_pw"`
}
if err := c.BodyParser(&parser); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if parser.OttId == "" || parser.OttPw == "" {
return fiber.ErrBadRequest
}
var group group
filter := bson.M{"ott": "wavve", "account.id": parser.OttId, "account.pw": parser.OttPw}
if err := getCollection(client, "group").FindOne(ctx, filter).Decode(&group); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
account, err := getWavveAccount(parser.OttId, parser.OttPw)
if err != nil {
return err
}
if group.Account != *account {
newAccountBsonByte, err := bson.Marshal(account)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var newAccountBson bson.M
if err = bson.Unmarshal(newAccountBsonByte, &newAccountBson); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
update := bson.M{"$set": bson.M{"account": newAccountBson}}
getCollection(client, "group").FindOneAndUpdate(ctx, filter, update)
}
accountByte, err := sonic.Marshal(account)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.Send(accountByte)
}