<commit_before>getEmbedlyData = function (url) {
  var data = {}
  var extractBase = 'http://api.embed.ly/1/extract';
  var embedlyKey = getSetting('embedlyKey');
  var thumbnailWidth = getSetting('thumbnailWidth', 200);
  var thumbnailHeight = getSetting('thumbnailHeight', 125);

  if(!embedlyKey) {
    // fail silently to still let the post be submitted as usual
    console.log("Couldn't find an Embedly API key! Please add it to your Telescope settings or remove the Embedly module.");
    return null;
  }

  try {
    
    var result = Meteor.http.get(extractBase, {
      params: {
        key: embedlyKey,
        url: url,
        image_width: thumbnailWidth,
        image_height: thumbnailHeight,
        image_method: 'crop'
      }
    });

    // console.log(result)

    if (!!result.data.images && !!result.data.images.length) // there may not always be an image
      result.data.thumbnailUrl = result.data.images[0].url; // add thumbnailUrl as its own property

    return _.pick(result.data, 'title', 'media', 'description', 'thumbnailUrl');

  } catch (error) {
    console.log(error)
    // the first 13 characters of the Embedly errors are "failed [400] ", so remove them and parse the rest
    var errorObject = JSON.parse(error.message.substring(13));
    throw new Meteor.Error(errorObject.error_code, errorObject.error_message);
    return null;
  }
}

Meteor.methods({
  testGetEmbedlyData: function (url) {
    console.log(getEmbedlyData(url))
  },
  getEmbedlyData: function (url) {
    return getEmbedlyData(url);
  },
  embedlyKeyExists: function () {
    return !!getSetting('embedlyKey');
  }
});

// For security reason, we use a separate server-side API call to set the media object
var addMediaOnSubmit = function (post) {
  if(post.url){
    var data = getEmbedlyData(post.url);
    if(!!data && !!data.media.html)
      post.media = data.media
  }
  return post;
}
postSubmitMethodCallbacks.push(addMediaOnSubmit);

// TODO: find a way to only do this is URL has actually changed?
var updateMediaOnEdit = function (updateObject) {
  var post = updateObject.$set
  if(post.url){
    var data = getEmbedlyData(post.url);
    if(!!data && !!data.media.html)
      updateObject.$set.media = data.media
  }
  return updateObject;
}
postEditMethodCallbacks.push(updateMediaOnEdit);<commit_msg>add thumbnail too if it hasn't been set yet<commit_after>getEmbedlyData = function (url) {
  var data = {}
  var extractBase = 'http://api.embed.ly/1/extract';
  var embedlyKey = getSetting('embedlyKey');
  var thumbnailWidth = getSetting('thumbnailWidth', 200);
  var thumbnailHeight = getSetting('thumbnailHeight', 125);

  if(!embedlyKey) {
    // fail silently to still let the post be submitted as usual
    console.log("Couldn't find an Embedly API key! Please add it to your Telescope settings or remove the Embedly module.");
    return null;
  }

  try {
    
    var result = Meteor.http.get(extractBase, {
      params: {
        key: embedlyKey,
        url: url,
        image_width: thumbnailWidth,
        image_height: thumbnailHeight,
        image_method: 'crop'
      }
    });

    // console.log(result)

    if (!!result.data.images && !!result.data.images.length) // there may not always be an image
      result.data.thumbnailUrl = result.data.images[0].url; // add thumbnailUrl as its own property

    return _.pick(result.data, 'title', 'media', 'description', 'thumbnailUrl');

  } catch (error) {
    console.log(error)
    // the first 13 characters of the Embedly errors are "failed [400] ", so remove them and parse the rest
    var errorObject = JSON.parse(error.message.substring(13));
    throw new Meteor.Error(errorObject.error_code, errorObject.error_message);
    return null;
  }
}

Meteor.methods({
  testGetEmbedlyData: function (url) {
    console.log(getEmbedlyData(url))
  },
  getEmbedlyData: function (url) {
    return getEmbedlyData(url);
  },
  embedlyKeyExists: function () {
    return !!getSetting('embedlyKey');
  }
});

// For security reason, we use a separate server-side API call to set the media object,
// and the thumbnail object if it hasn't already been set
var addMediaOnSubmit = function (post) {
  if(post.url){
    var data = getEmbedlyData(post.url);
    if (!!data) {
      // only add a thumbnailUrl if there isn't one already
      if(!post.thumbnailUrl && !!data.thumbnailUrl)
        post.thumbnailUrl = data.thumbnailUrl
      // add media if necessary
      if(!!data.media.html)
        post.media = data.media
    }
  }
  return post;
}
postSubmitMethodCallbacks.push(addMediaOnSubmit);

// TODO: find a way to only do this is URL has actually changed?
var updateMediaOnEdit = function (updateObject) {
  var post = updateObject.$set
  if(post.url){
    var data = getEmbedlyData(post.url);
    if(!!data && !!data.media.html)
      updateObject.$set.media = data.media
  }
  return updateObject;
}
postEditMethodCallbacks.push(updateMediaOnEdit);
{'fraction_non_alphanumeric': 0.1179559684912139, 'fraction_numerical': 0.0060593819430418095, 'mean_word_length': 3.3629955947136563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6199629', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1509, 'n_words': 427}