var request = require('request-json');
var mongoose = require("mongoose");
var Anime   = require('../models/anime');

// Database
var config = require('../config/config');
mongoose.connect(config.database);

//data to get token
var data = {
  grant_type    : "client_credentials",
  client_id     :  process.env.ClientID,
  client_secret :  process.env.ClientSecret,
};
// client url
var client = request.createClient('https://anilist.co/api/');
//post to get token back
client.post('auth/access_token', data, function(err, res, body) {
  if (err) return console.log(err); //error
  var clientToken = body.access_token;
  var sendClientToken = '?access_token=' + clientToken;

  Anime.find().exec(function(err, animes){
    if (err) return console.log(err); //error
    // loop through all animes in db
    for (var i = 0; i < animes.length; i++) {
      var id = animes[i]._id;
      var url = 'anime/' + id + sendClientToken;
      // request from api every anime in the db
      client.get(url, function (err, res, body) {
        // updates the current data in database
        var re = /<br>|\(Source.+/g;
        var description = body.description.replace(re, "");
        Anime.update({ _id: body.id }, { $set: { description: description }}, function (err, anime) {
          if (err) return console.log(err); //error
        });
      });
    }
  });
});

console.log('done');

@@ -109,16 +109,51 @@
 anime');
+%0Avar Token = require('./aniToken');
 %0A%0A// Dat
@@ -240,476 +240,62 @@
 );%0A%0A
-//data to get token%0Avar data = %7B%0A  grant_type    : %22client_credentials%22,%0A  client_id     :  process.env.ClientID,%0A  client_secret :  process.env.ClientSecret,%0A%7D;%0A// client url%0Avar client = request.createClient('https://anilist.co/api/');%0A//post to get token back%0Aclient.post('auth/access_token', data, function(err, res, body) %7B%0A  if (err) return console.log(err); //error%0A  var clientToken = body.access_token;%0A  var sendClientToken = '?access_token=' + clientToken;%0A
+function updateDescriptions(sendClientToken, client) %7B
 %0A  A
@@ -978,29 +978,45 @@
 );%0A%7D
-);%0A%0Aconsole.log('done'
+%0A%0AToken.getAniToken(updateDescriptions
 );%0A

update description uses aniToken module