So I don’t know if this fits here, as I am very new to coding and this might be an advanced topic.
If you ever try to access Reddit on an amazon echo, when you give it a subreddit, it reads out the first three results on that sub’s ‘hot’ page. The ‘hot’ page changes multiple times a day and so the results are ever-changing.
So I was curious as to how one would code something like that so the link is not static. I asked around on Reddit a bit and was told it uses the Reddit API but not much else.
Did you look at the API docs?
Have you done any projects using an API before?
Do you know how to use fetch?
Learn Fetch API In 6 Minutes
Do you know how to get the data from the API onto the page?
Learn DOM Manipulation In 18 Minutes
As long as the API returns new data you really do not have to do anything (other than re-fetch) to get the latest threads. There is apparently also a web socket endpoint for listening in real-time for new data (for example for new posts in a thread). But I have never used it.
Here is a quick simple example (because I apparently had nothing better to do).
Yeah, I viewed the API code this morning around 3 am so I didn’t really understand much, but I would look at it again.No I have never worked with API before so I really appreciate the small demo. Thanks a million for your help it was ver insightful.
No problem, happy to be of help.
If you start coding and need help with it you can always ask here on the forum.
To code this functionality, you would need to use the Reddit API to retrieve the latest information from the subreddit’s “hot” page. Here’s a high-level overview of the steps involved:
- Get a Reddit API key: To access Reddit’s data, you’ll need to sign up for a Reddit API key. This key will be used to make requests to the Reddit API and retrieve the data you need.
- Make a request to the Reddit API: Once you have your API key, you’ll need to make a request to the Reddit API to retrieve the data for the specified subreddit’s “hot” page. You can use an HTTP client library in your programming language of choice to make this request.
- Parse the response: The response from the Reddit API will be in JSON format, which you’ll need to parse in order to extract the data you need. You can use a JSON parsing library or built-in functions in your programming language to parse the response.
- Extract the information you need: Once you’ve parsed the response, you can extract the information you need for the first three results on the subreddit’s “hot” page. This information may include the title of the post, the URL, and other relevant details.
- Display the information: Finally, you’ll need to display the information to the user. This could involve reading the information out loud on an Amazon Echo device, or displaying it in a web page or mobile app.
This is a high-level overview of the process, and there are many different ways you could implement this functionality, depending on your specific requirements and the programming language and tools you’re using. However, this should give you a good starting point if you’re looking to build a Reddit reader that retrieves information from the Reddit API in real-time.
I did want to simplify things a little bit as it may help. Basically making an API request is just going to a web address to get some data with a specific request type (GET,PUT,POST,DELETE). For instance:
That is the web address to a Weather API on FCC to get the weather. Clicking it basically sends a GET request, and then you can see what the data you get back might look like. Thought about sharing the address for the Reddit data you wanted, but thought it maybe bad form to make those requests from a FCC forum.
So basically thats what you need to do in your code… which is what the
fetch function can do. Once you get the data you just need to post it to your page.
Hope that helps:)