Created
December 27, 2018 23:39
-
-
Save Adron/376904c39ea5c11ef5a0dcc87de317ab to your computer and use it in GitHub Desktop.
The parse function in the twitz app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parseCmd = &cobra.Command{ | |
Use: "parse", | |
Short: "This command will extract the Twitter Accounts form a text file.", | |
Long: `This command will extract the Twitter Accounts and clean up or disregard other characters | |
or text around the twitter accounts to create a simple, clean, Twitter Accounts only list.`, | |
Run: func(cmd *cobra.Command, args []string) { | |
completedTwittererList := buildTwitterList() | |
fmt.Println(completedTwittererList) | |
if viper.Get("fileExport") != nil { | |
exportParsedTwitterList(viper.GetString("fileExport"), viper.GetString("fileFormat"), completedTwittererList) | |
} | |
}, | |
} | |
func exportParsedTwitterList(exportFilename string, exportFormat string, twittererList []string) { | |
if exportFormat == "txt" { | |
exportTxt(exportFilename, twittererList, exportFormat) | |
} else if exportFormat == "json" { | |
exportJson(exportFilename, twittererList, exportFormat) | |
} else if exportFormat == "xml" { | |
exportXml(exportFilename, twittererList, exportFormat) | |
} else if exportFormat == "csv" { | |
exportCsv(exportFilename, twittererList, exportFormat) | |
} else { | |
fmt.Println("Export type unsupported.") | |
} | |
} | |
func exportXml(exportFilename string, twittererList []string, exportFormat string) { | |
fmt.Printf("Starting xml export to %s.", exportFilename) | |
xmlContent, err := xml.Marshal(twittererList) | |
check(err) | |
header := xml.Header | |
collectedContent := header + string(xmlContent) | |
exportFile(collectedContent, exportFilename+"."+exportFormat) | |
} | |
func exportCsv(exportFilename string, twittererList []string, exportFormat string) { | |
fmt.Printf("Starting txt export to %s.", exportFilename) | |
collectedContent := rebuildForExport(twittererList, ",") | |
exportFile(collectedContent, exportFilename+"."+exportFormat) | |
} | |
func exportTxt(exportFilename string, twittererList []string, exportFormat string) { | |
fmt.Printf("Starting %s export to %s.", exportFormat, exportFilename) | |
collectedContent := rebuildForExport(twittererList, "\n") | |
exportFile(collectedContent, exportFilename+"."+exportFormat) | |
} | |
func exportJson(exportFilename string, twittererList []string, exportFormat string) { | |
fmt.Printf("Starting %s export to %s.", exportFormat, exportFilename) | |
collectedContent := collectContent(twittererList) | |
exportFile(string(collectedContent), exportFilename+"."+exportFormat) | |
} | |
func collectContent(twittererList []string) []byte { | |
collectedContent, err := json.Marshal(twittererList) | |
check(err) | |
return collectedContent | |
} | |
func rebuildForExport(twittererList []string, concat string) string { | |
var collectedContent string | |
for _, twitterAccount := range twittererList { | |
collectedContent = collectedContent + concat + twitterAccount | |
} | |
if concat == "," { | |
collectedContent = strings.TrimLeft(collectedContent, concat) | |
} | |
return collectedContent | |
} | |
func exportFile(collectedContent string, exportFile string) { | |
contentBytes := []byte(collectedContent) | |
err := ioutil.WriteFile(exportFile, contentBytes, 0644) | |
check(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment