@ -7,12 +7,13 @@ import (
"fmt"
"fmt"
"hack-browser-data/log"
"hack-browser-data/log"
"hack-browser-data/utils"
"hack-browser-data/utils"
"io"
"os"
"os"
"github.com/gocarina/gocsv "
"github.com/jszwec/csvutil "
)
)
var utf8Bom = [ ] byte { 239 , 187 , 191 }
func ( b BrowserData ) OutPutCsv ( dir , browser , format string ) error {
func ( b BrowserData ) OutPutCsv ( dir , browser , format string ) error {
switch {
switch {
case len ( b . BookmarkSlice ) != 0 :
case len ( b . BookmarkSlice ) != 0 :
@ -20,17 +21,18 @@ func (b BrowserData) OutPutCsv(dir, browser, format string) error {
file , err := os . OpenFile ( filename , os . O_RDWR | os . O_CREATE | os . O_TRUNC | os . O_APPEND , 0644 )
file , err := os . OpenFile ( filename , os . O_RDWR | os . O_CREATE | os . O_TRUNC | os . O_APPEND , 0644 )
defer file . Close ( )
defer file . Close ( )
if err != nil {
if err != nil {
log . Errorf ( "create file %s fail" , filename )
log . Errorf ( "create file %s fail %s" , filename , err )
}
}
gocsv . SetCSVWriter ( func ( out io . Writer ) * gocsv . SafeCSVWriter {
file . Write ( utf8Bom )
writer := csv . NewWriter ( out )
w := csv . NewWriter ( file )
writer . Comma = ' '
w . Comma = ';'
return gocsv . NewSafeCSVWriter ( writer )
enc := csvutil . NewEncoder ( w )
} )
for _ , u := range b . BookmarkSlice {
err = gocsv . MarshalFile ( b . BookmarkSlice , file )
if err := enc . Encode ( u ) ; err != nil {
if err != nil {
log . Error ( err )
log . Error ( err )
}
}
}
w . Flush ( )
fmt . Printf ( "%s Get %d bookmarks, filename is %s \n" , log . Prefix , len ( b . BookmarkSlice ) , filename )
fmt . Printf ( "%s Get %d bookmarks, filename is %s \n" , log . Prefix , len ( b . BookmarkSlice ) , filename )
fallthrough
fallthrough
case len ( b . LoginDataSlice ) != 0 :
case len ( b . LoginDataSlice ) != 0 :
@ -40,10 +42,16 @@ func (b BrowserData) OutPutCsv(dir, browser, format string) error {
if err != nil {
if err != nil {
log . Errorf ( "create file %s fail" , filename )
log . Errorf ( "create file %s fail" , filename )
}
}
err = gocsv . MarshalFile ( b . LoginDataSlice , file )
file . Write ( utf8Bom )
if err != nil {
w := csv . NewWriter ( file )
w . Comma = ';'
enc := csvutil . NewEncoder ( w )
for _ , u := range b . LoginDataSlice {
if err := enc . Encode ( u ) ; err != nil {
log . Error ( err )
log . Error ( err )
}
}
}
w . Flush ( )
fmt . Printf ( "%s Get %d login data, filename is %s \n" , log . Prefix , len ( b . LoginDataSlice ) , filename )
fmt . Printf ( "%s Get %d login data, filename is %s \n" , log . Prefix , len ( b . LoginDataSlice ) , filename )
fallthrough
fallthrough
case len ( b . CookieMap ) != 0 :
case len ( b . CookieMap ) != 0 :
@ -57,10 +65,16 @@ func (b BrowserData) OutPutCsv(dir, browser, format string) error {
for _ , v := range b . CookieMap {
for _ , v := range b . CookieMap {
tempSlice = append ( tempSlice , v ... )
tempSlice = append ( tempSlice , v ... )
}
}
err = gocsv . MarshalFile ( tempSlice , file )
file . Write ( utf8Bom )
if err != nil {
w := csv . NewWriter ( file )
w . Comma = ';'
enc := csvutil . NewEncoder ( w )
for _ , u := range tempSlice {
if err := enc . Encode ( u ) ; err != nil {
log . Error ( err )
log . Error ( err )
}
}
}
w . Flush ( )
fmt . Printf ( "%s Get %d cookies, filename is %s \n" , log . Prefix , len ( b . CookieMap ) , filename )
fmt . Printf ( "%s Get %d cookies, filename is %s \n" , log . Prefix , len ( b . CookieMap ) , filename )
fallthrough
fallthrough
case len ( b . HistorySlice ) != 0 :
case len ( b . HistorySlice ) != 0 :
@ -70,10 +84,16 @@ func (b BrowserData) OutPutCsv(dir, browser, format string) error {
if err != nil {
if err != nil {
log . Errorf ( "create file %s fail" , filename )
log . Errorf ( "create file %s fail" , filename )
}
}
err = gocsv . MarshalFile ( b . HistorySlice , file )
file . Write ( utf8Bom )
if err != nil {
w := csv . NewWriter ( file )
w . Comma = ';'
enc := csvutil . NewEncoder ( w )
for _ , u := range b . HistorySlice {
if err := enc . Encode ( u ) ; err != nil {
log . Error ( err )
log . Error ( err )
}
}
}
w . Flush ( )
fmt . Printf ( "%s Get %d login data, filename is %s \n" , log . Prefix , len ( b . HistorySlice ) , filename )
fmt . Printf ( "%s Get %d login data, filename is %s \n" , log . Prefix , len ( b . HistorySlice ) , filename )
}
}
return nil
return nil