Fetching top story IDs on Hacker News

First, we will create a module called HackerNewsAnalysis. The very first function is going to retrieve the top stories from Hacker News. The code for this is as follows:

using HTTP
using JSON3

function fetch_top_stories()
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
response = HTTP.request("GET", url)
return JSON3.read(String(response.body))
end

How does it work? Let's try it out:

Let's take several steps and dissect the logic in this function. The top stories can be retrieved from a fixed URL. Here, we have used the HTTP package for fetching data from web services. The HTTP.request function call, if successful, returns an HTTP.Message.Response object. It is easy to verify from the REPL:

So, how do we get the content from the Response object? It is available from the body field. As it turns out, the body field is just a byte array. To understand what the data means, we can convert it into a String, as follows:

Judging from the output, we can see that it is in JSON format. We can also verify the same by visiting the web URL from a browser. From the API documentation, we know that the numbers represent story IDs from Hacker News. To parse the data into usable Julia data types, we can leverage the JSON3 package:

The JSON3.Array object is a lazy version of an array. By design, JSON3 does not extract the value until you ask for it. We can use it as if it were a regular Julia array. For more information, you are encouraged to visit JSON3's documentation on GitHub: https://github.com/quinnj/JSON3.jl/blob/master/README.md.

Now that we have an array of story IDs, we will develop the function for retrieving detailed information about a Hacker News story.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.166.98