<commit_before>var _ = require('lodash'),
    Q = require('q'),
    request = require('request'),
    token = process.env.META_RESOLVER_VIMEO_ACCESS_TOKEN,
    pattern = /^https:\/\/vimeo.com\/([0-9]+)/ig,
    endpoint = 'https://api.vimeo.com/videos/';

function _andReject(deferred, item) {
  deferred.reject(item);
  return deferred.promise;
}

function _fetchVimeoMeta(id, cb) {
  var options = {
    url: endpoint + id,
    method: 'GET',
    auth: {
      bearer: token
    },
    json: true
  }

  request(options, function(err, res, body) {
    if(err || res.statusCode != 200) {
      return cb(err || body);
    }

    cb(null, body);
  });
}

function _getBestPicture(pictures) {
  if(!pictures || !pictures.sizes) {
    return undefined;
  }

  return _(pictures.sizes)
    .chain()
    .sortBy(function(t) { return t.width; })
    .map(function(t) { return t.link; })
    .last()
    .value();
}

function _normalizeMeta(id, meta) {
  return {
    id: id,
    title: meta.name,
    description: meta.description,
    createdAt: meta.created_time,
    previewImage: _getBestPicture(meta.pictures),
    plays: meta.stats.plays,
    likes: meta.metadata.connections.likes.total
  };
}

module.exports = function(item) {
  var deferred = Q.defer();
  if(!pattern.test(item.url)) {
    return _andReject(deferred, item);
  }

  pattern.lastIndex = 0;
  var videoId = _(pattern.exec(item.url)).last();

  _fetchVimeoMeta(videoId, function(err, meta) {
    if(err) {
      console.log('[VIMEO] Error:'.red, err);
      return _andReject(deferred, item);
    }

    deferred.resolve(_.extend(item, {
      type: 'vimeo',
      resolved: true,
      meta: _normalizeMeta(videoId, meta)
    }));
  });

  return deferred.promise;<commit_msg>Update vimeo.js<commit_after>var _ = require('lodash'),
    Q = require('q'),
    request = require('request'),
    token = process.env.META_RESOLVER_VIMEO_ACCESS_TOKEN,
    pattern = /^https:\/\/vimeo.com\/([0-9]+)/ig,
    endpoint = 'https://api.vimeo.com/videos/';

function _andReject(deferred, item) {
  deferred.reject(item);
  return deferred.promise;
}

function _fetchVimeoMeta(id, cb) {
  var options = {
    url: endpoint + id,
    method: 'GET',
    auth: {
      bearer: token
    },
    json: true
  }

  request(options, function(err, res, body) {
    if(err || res.statusCode != 200) {
      return cb(err || body);
    }

    cb(null, body);
  });
}

function _getBestPicture(pictures) {
  if(!pictures || !pictures.sizes) {
    return undefined;
  }

  return _(pictures.sizes)
    .chain()
    .sortBy(function(t) { return t.width; })
    .map(function(t) { return t.link; })
    .last()
    .value();
}

function _normalizeMeta(id, meta) {
  return {
    id: id,
    title: meta.name,
    description: meta.description,
    createdAt: meta.created_time,
    previewImage: _getBestPicture(meta.pictures),
    plays: meta.stats.plays,
    likes: meta.metadata.connections.likes.total,
    duration: meta.duration * 1000
  };
}

module.exports = function(item) {
  var deferred = Q.defer();
  if(!pattern.test(item.url)) {
    return _andReject(deferred, item);
  }

  pattern.lastIndex = 0;
  var videoId = _(pattern.exec(item.url)).last();

  _fetchVimeoMeta(videoId, function(err, meta) {
    if(err) {
      console.log('[VIMEO] Error:'.red, err);
      return _andReject(deferred, item);
    }

    deferred.resolve(_.extend(item, {
      type: 'vimeo',
      resolved: true,
      meta: _normalizeMeta(videoId, meta)
    }));
  });

  return deferred.promise;
{'fraction_non_alphanumeric': 0.16023823028927964, 'fraction_numerical': 0.0045377197958026095, 'mean_word_length': 3.026255707762557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1743476', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1268, 'n_words': 231}