import.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "encoding/csv"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "log"
  10. "os"
  11. "github.com/northbright/pathhelper"
  12. "github.com/northbright/xls2csv-go/xls2csv"
  13. "golang.org/x/net/html/charset"
  14. _ "github.com/lib/pq"
  15. )
  16. func importCreditCards(path string) {
  17. f, err := os.Open(path + "/dataimport/cc2.csv")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // Get absolute path of XLS file.
  22. xls, _ := pathhelper.GetAbsPath(path + "/dataimport/cc2.csv")
  23. // Call XLS2CSV() to convert XLS and get all records.
  24. if records, err = xls2csv.XLS2CSV(f, 0); err != nil {
  25. log.Printf("XLS2CSV() error: %v\n", err)
  26. goto end
  27. }
  28. for i, row := range records {
  29. fmt.Printf("%v", row)
  30. if i != len(records)-1 {
  31. fmt.Printf("\n")
  32. }
  33. }
  34. // remember to close the file at the end of the program
  35. defer f.Close()
  36. // read csv values using csv.Reader
  37. csvReader := csv.NewReader(f)
  38. for {
  39. rec, err := csvReader.Read()
  40. if err == io.EOF {
  41. break
  42. }
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. // do something with read line
  47. fmt.Printf("%+v\n", rec[0])
  48. }
  49. }
  50. func importAccount(path string) {
  51. transactions := &AccountTransactions{}
  52. data, err := os.ReadFile(path + "/dataimport/2016-2022.xml")
  53. reader := bytes.NewReader(data)
  54. decoder := xml.NewDecoder(reader)
  55. decoder.CharsetReader = charset.NewReaderLabel
  56. err = decoder.Decode(&transactions)
  57. // Open our xmlFile
  58. // if we os.Open returns an error then handle it
  59. if err != nil {
  60. fmt.Println(err)
  61. }
  62. // defer the closing of our xmlFile so that we can parse it later on
  63. //defer xmlFile.Close()
  64. // read our opened xmlFile as a byte array.
  65. //byteValue, _ := ioutil.ReadAll(xmlFile)
  66. // we initialize our Users array
  67. // we unmarshal our byteArray which contains our
  68. // xmlFiles content into 'users' which we defined above
  69. //xml.Unmarshal([]byte(data), &transactions)
  70. fmt.Println(transactions)
  71. // we iterate through every user within our users array and
  72. // print out the user Type, their name, and their facebook url
  73. // as just an example
  74. for i := 0; i < len(transactions.Transactions); i++ {
  75. fmt.Println("User Type: " + transactions.Transactions[i].ID)
  76. fmt.Println("User Name: " + transactions.Transactions[i].Date)
  77. fmt.Println("Facebook Url: " + transactions.Transactions[i].Amount)
  78. }
  79. }
  80. func main() {
  81. argsWithoutProg := os.Args[1:]
  82. path, err := os.Getwd()
  83. if err != nil {
  84. log.Println(err)
  85. }
  86. fmt.Println("Starting importer on path " + path)
  87. fmt.Println("Program " + argsWithoutProg[0])
  88. switch argsWithoutProg[0] {
  89. case "creditcards":
  90. importCreditCards(path)
  91. break
  92. default:
  93. importAccount(path)
  94. }
  95. fmt.Println("Connecting to database")
  96. psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
  97. "password=%s dbname=%s sslmode=disable",
  98. host, port, user, password, dbname)
  99. db, err := sql.Open("postgres", psqlInfo)
  100. if err != nil {
  101. panic(err)
  102. }
  103. sqlStatement := `
  104. INSERT INTO creditcards.transactions (date, description, exchange_rate, fx_amount,local_amount)
  105. VALUES ('2022-02-02','desc', '1', '1', '1')`
  106. _, err = db.Exec(sqlStatement)
  107. if err != nil {
  108. panic(err)
  109. }
  110. defer db.Close()
  111. }
  112. const (
  113. host = "172.23.0.2"
  114. port = 5432
  115. user = "postgres"
  116. password = "example"
  117. dbname = "books"
  118. )
  119. // our struct which contains the complete
  120. // array of all Users in the file
  121. type AccountTransactions struct {
  122. XMLName xml.Name `xml:"main"`
  123. Transactions []Transaction `xml:"Faerslur"`
  124. }
  125. // the user struct, this contains our
  126. // Type attribute, our user's name and
  127. // a social struct which will contain all
  128. // our social links
  129. type Transaction struct {
  130. XMLName xml.Name `xml:"Faerslur"`
  131. ID string `xml:"Radnumer"`
  132. Date string `xml:"Dagsetning"`
  133. Amount string `xml:"Upphaed"`
  134. }
  135. // a simple struct which contains all our
  136. // social links
  137. type Social struct {
  138. XMLName xml.Name `xml:"social"`
  139. Facebook string `xml:"facebook"`
  140. Twitter string `xml:"twitter"`
  141. Youtube string `xml:"youtube"`
  142. }