fixrbc

Tool to rename RBC statements to yyyy-mm-dd format
git clone https://git.bracken.jp/fixrbc.git
Log | Files | Refs | LICENSE

convert.go (2547B)


      1 // CLI tool to rename all RBC statement PDFs in the current directory to
      2 // YYYY-MM-DD.pdf format.
      3 package convert
      4 
      5 import (
      6 	"fmt"
      7 	"path/filepath"
      8 	"regexp"
      9 	"strings"
     10 )
     11 
     12 type Converter interface {
     13 	Type() string
     14 	CanConvert(fname string) bool
     15 	Convert(fname string) (string, error)
     16 }
     17 
     18 type RbcDirectInvesting struct{}
     19 type AppleCard struct{}
     20 
     21 func ConverterForFile(fname string) (Converter, error) {
     22 	converters := []Converter{&AppleCard{}, &RbcDirectInvesting{}}
     23 	for _, c := range converters {
     24 		if c.CanConvert(fname) {
     25 			return c, nil
     26 		}
     27 	}
     28 	return nil, fmt.Errorf("Unknown statement type: %s", fname)
     29 }
     30 
     31 func (c *RbcDirectInvesting) Type() string {
     32 	return "RBC Direct Investing"
     33 }
     34 
     35 func (c *RbcDirectInvesting) CanConvert(fname string) bool {
     36 	r := regexp.MustCompile(`\d+-\d{4}[A-Za-z]{3}\d\d-\d{4}[A-Za-z]{3}\d\d`)
     37 	return r.MatchString(fname)
     38 }
     39 
     40 // Format: ACCTNO-2020Jan15-2020Feb14.pdf
     41 func (c *RbcDirectInvesting) Convert(fname string) (string, error) {
     42 	date := strings.Split(fname, "-")[2][0:9]
     43 	y := date[0:4]
     44 	m, err := getMonthNumber(strings.ToLower(date[4:7]))
     45 	if err != nil {
     46 		return "", err
     47 	}
     48 	d := date[7:9]
     49 	newFname := fmt.Sprintf("%s-%s-%s.pdf", y, m, d)
     50 	return newFname, nil
     51 }
     52 
     53 func (c *AppleCard) Type() string {
     54 	return "Apple Card"
     55 }
     56 
     57 func (c *AppleCard) CanConvert(fname string) bool {
     58 	return strings.HasPrefix(strings.ToLower(fname), "apple card statement ")
     59 }
     60 
     61 // Format: Apple Card Statement - January 2020.pdf
     62 func (c *AppleCard) Convert(fname string) (string, error) {
     63 	ext := filepath.Ext(fname)
     64 	basename := fname[0 : len(fname)-len(ext)]
     65 	date := strings.Split(basename, " ")[4:6]
     66 	y := date[1]
     67 	m, err := getMonthNumber(strings.ToLower(date[0]))
     68 	if err != nil {
     69 		return "", err
     70 	}
     71 	newFname := fmt.Sprintf("%s-%s.pdf", y, m)
     72 	return newFname, nil
     73 }
     74 
     75 func getMonthNumber(m string) (string, error) {
     76 	switch {
     77 	case m == "jan" || m == "january":
     78 		return "01", nil
     79 	case m == "feb" || m == "february":
     80 		return "02", nil
     81 	case m == "mar" || m == "march":
     82 		return "03", nil
     83 	case m == "apr" || m == "april":
     84 		return "04", nil
     85 	case m == "may":
     86 		return "05", nil
     87 	case m == "jun" || m == "june":
     88 		return "06", nil
     89 	case m == "jul" || m == "july":
     90 		return "07", nil
     91 	case m == "aug" || m == "august":
     92 		return "08", nil
     93 	case m == "sep" || m == "september":
     94 		return "09", nil
     95 	case m == "oct" || m == "october":
     96 		return "10", nil
     97 	case m == "nov" || m == "november":
     98 		return "11", nil
     99 	case m == "dec" || m == "december":
    100 		return "12", nil
    101 	}
    102 	return "", fmt.Errorf("Bad month name: %s", m)
    103 }