diff --git a/cmd/mtparse/main.go b/cmd/mtparse/main.go index f5f5f87..f1be984 100644 --- a/cmd/mtparse/main.go +++ b/cmd/mtparse/main.go @@ -1,125 +1,31 @@ package main import ( - "bytes" "encoding/json" "fmt" "io/ioutil" - "net/http" + "os" - "github.com/google/uuid" + "gitstore.ru/tolikproh/mchstest/internal/model" ) -const ( - url string = "https://digital.mchs.gov.ru/testing/api/instance/" - uidMchs string = "4e0d6bf6-50f2-4d67-8131-b99b3c4a3f39" -) - -type ResMchsTestType struct { - Id uuid.UUID `json:"id"` - CurrentQuestion CQType `json:"current_question"` - CurrentAnswers []CAType `json:"current_answers"` - QuestionCount int `json:"questions_count"` - QuestionNumber int `json:"question_number"` - QuestionStatus []int `json:"questions_statuses"` - ValidAnswer uuid.UUID `json:"valid_answer"` - QuestionPassed int `json:"questions_passed"` - ValidAnswers string `json:"valid_answers"` -} - -type CQType struct { - Id uuid.UUID `json:"id"` - Content string `json:"content"` - ResourcesPath []string `json:"resources_path"` - Types string `json:"type"` - IsAdd bool `json:"id_additional"` -} - -type CAType struct { - Id uuid.UUID `json:"id"` - Title string `json:"title"` - ResourcesPath []string `json:"resources_path"` -} - -func ResMchsTestToJSON(data *ResMchsTestType) string { - req, _ := json.Marshal(data) - return string(req) -} - -func UnmarshalJSONToType(in []byte) *ResMchsTestType { - var data ResMchsTestType - _ = json.Unmarshal(in, &data) - return &data -} - -type ReqAnswer struct { - Answer uuid.UUID `json:"answer"` -} - -type ReqNull struct { - Answers string `json:"answers"` -} - func main() { - var rN ReqNull - var rA ReqAnswer - body, _ := ReqestToSiteJSON(rN) - fmt.Println("[") - for i := 0; i < 520; i++ { - d := UnmarshalJSONToType(body) - fmt.Print(ResMchsTestToJSON(d), ",\n") - rA.Answer = d.ValidAnswer - body, _ = ReqestToSiteJSON(rA) + var inTest []model.MTType + var outTest []model.MTParseT + bs, err := ioutil.ReadFile("mchs_test.json") + if err != nil { + fmt.Println(err) + os.Exit(1) } - d := UnmarshalJSONToType(body) - fmt.Println(ResMchsTestToJSON(d)) - fmt.Println("]") -} - -func ReqestToSiteJSON(req any) ([]byte, error) { - - reqBodyBytes, err := json.Marshal(req) - if err != nil { - return nil, err - } - - urlr := url + uidMchs - resp, err := http.Post( - urlr, - "application/json", bytes.NewBuffer(reqBodyBytes)) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - return body, nil -} - -func ReqestToSite(req any) (*ResMchsTestType, error) { - - reqBodyBytes, err := json.Marshal(req) - if err != nil { - return nil, err - } - - urlr := url + uidMchs - resp, err := http.Post( - urlr, - "application/json", bytes.NewBuffer(reqBodyBytes)) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - return UnmarshalJSONToType(body), nil + + err = json.Unmarshal(bs, &inTest) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, in := range inTest { + outTest = append(outTest, in.Parse()) + } + fmt.Println(model.SaveToExel("mchs_test.xlsx", &outTest)) } diff --git a/go.mod b/go.mod index 0a51858..c0e9867 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,15 @@ module gitstore.ru/tolikproh/mchstest go 1.19 require github.com/google/uuid v1.3.0 + +require ( + github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect + github.com/richardlehane/mscfb v1.0.4 // indirect + github.com/richardlehane/msoleps v1.0.3 // indirect + github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect + github.com/xuri/excelize/v2 v2.7.0 // indirect + github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect + golang.org/x/crypto v0.5.0 // indirect + golang.org/x/net v0.5.0 // indirect + golang.org/x/text v0.6.0 // indirect +) diff --git a/go.sum b/go.sum index 3dfe1c9..8c18951 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,57 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM= +github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk= +github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= +github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM= +github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c= +github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= +github.com/xuri/excelize/v2 v2.7.0 h1:Hri/czwyRCW6f6zrCDWXcXKshlq4xAZNpNOpdfnFhEw= +github.com/xuri/excelize/v2 v2.7.0/go.mod h1:ebKlRoS+rGyLMyUx3ErBECXs/HNYqyj+PbkkKRK5vSI= +github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M= +github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= +golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= +golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/model/mtparse.go b/internal/model/mtparse.go new file mode 100644 index 0000000..c335b9d --- /dev/null +++ b/internal/model/mtparse.go @@ -0,0 +1,75 @@ +package model + +import ( + "strconv" + "strings" + + excelize "github.com/xuri/excelize/v2" +) + +type MTParseT struct { + QuestionNumber int `json:"question_number"` + QuestionCount int `json:"questions_count"` + ContentQuestion string `json:"content_question"` + Answers []AnswerT `json:"answers"` +} + +type AnswerT struct { + Title string `json:"title"` + Correct bool `json:"correct"` +} + +func (data *MTType) Parse() MTParseT { + var ret MTParseT + + ret.QuestionNumber = data.QuestionNumber + ret.QuestionCount = data.QuestionCount + ret.ContentQuestion = data.CurrentQuestion.Content + answers := strings.Split(data.ValidAnswers, ",") + + for _, d := range data.CurrentAnswers { + var a AnswerT + a.Title = d.Title + for _, as := range answers { + if d.Id.String() == as { + a.Correct = true + } + } + ret.Answers = append(ret.Answers, a) + } + return ret +} + +func SaveToExel(filename string, data *[]MTParseT) error { + f := excelize.NewFile() + defer func() { + if err := f.Close(); err != nil { + return + } + }() + // Create a new sheet. + f.SetCellValue("Sheet1", "A2", "№ п/п") + f.SetCellValue("Sheet1", "B2", "ВОПРОСЫ") + f.SetCellValue("Sheet1", "C2", "Варианты ответа") + f.SetCellValue("Sheet1", "D2", "Результат") + i := 3 + for _, d := range *data { + f.SetCellValue("Sheet1", "A"+strconv.Itoa(i), d.QuestionNumber) + f.SetCellValue("Sheet1", "B"+strconv.Itoa(i), d.ContentQuestion) + for _, a := range d.Answers { + f.SetCellValue("Sheet1", "C"+strconv.Itoa(i), a.Title) + if a.Correct { + f.SetCellValue("Sheet1", "D"+strconv.Itoa(i), "ПРАВИЛЬНЫЙ ОТВЕТ") + } else { + f.SetCellValue("Sheet1", "D"+strconv.Itoa(i), "Ответ не верный") + } + i++ + } + i++ + } + // Save spreadsheet by the given path. + if err := f.SaveAs(filename); err != nil { + return err + } + return nil +} diff --git a/internal/model/mtentity.go b/internal/model/mtsites.go similarity index 100% rename from internal/model/mtentity.go rename to internal/model/mtsites.go diff --git a/mchs_test.xlsx b/mchs_test.xlsx new file mode 100644 index 0000000..72b7a4c Binary files /dev/null and b/mchs_test.xlsx differ diff --git a/mtparse.exe b/mtparse.exe new file mode 100644 index 0000000..12dd771 Binary files /dev/null and b/mtparse.exe differ