Saya menggunakan variabel before_epoch untuk mengambil posting reddit sebelum hari ini di iterasi pertama. Iterasi berikutnya before_epoch harus berisi tanggal dari data respons. Anehnya, tanggal tidak berubah setelah dua putaran pertama

const getposts = async(user) => {
    var dataArray = [];
    var utc = new Date()
      .toJSON()
      .slice(0, 10)
      .replace(/-/g, "/");

var previous_epoch;


  for(let i=0; i <=5; i++) {
    if(i == 0) {
        console.log('i is 0');
        previous_epoch = new Date(utc).getTime() / 1000;
        console.log('p',previous_epoch); <--keeps doesn't work after second loop
    } 
    else {
        // console.log('else',dataArray);
        previous_epoch = dataArray[dataArray.length-1].created_utc
        console.log('else',previous_epoch);
    }
    await getdat(user,previous_epoch).then((resp) => {
        let jsondat = resp.data.data;
                dataArray = jsondat.concat(dataArray);
    })
  }
  // console.log( dataArray);
  var result = _.map( dataArray, function( o ) {
      return o.id;
  });
/* console.log("%j",result); */
}

Fungsi getdat()

const getdat = (user,epoch) => {

    const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${user}&limit=300&sort=desc&before=${epoch}`;
    console.log('elZK',sub_url);
  return new Promise((resolve, reject) => {
    axios.get(sub_url)
    .then(response => {
      return resolve(response)
    })
    .catch(error => {
      return reject(error.message)
    })
  })
}

Di sini adalah biola

EDIT 2: Saya telah mengubah logika sehingga saya mendapatkan tanggal berikutnya dari fungsi getdat itu sendiri

const getdat = (user,epoch) => {
    let res= {};
    const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${user}&limit=300&sort=desc&before=${epoch}`;
    console.log('elZK',sub_url);
  return new Promise((resolve, reject) => {
    axios.get(sub_url)
    .then(response => {
        let d = response.data.data;
        let e = d[d.length - 1].created_utc;
        console.log('e',e);
        res.data = d;
        res.epoch = e;
      return resolve(res)
    })
    .catch(error => {
      return reject(error.message)
    })
  })
}



export const getUserPosts = async(user) => {
    var dataArray = [];
    var utc = new Date()
      .toJSON()
      .slice(0, 10)
      .replace(/-/g, "/");

var previous_epoch;
var epoch;

for(let i=0; i <=5; i++) {
    if(dataArray.length === 0) {
        console.log('i is 0');
        previous_epoch = new Date(utc).getTime() / 1000;
        console.log('p',previous_epoch);
    } 
    else {
        // console.log('else',dataArray);
        previous_epoch = epoch;
        console.log('else',previous_epoch);
    }
    const resp = await getdat(user,previous_epoch);
    const jsondat = resp.data;
    dataArray = jsondat.concat(dataArray);  
    epoch = resp.epoch;      
  }
  // console.log( dataArray);
  var result = _.map( dataArray, function( o ) {
      return o.id;
  });
console.log("%j",result);
}
0
Sai Krishna 28 Mei 2019, 16:00

2 jawaban

Jawaban Terbaik

Pada iterasi pertama Anda mengambil posting sebelum hari ini, jadi posting terakhir dalam hasil akan create_utc sebelum atau sama dengan hari ini. Iterasi berikutnya cukup ulangi permintaan pertama Anda.

Coba kode ini

for(let i=0; i <=5; i++) {
    if(dataArray.length === 0) {
        console.log('i is 0');
        previous_epoch = new Date(utc).getTime() / 1000;
        console.log('p',previous_epoch);
    } 
    else {
        // console.log('else',dataArray);
        previous_epoch = dataArray[0].created_utc
        console.log('else',previous_epoch);
    }
    const resp = await getdat(user,previous_epoch);
    const jsondat = resp.data.data;
    dataArray = jsondat.concat(dataArray);        
  }
1
Yaroslav Gaponov 28 Mei 2019, 13:31

Coba kode ini

const getdat = (user,epoch) => {    
    const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${user}&limit=10&sort=desc&before=${epoch}`;
    console.log('elZK',sub_url);
    return axios.get(sub_url);
};

const getposts = async(user) => {
    let dataArray = []; 
    let previous_epoch;

    for(let i=0; i <=5; i++) {
     previous_epoch = dataArray.length>0 ? dataArray[dataArray.length-1].created_utc-1 : Math.floor(Date.now()/1000);
      const result =  await getdat(user,previous_epoch);  
      dataArray = dataArray.concat(result.data.data);
    }

    console.log(dataArray.map(e => e.created_utc));
};

getposts('data');
1
Yaroslav Gaponov 29 Mei 2019, 07:48