loading words...

Feb 12, 2019 17:41:46

200WAD Post Data into CSV

by @itsabhinaya PATRON | 317 words | 🐣 | 180💌

Abhinaya Konduru

Current day streak: 0🐣
Total posts: 180💌
Total words: 69826 (279 pages 📄)

Recently saw @danielmiller write a script to save all 200WAD posts into txt files. While it is useful to have that as a backup, I wanted to write a script to extract info into a CSV file. You can find the code here.

I am trying to get better at coding to work on side projects so here is a breakdown of the script and thought process behind it.

  • First thing I did was test out what the API would return because there is no documentation on it yet (https://200wordsaday.com/api/texts?api_key=API_KEY_HERE).
  • After learning it was JSON format, I looked into JSON gem but noticed that @danielmiller used HTTParty gem instead. I looked into the differences and decided to use HTTParty gem for my script as well.
  • Then I tried to print just one entry/post data to see what the structure is. When you print it, you can't really see the structure well. I had to use JSON.pretty_generate to see the structure. 
  • After learning about the structure, I selected the variables that I want in my CSV.
  • I forgot what the code was to create and update a CSV file was and ended up googling it.
  • Now I had all the elements that I needed to get the CSV file. After running the code on just a few entries, I noticed that the "categories" variable was in a hash format.
  • I had to convert this [{"name"=>"#random"}, {"name"=>"#tech"}] to "#random, #tech"
  • After couple of tries and googling figured out how to do it. entry["categories"].collect { |item| item['name'] }.compact.join(", ")

Once I had all the parts that I needed, I ran the script with all entries and it does exactly what it supposed to do.

As if now, the API doesn't return the collections info for a specific post.

From Abhinaya Konduru's collection:

  • 1

    @itsabhinaya nice!! :)

    Basile Samel avatar Basile Samel | Feb 13, 2019 09:58:39
  • 1

    @itsabhinaya also excited that you also used Ruby! :)

    Daniel Miller avatar Daniel Miller | Feb 12, 2019 12:12:20
    • 1

      @danielmiller Ruby was the first language I learned and loved using it before having to use the others like Python, C , and Javascript.

      Abhinaya Konduru avatar Abhinaya Konduru | Feb 13, 2019 15:16:56
  • 1

    @itsabhinaya Awesome! I'm excited my little script has proved useful. Mine is a bit messy still, I am hoping to clean it up a bit.

    I love the chance to write a small script or application to solve a single problem. It brings back some of the original passion I felt around programming.

    Daniel Miller avatar Daniel Miller | Feb 12, 2019 11:55:44
    • 1

      @danielmiller I have so many scripts that I write to automate stuff. I should start making them public but yes love them.

      Abhinaya Konduru avatar Abhinaya Konduru | Feb 13, 2019 15:16:03
contact: email - twitter / Terms / Privacy