Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reddit API only returning one post

I am trying to get all of the links in a subreddit using the API, but it is only returning one url. Here is the code I have:

var request = require('request');
webpage = 'http://www.reddit.com/r/AmazonUnder5/top.json?limit=100';

//login
request.post('http://www.reddit.com/api/login',{form:{api_type:'json', passwd:'password', rem:true, user:'username'}});

//get urls
request({uri : webpage, json:true, headers:{useragent: 'mybot v. 0.0.1'}}, function(error, response, body) {
    if(!error && response.statusCode == 200) {
        for(var key in body.data.children) {
            var url = body.data.children[key].data.url;
            console.log(url);
        }

    }
});

When I visit the json link in my browser, it returns all 100 posts.

like image 780
Edward Yu Avatar asked Nov 02 '22 17:11

Edward Yu


1 Answers

Thats because only 1 exists in the top

http://www.reddit.com/r/AmazonUnder5/top

You could use hot instead

http://www.reddit.com/r/AmazonUnder5/hot.json

Also, you don't need to log in to do public get requests

Edit: You are getting so few results because you are not logged in properly

When logging in, use the

"op" => "login"

Parameter and test what cookies and data is returned.

I also recommend using the ssl login url since that works for me

https://ssl.reddit.com/api/login/
like image 157
John Avatar answered Nov 15 '22 05:11

John