Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.77 KB | None | 0 0
  1. func getPostsFromCSV(byteResponse []byte, network string) map[string][]ESPosts {
  2.     r := bytes.NewReader(byteResponse)
  3.     reader := csv.NewReader(r)
  4.     reader.FieldsPerRecord = -1
  5.  
  6.     csvData, err := reader.ReadAll()
  7.     if err != nil {
  8.         fmt.Println(err)
  9.         os.Exit(1)
  10.     }
  11.  
  12.     var firstAccount string
  13.     var lastAccount string
  14.  
  15.     postsArr := make(map[string][]ESPosts)
  16.     for _, line := range csvData {
  17.  
  18.         data := CSVStruct{
  19.             NetworkUser:     line[0],
  20.             Type:            &line[1],
  21.             SocialNetworkId: &line[2],
  22.             Link:            &line[3],
  23.             PostDateTime:    &line[4],
  24.             Thumbnail:       &line[5],
  25.             LikesCount:      &line[6],
  26.             CommentsCount:   &line[7],
  27.             SharesCount:     &line[8],
  28.             ViewCount:       &line[9],
  29.             Text:            line[10],
  30.             Title:           &line[11],
  31.             ImageLabels:     &line[12],
  32.             BrandsInfo:      &line[13],
  33.             HasNSFWText:     &line[14],
  34.             NSFWWords:       &line[15],
  35.             IsSponsored:     &line[16],
  36.             SponsoredWords:  &line[17],
  37.         }
  38.  
  39.         if data.PostDateTime != nil {
  40.             csvFormat := "2006-01-02 15:04:05"
  41.             timeVariable, err := time.Parse(csvFormat, *data.PostDateTime)
  42.  
  43.             if err != nil {
  44.                 fmt.Println(err)
  45.             }
  46.  
  47.             RFC3339Format := timeVariable.Format(time.RFC3339)
  48.             data.PostDateTime = &RFC3339Format
  49.         }
  50.  
  51.         if len(firstAccount) == 0 {
  52.             if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
  53.                 firstAccount = data.NetworkUser
  54.             } else {
  55.                 firstAccount, _ = esLogic.InterfaceToString(*data.SocialNetworkId)
  56.             }
  57.         }
  58.  
  59.         if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
  60.             lastAccount = data.NetworkUser
  61.         } else {
  62.             lastAccount, _ = esLogic.InterfaceToString(*data.SocialNetworkId)
  63.         }
  64.  
  65.         esPosts := ESPosts{}
  66.         esPosts.NetworkUser = data.NetworkUser
  67.         if data.Type != nil {
  68.             esPosts.Type = *data.Type
  69.         }
  70.  
  71.         if data.SocialNetworkId != nil {
  72.             esPosts.SocialNetworkID = *data.SocialNetworkId
  73.         }
  74.         if data.Link != nil {
  75.             esPosts.Link = data.Link
  76.         }
  77.         if data.PostDateTime != nil {
  78.             esPosts.PostDateTime = *data.PostDateTime
  79.         }
  80.         if data.Thumbnail != nil {
  81.             esPosts.Thumbnail = data.Thumbnail
  82.         }
  83.         if data.LikesCount != nil {
  84.             metricCnt, _ := strconv.ParseInt(*data.LikesCount, 10, 64)
  85.             esPosts.LikesCount = &metricCnt
  86.         }
  87.         if data.CommentsCount != nil {
  88.             metricCnt, _ := strconv.ParseInt(*data.CommentsCount, 10, 64)
  89.             esPosts.CommentCount = &metricCnt
  90.         }
  91.         if data.SharesCount != nil {
  92.             metricCnt, _ := strconv.ParseInt(*data.SharesCount, 10, 64)
  93.             esPosts.SharesCount = &metricCnt
  94.         }
  95.         if data.ViewCount != nil {
  96.             metricCnt, _ := strconv.ParseInt(*data.ViewCount, 10, 64)
  97.             esPosts.ViewCount = &metricCnt
  98.         }
  99.         esPosts.Text = &data.Text
  100.         esPosts.Title = data.Title
  101.         if data.ImageLabels != nil && len(*data.ImageLabels) > 2 {
  102.             imageLabels := getLablesFromCustomString(*data.ImageLabels)
  103.             esPosts.ImageLabels = &imageLabels
  104.         }
  105.  
  106.         if data.BrandsInfo != nil && len(*data.BrandsInfo) > 1 {
  107.             brandsData := make(map[string][]string)
  108.             err := json.Unmarshal([]byte(*data.BrandsInfo), &brandsData)
  109.             if err != nil {
  110.                 fmt.Println(err)
  111.             }
  112.  
  113.             brandsInfoAll := []KeyValueStruct{}
  114.             for brandName, hashes := range brandsData {
  115.                 brandsInfoItem := KeyValueStruct{}
  116.                 brandsInfoItem.Value = brandName
  117.                 brandsInfoItem.Hashes = hashes
  118.  
  119.                 brandsInfoAll = append(brandsInfoAll, brandsInfoItem)
  120.             }
  121.  
  122.             esPosts.BrandsInfo = &brandsInfoAll
  123.         }
  124.  
  125.         if data.NSFWWords != nil && len(*data.NSFWWords) > 1 {
  126.             boolValStruct := BoolValueStruct{}
  127.             boolValStruct.Value = processCSVBool(*data.HasNSFWText)
  128.             boolValStruct.Hashes = JSONArrayFromString(*data.NSFWWords)
  129.  
  130.             esPosts.NSFWInfo = &boolValStruct
  131.         }
  132.  
  133.         if data.SponsoredWords != nil && len(*data.SponsoredWords) > 1 {
  134.             boolValStruct := BoolValueStruct{}
  135.             boolValStruct.Value = processCSVBool(*data.IsSponsored)
  136.             boolValStruct.Hashes = JSONArrayFromString(*data.SponsoredWords)
  137.  
  138.             esPosts.SponsoredInfo = &boolValStruct
  139.         }
  140.  
  141.         if esLogic.InArray(network, []string{esLogic.ESData.Title.Network.Instagram, esLogic.ESData.Title.Network.Youtube}) {
  142.             postsArr[esPosts.NetworkUser] = append(postsArr[esPosts.NetworkUser], esPosts)
  143.         } else {
  144.             networkNameInt, isNil := esLogic.InterfaceToInt64(esPosts.SocialNetworkID)
  145.             if !isNil {
  146.                 networkIdStr := strconv.FormatInt(networkNameInt, 10)
  147.                 postsArr[networkIdStr] = append(postsArr[networkIdStr], esPosts)
  148.             }
  149.         }
  150.  
  151.     }
  152.  
  153.     firstAccountFullData := fillFirstAccount(postsArr[firstAccount], firstAccount, network)
  154.     postsArr[firstAccount] = firstAccountFullData
  155.  
  156.     lastAccountFullData := fillFirstAccount(postsArr[lastAccount], lastAccount, network)
  157.     postsArr[lastAccount] = lastAccountFullData
  158.  
  159.     return postsArr
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement