Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func getPostsFromCSV(byteResponse []byte, network string) map[string][]ESPosts {
- r := bytes.NewReader(byteResponse)
- reader := csv.NewReader(r)
- reader.FieldsPerRecord = -1
- csvData, err := reader.ReadAll()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- var firstAccount string
- var lastAccount string
- postsArr := make(map[string][]ESPosts)
- for _, line := range csvData {
- data := CSVStruct{
- NetworkUser: line[0],
- Type: &line[1],
- SocialNetworkId: &line[2],
- Link: &line[3],
- PostDateTime: &line[4],
- Thumbnail: &line[5],
- LikesCount: &line[6],
- CommentsCount: &line[7],
- SharesCount: &line[8],
- ViewCount: &line[9],
- Text: line[10],
- Title: &line[11],
- ImageLabels: &line[12],
- BrandsInfo: &line[13],
- HasNSFWText: &line[14],
- NSFWWords: &line[15],
- IsSponsored: &line[16],
- SponsoredWords: &line[17],
- }
- if data.PostDateTime != nil {
- csvFormat := "2006-01-02 15:04:05"
- timeVariable, err := time.Parse(csvFormat, *data.PostDateTime)
- if err != nil {
- fmt.Println(err)
- }
- RFC3339Format := timeVariable.Format(time.RFC3339)
- data.PostDateTime = &RFC3339Format
- }
- if len(firstAccount) == 0 {
- if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
- firstAccount = data.NetworkUser
- } else {
- firstAccount, _ = esLogic.InterfaceToString(*data.SocialNetworkId)
- }
- }
- if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
- lastAccount = data.NetworkUser
- } else {
- lastAccount, _ = esLogic.InterfaceToString(*data.SocialNetworkId)
- }
- esPosts := ESPosts{}
- esPosts.NetworkUser = data.NetworkUser
- if data.Type != nil {
- esPosts.Type = *data.Type
- }
- if data.SocialNetworkId != nil {
- esPosts.SocialNetworkID = *data.SocialNetworkId
- }
- if data.Link != nil {
- esPosts.Link = data.Link
- }
- if data.PostDateTime != nil {
- esPosts.PostDateTime = *data.PostDateTime
- }
- if data.Thumbnail != nil {
- esPosts.Thumbnail = data.Thumbnail
- }
- if data.LikesCount != nil {
- metricCnt, _ := strconv.ParseInt(*data.LikesCount, 10, 64)
- esPosts.LikesCount = &metricCnt
- }
- if data.CommentsCount != nil {
- metricCnt, _ := strconv.ParseInt(*data.CommentsCount, 10, 64)
- esPosts.CommentCount = &metricCnt
- }
- if data.SharesCount != nil {
- metricCnt, _ := strconv.ParseInt(*data.SharesCount, 10, 64)
- esPosts.SharesCount = &metricCnt
- }
- if data.ViewCount != nil {
- metricCnt, _ := strconv.ParseInt(*data.ViewCount, 10, 64)
- esPosts.ViewCount = &metricCnt
- }
- esPosts.Text = &data.Text
- esPosts.Title = data.Title
- if data.ImageLabels != nil && len(*data.ImageLabels) > 2 {
- imageLabels := getLablesFromCustomString(*data.ImageLabels)
- esPosts.ImageLabels = &imageLabels
- }
- if data.BrandsInfo != nil && len(*data.BrandsInfo) > 1 {
- brandsData := make(map[string][]string)
- err := json.Unmarshal([]byte(*data.BrandsInfo), &brandsData)
- if err != nil {
- fmt.Println(err)
- }
- brandsInfoAll := []KeyValueStruct{}
- for brandName, hashes := range brandsData {
- brandsInfoItem := KeyValueStruct{}
- brandsInfoItem.Value = brandName
- brandsInfoItem.Hashes = hashes
- brandsInfoAll = append(brandsInfoAll, brandsInfoItem)
- }
- esPosts.BrandsInfo = &brandsInfoAll
- }
- if data.NSFWWords != nil && len(*data.NSFWWords) > 1 {
- boolValStruct := BoolValueStruct{}
- boolValStruct.Value = processCSVBool(*data.HasNSFWText)
- boolValStruct.Hashes = JSONArrayFromString(*data.NSFWWords)
- esPosts.NSFWInfo = &boolValStruct
- }
- if data.SponsoredWords != nil && len(*data.SponsoredWords) > 1 {
- boolValStruct := BoolValueStruct{}
- boolValStruct.Value = processCSVBool(*data.IsSponsored)
- boolValStruct.Hashes = JSONArrayFromString(*data.SponsoredWords)
- esPosts.SponsoredInfo = &boolValStruct
- }
- if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
- postsArr[esPosts.NetworkUser] = append(postsArr[esPosts.NetworkUser], esPosts)
- } else {
- networkNameInt, isNil := esLogic.InterfaceToInt64(esPosts.SocialNetworkID)
- if !isNil {
- networkIdStr := strconv.FormatInt(networkNameInt, 10)
- postsArr[networkIdStr] = append(postsArr[networkIdStr], esPosts)
- }
- }
- }
- firstAccountFullData := fillFirstAccount(postsArr[firstAccount], firstAccount, network)
- postsArr[firstAccount] = firstAccountFullData
- lastAccountFullData := fillFirstAccount(postsArr[lastAccount], lastAccount, network)
- postsArr[lastAccount] = lastAccountFullData
- return postsArr
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement