0

I was writing an API using the kat.cr API and the IMDB api too in nodejs, I didn't use json.stringify cuz I didn't know about it at the time of writing haha XD, anyway, the problem is that when I loop through the code in 46 through 50, the response remains the same here's an example, here is the json generated,

{
  "MovieList": [{
    "title": "Jurassic World",
    "imdb": "tt0369610",
    "poster_med": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE@._V1_SX300.jpg",
    "poster_big": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE@._V1_SX300.jpg",
    "genres": ["Action, Adventure, Sci-Fi"],
    "items": [{
      "torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
      "torrent_seeds": "1262",
      "torrent_peers": "1306",
      "id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
    }]
  }, {
    "title": "San Andreas",
    "imdb": "tt2126355",
    "poster_med": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE@._V1_SX300.jpg",
    "poster_big": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE@._V1_SX300.jpg",
    "genres": ["Action, Drama, Thriller"],
    "items": [{
      "torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
      "torrent_seeds": "1262",
      "torrent_peers": "1306",
      "id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
    }]
  }]
}

Here is the crawler code :

var kat = require('kat-api');
var IMDb = require('imdb-scraper');
var movieTitle = require('movie-title');
var nameToImdb = require("name-to-imdb");
var movie = require('node-movie');
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/main.json', {
  flags: 'w'
});
var log_stdout = process.stdout;

var config = '720p 2015'; //This is the line that should be changed if needed!

console.log = function(d) { //
  log_file.write(util.format(d) + '\n');
  log_stdout.write(util.format(d) + '\n');
};

var kat = require('kat-api');
kat.search({
  query: config,
  category: 'movies',
  language: 'en'
}).then(function(response) {    
  var quotes = '"';
  var startingOfJson = "{" + quotes + "MovieList" + quotes + ":" + "[";
  var endingOfJson = "}";
  var itemStart = quotes + "items" + quotes + ":" + "[{";
  var itemEnd = "}]";

  console.log(startingOfJson);
  for (i = 0; i <= 20; i++) {    
    var titleForEverything = movieTitle(response.results[i].title);

    movie(titleForEverything, function(err, data) {

      console.log("{");
      console.log(quotes + "title" + quotes + ":" + quotes + data.Title + quotes + ",");
      console.log(quotes + "imdb" + quotes + ":" + quotes + data.imdbID + quotes + ",");
      console.log(quotes + "poster_med" + quotes + ":" + quotes + data.Poster + quotes + ",");
      console.log(quotes + "poster_big" + quotes + ":" + quotes + data.Poster + quotes + ",");
      var genres = quotes + "genres" + quotes + ":" + "[" + quotes + data.Genre + quotes + "]" + ",";
      console.log(genres);
      console.log(itemStart);
      console.log(quotes + "torrent_magnet" + quotes + ":" + quotes + response.results[i].magnet + quotes + ",");
      console.log(quotes + "torrent_seeds" + quotes + ":" + quotes + response.results[i].seeds + quotes + ",");
      console.log(quotes + "torrent_peers" + quotes + ":" + quotes + response.results[i].peers + quotes + ",");
      console.log(quotes + "id" + quotes + ":" + quotes + response.results[i].hash + quotes);
      console.log(itemEnd);
      if (i == 20) {
        console.log("}");
      } else {
        console.log("},")
      }
    });
  }
}).catch(function(error) {
  console.log('an error occured' + error);
});
console.log("]}");

and you can see that the magnet, seeds, hash and peers remain the same for all the results generated! How can I fix this and why does this happen? Thank you! :D

Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • 2
    Include relevant code here, not in a pastebin. We don't like clicking on links to see code – Tim Jan 08 '16 at 10:53
  • I'm so sorry I didn't know how to do it, how do I do it ? – Hayzam Sherif Jan 08 '16 at 10:57
  • http://stackoverflow.com/help/how-to-ask – Tim Jan 08 '16 at 10:58
  • *you can see that the magnet, seeds, hash and peers remain the same for all the results* we cannot see that, can you include the output you're getting? – Tim Jan 08 '16 at 11:20
  • I did include that, the first block of json code,the results from kat is not different, they're all the same when I try to loop through it – Hayzam Sherif Jan 08 '16 at 11:22
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) –  Jan 08 '16 at 11:54

1 Answers1

1

You're committing the classic error of a function inside a loop closing over the loop index i; when the function is executed, i will already have its final value. The easiest way to fix this is with for (let i.