Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mpro coleta #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 118 additions & 2 deletions crawler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,122 @@
package main

func Crawl(month int, year int, outputPath string) []string {
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/dadosjusbr/coletores/status"
)

const (
viURLType int = 0
remuURLType int = 1
)

type requestURLs struct {
remunerationURL string
benefitsURL string
}

// Retorna as url para download de cada planilha em questão
func getRequestURLs(year, month int) (requestURLs, error) {
remuIDURL := fmt.Sprint("https://servicos-portal.mpro.mp.br/plcVis/frameset?__report=..%2FROOT%2Frel%2Fcontracheque%2Fmembros%2FremuneracaoMembrosAtivos.rptdesign&anomes=", year, fmt.Sprintf("%02d", month), "&nome=&cargo=&lotacao=")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aplicar o mesmo estilo da sugestão abaixo aqui.

remuSessionID, err := getSessionID(remuIDURL)
if err != nil {
return requestURLs{}, err
}
remuDownloadURL := fmt.Sprintf("%s&__sessionId=%s&__format=xls&__asattachment=true&__overwrite=false", remuIDURL, remuSessionID)

viIDURL := fmt.Sprint("https://servicos-portal.mpro.mp.br/plcVis/frameset?__report=..%2FROOT%2Frel%2Fcontracheque%2Fmembros%2FverbasIndenizatoriasMembrosAtivos.rptdesign&anomes=", year, fmt.Sprintf("%02d", month))
benefitsSessionID, err := getSessionID(viIDURL)
if err != nil {
return requestURLs{}, err
}
benefitsURL := fmt.Sprintf("%s&__sessionId=%s&__format=xls&__asattachment=true&__overwrite=false", viIDURL, benefitsSessionID)

thyagopereira marked this conversation as resolved.
Show resolved Hide resolved
return requestURLs{remuDownloadURL, benefitsURL}, nil
}

// Inicializa o id de sessão para uma dada url
func getSessionID(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
danielfireman marked this conversation as resolved.
Show resolved Hide resolved
return "", status.NewError(status.ConnectionError, fmt.Errorf("Was not possible to get a season id to the url: %s. %q", url, err))
}
defer resp.Body.Close()

page, err := ioutil.ReadAll(resp.Body)
danielfireman marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", status.NewError(status.ConnectionError, fmt.Errorf("Was not possible to get a season id to the url: %s. %q", url, err))
}

id := strings.Split(string(page), "Constants.viewingSessionId = \"")

return id[1][0:19], err
}

func download(url string, filePath string, outputPath string) error {
resp, err := http.Get(url)
if err != nil {
return status.NewError(status.ConnectionError, fmt.Errorf("Problem doing GET on the URL(%s) to download the file(%s). Error: %q", url, filePath, err))
}
defer resp.Body.Close()

_, err = os.Stat(outputPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colocar esse assinalamento no if abaixo

if os.IsNotExist(err) {
err = os.Mkdir(outputPath, 0755)
if err != nil {
return status.NewError(status.SystemError, fmt.Errorf("Error creating outputfolder (%s). Error: %q", outputPath, err))
}
}

file, err := os.Create(filePath)
if err != nil {
return status.NewError(status.DataUnavailable, fmt.Errorf("Error creating downloaded (%s) file(%s). Error: %q", url, filePath, err))
}
defer file.Close()

if _, err := io.Copy(file, resp.Body); err != nil {
return status.NewError(status.SystemError, fmt.Errorf("Was not possible to save the downloaded file: %s. The following mistake was teken: %q", filePath, err))
}

return nil
}

func Crawl(month int, year int, outputPath string) ([]string, error) {
var paths []string
thyagopereira marked this conversation as resolved.
Show resolved Hide resolved
return paths

thyagopereira marked this conversation as resolved.
Show resolved Hide resolved
request, err := getRequestURLs(year, month)
if err != nil {
return paths, err
}

for typ := 0; typ < 2; typ++ {
switch typ {
case remuURLType:
var fileName = fmt.Sprintf("%d_%02d_remu.xls", year, month)
var filePath = fmt.Sprint(outputPath, "/", fileName)

err = download(request.remunerationURL, filePath, outputPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colocar esse assinalamento no if abaixo

if err != nil {
return paths, err
}

paths = append(paths, filePath)
case viURLType:
var fileName = fmt.Sprintf("%d_%02d_vi.xls", year, month)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var fileName = fmt.Sprintf("%d_%02d_vi.xls", year, month)
fileName := fmt.Sprintf("%d_%02d_vi.xls", year, month)

var filePath = fmt.Sprint(outputPath, "/", fileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var filePath = fmt.Sprint(outputPath, "/", fileName)
filePath := fmt.Sprint(outputPath, "/", fileName)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usar filepath.Join


err = download(request.benefitsURL, filePath, outputPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mover assinalamento para dentro do if abaixo

if err != nil {
return paths, err
}

paths = append(paths, filePath)
}
}
return paths, nil
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type Environment struct {
Month int `envconfig:"MONTH" required:"true"`
Year int `envconfig:"YEAR" required:"true"`
OutputFolder string `envconfig:"OUTPUT_FOLDER" default:"/output"`
OutputFolder string `envconfig:"OUTPUT_FOLDER" default:"./output"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover "."

GitCommit string `envconfig:"GIT_COMMIT" required:"true"`
}

Expand Down Expand Up @@ -46,7 +46,10 @@ func main() {
}

// Main execution
fileNames := Crawl(month, year, outputPath)
fileNames, err := Crawl(month, year, outputPath)
if err != nil {
os.Exit(1)
thyagopereira marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usar pacote status

}
thyagopereira marked this conversation as resolved.
Show resolved Hide resolved
employees := Parse(month, year, fileNames)

cr := coletores.ExecutionResult{
Expand All @@ -67,7 +70,6 @@ func main() {
result, err := json.MarshalIndent(cr, "", " ")
if err != nil {
status.ExitFromError(status.NewError(status.SystemError, fmt.Errorf("JSON marshiling error: %q", err)))
os.Exit(1)
}
fmt.Println(string(result))
}