Skip to content

Commit

Permalink
Gets date of the date extraction as updatedAt
Browse files Browse the repository at this point in the history
Closes #199
  • Loading branch information
cuducos committed Apr 21, 2024
1 parent 09994bf commit 8415a00
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions download/federal_revenue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
Expand All @@ -17,13 +18,16 @@ const (
// extracted by the Federal Revenue
FederalRevenueUpdatedAt = "updated_at.txt"

federalRevenueURL = "https://dados.gov.br/api/publico/conjuntos-dados/cadastro-nacional-da-pessoa-juridica---cnpj"
federalRevenueFormat = "zip+csv"
federalRevenueDateFormat = "02/01/2006 15:04:05"
federalRevenueURL = "https://dados.gov.br/api/publico/conjuntos-dados/cadastro-nacional-da-pessoa-juridica---cnpj"
federalRevenueFormat = "zip+csv"
federalRevenueDateFormat = "02/01/2006 15:04:05"
federalRevenueDateFormatNotes = "02/01/2006"

userAgent = "Minha Receita/0.0.1 (minhareceita.org)"
)

var datePattern = regexp.MustCompile(`Data da última extração:? +(?P<updatedAt>\d{2}/\d{2}/\d{4})`)

type federalRevenueTime struct{ Time time.Time }

func (t *federalRevenueTime) UnmarshalJSON(b []byte) error {
Expand All @@ -48,9 +52,18 @@ type federalRevenueResource struct {

type federalRevenueResponse struct {
Resources []federalRevenueResource `json:"resources"`
Notes string `json:"notes"`
}

func (r *federalRevenueResponse) updatedAt() (t time.Time) {
m := datePattern.FindStringSubmatch(r.Notes)
if len(m) == 2 {
t, err := time.Parse(federalRevenueDateFormatNotes, m[1])
if err == nil {
return t
}
}

for _, v := range r.Resources {
if t.Before(v.MetadataModified.Time) {
t = v.MetadataModified.Time
Expand Down
2 changes: 1 addition & 1 deletion download/federal_revenue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestFederalRevenueGetURLs(t *testing.T) {
if err != nil {
t.Errorf("expected no error reading %s, updatedAt %s", pth, err)
}
expected := "2022-11-24"
expected := "2024-04-13"
if string(got) != expected {
t.Errorf("expected updated at to be %s, got %s", expected, string(got))
}
Expand Down
Loading

0 comments on commit 8415a00

Please sign in to comment.