How to access all list elements

I have a tweeter scraper and I want to save scraped data to google spreadsheets. Result of the scraping is a struct like so

{:ok,
 %TwitterFeed.Feed{
   last_tweet_retrieved: 1204807643379974151,
   more_tweets_exist: true,
   tweets: [
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Today we're celebrating more than just new levels; today we hit level 6000! \nShare below if you've reached that milestone yet! pic.twitter.com/HxSj0GpUpk",
       timestamp: "2019-12-18 15:00:01.000Z",
       tweet_id: 1207314578675126272,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "We've got a gift for you...TWICE the number of New Levels! Let us know how you spent your day playing below! pic.twitter.com/eAVPxSAH2d",
       timestamp: "2019-12-25 15:00:00.000Z",
       tweet_id: 1209851293331841024,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Happy Holidays from us to you! We hope you're having a Sweet time with your loved ones! \nAre you spending the festive season playing Candy Crush? Tell us when a...",
       timestamp: "2019-12-24 14:00:00.000Z",
       tweet_id: 1209473805816479744,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Anybody else?! \n\nDo you spend the Holidays playing Candy Crush?https://twitter.com/JRxxxxxxx/status/1209361113487310848 …Don’t mind me \nI’ll just be playing can...",
       timestamp: "2019-12-24 09:47:13.000Z",
       tweet_id: 1209410188203216896,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "App Store",
       handle_id: 592700022,
       image_url: "",
       retweet: true,
       text_summary: "Winter offer! Get extra-special boosters that last twice as long in @CandyCrushSaga\n\nView the deal in-game: http://apple.co/CandyCrushSaga pic.twitter.com/l47U1...",
       timestamp: "2019-12-23 13:20:13.000Z",
       tweet_id: 1209101406126383104,
       tweet_lng: "",
       user_id: 74594552,
       user_name: "AppStore"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "https://pbs.twimg.com/media/EMgjh7jXUAUon4_.jpg",
       retweet: false,
       text_summary: "Earn points and get Sweet rewards this Holiday season! You can get x4 Google #PlayPoints on everything you buy in the Year End Deals in #CandyCrush before Decem...",
       timestamp: "2019-12-23 23:32:48.000Z",
       tweet_id: 1209255568038400002,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "https://pbs.twimg.com/media/EMe37k9WkAAb6tI.jpg",
       retweet: false,
       text_summary: "What a nice surprise!! It's Tiffi! Have you added all the new character costumes to your collection yet?! pic.twitter.com/XWbzNytJTX",
       timestamp: "2019-12-23 15:42:43.000Z",
       tweet_id: 1209137264145653769,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "https://pbs.twimg.com/media/EMOZdAHWwAAgL5b.jpg",
       retweet: false,
       text_summary: "Treat yourself to a crossword this weekend...Candy Crush style #CrossWordPuzzleDaypic.twitter.com/egvJL5EkS1",
       timestamp: "2019-12-21 14:00:00.000Z",
       tweet_id: 1208386641011904512,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "What could be more Sweet than a home-made card? \n\nFollow the easy steps below to learn how to make a Candy Crush card for your loved ones and be sure to tag us ...",
       timestamp: "2019-12-20 16:02:07.000Z",
       tweet_id: 1208054984597819399,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Holidays are just around the corner but, wait! You can still make that Delicious Gift for your special one! \n\nGet them a Google Gift Card before New Year’s Eve ...",
       timestamp: "2019-12-19 17:46:08.000Z",
       tweet_id: 1207718774943694848,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Has anyone else got to level 6000 yet?! \n\nWe want to know!https://twitter.com/cesar_ogata/status/1207514179315392512 …Level 6000 !! pic.twitter.com/1EHvB5G8xO",
       timestamp: "2019-12-19 11:02:38.000Z",
       tweet_id: 1207617229082288128,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Reasons to play Candy Crush: \n\nWhat are yours? Let us know below! https://twitter.com/theroyalfinesse/status/1207233349686906880 …1. My Go to Stress Reliever.\n2...",
       timestamp: "2019-12-18 09:49:38.000Z",
       tweet_id: 1207236471482175488,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Who can give us THREE reasons why Candy Crush was the most important app you used this year?! https://twitter.com/AppStore/status/1206621005076189185 …What’s th...",
       timestamp: "2019-12-17 10:42:51.000Z",
       tweet_id: 1206887475651776512,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Us too https://twitter.com/JEUmogbai/status/1206841388467404800 …Curious to find out: \"what level are you on in the @CandyCrushSaga",
       timestamp: "2019-12-17 10:07:50.000Z",
       tweet_id: 1206878661388967937,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "That's the #MondayMotivation we need today!! \n\nHow do you feel when you pass a level?! https://twitter.com/hessaa_alk/status/1206524514324099077 …i won a very h...",
       timestamp: "2019-12-16 11:51:28.000Z",
       tweet_id: 1206542353756033025,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "https://pbs.twimg.com/media/ELqGjMzWsAAjDzC.jpg",
       retweet: false,
       text_summary: "We're spilling the tea; Candy Crush goes nicely with a cup of your favorite hot drink ! \nWhat's your favorite flavour of tea?! \n\nThanks @nikki_starr_ for sharin...",
       timestamp: "2019-12-15 14:00:00.000Z",
       tweet_id: 1206212313583366144,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Follow the steps in the video to create your very own Color-Bomb decoration! \nA festive DIY delight! Make your very own Color-Bomb Bauble to decorate your tree ...",
       timestamp: "2019-12-13 14:00:00.000Z",
       tweet_id: 1205487536623280128,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "Couldn't agree more https://twitter.com/MKarelyn/status/1205348068864004100 …If you really love someone you show it by sending them unsolicited candy crush live...",
       timestamp: "2019-12-13 10:47:22.000Z",
       tweet_id: 1205439061135167489,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: true,
       text_summary: "The new @JumanjiMovie has brought Candy Crush to The Next Level! Play the exclusive in-game event with @TheRock and @KevinHart4Real for three-days only! Be sure...",
       timestamp: "2019-12-10 19:01:13.000Z",
       tweet_id: 1204476179719294976,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     },
     %TwitterFeed.Tweet{
       display_name: "Candy Crush Saga",
       handle_id: 592700022,
       image_url: "",
       retweet: false,
       text_summary: "After a long day of frolicking in the snow, Tiffi glides down the mountain and plays all NEW levels of Candy Crush. Out now! pic.twitter.com/pmD5eueZr7",
       timestamp: "2019-12-11 16:58:21.000Z",
       tweet_id: 1204807643379974151,
       tweet_lng: "",
       user_id: 592700022,
       user_name: "CandyCrushSaga"
     }
   ]
 }}

currently I’m accessing only first element of tweets and doing something like this

  def save_to_spreadsheet(handle, start_after_tweet \\ 0) do
    {:ok, pid} = GSS.Spreadsheet.Supervisor.spreadsheet("1Xvv", list_name: "candy")

    fetch_tweets =
      TwitterFeed.get_tweets(handle, start_after_tweet)

    save_to_candy =
      GSS.Spreadsheet.write_rows(pid, ["A2:I2"], [[
        List.first(fetch_tweets.tweets).handle_id,
        List.first(fetch_tweets.tweets).tweet_id,
        List.first(fetch_tweets.tweets).user_id,
        List.first(fetch_tweets.tweets).tweet_lng,
        List.first(fetch_tweets.tweets).user_name,
        List.first(fetch_tweets.tweets).display_name,
        List.first(fetch_tweets.tweets).timestamp,
        List.first(fetch_tweets.tweets).text_summary,
        List.first(fetch_tweets.tweets).image_url,
        ]])

    {:ok, save_to_candy}
  end
end

Can you help me understand what is the proper way of accessing all the elements of tweets so I can save them into the spreadsheet.

Use Enum.each/2 or Enum.map/2 instead depending on if you need the result list or just calling for the sideffects.

1 Like

ok cool, so either iterate or create a map

Enum - and perhaps you also need index eg.

fetch_tweets.tweets
|> Enum.with_index(1) #offset 1
|> Enum.each(fn {tweet, index} -> 

  GSS.Spreadsheet.append_row(pid, index,
  
    [ tweet.handle_id, tweet.tweet_id, etc..]
   #maybe DateTime parse the timestamp
   )

end)
1 Like

hey, I just created working example with map, but thanks for the idea, I’ll also play with your solution.

1 Like