From a787d52af38f2ed9213ea2f31bf07fb86c8a5c5b Mon Sep 17 00:00:00 2001 From: lasconic Date: Wed, 16 Nov 2016 21:27:55 +0100 Subject: [PATCH] fix #30: add environment variable to define user agent --- src/config/environment_vars.js | 4 +++- src/streams/sources/external.js | 13 ++++++++++--- src/streams/sources/facebook.js | 5 ++++- src/streams/sources/twitter.js | 5 ++++- src/streams/sources/vimeo.js | 15 ++++++++++++--- src/streams/sources/youtube.js | 5 ++++- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/config/environment_vars.js b/src/config/environment_vars.js index 040b320..5ce07f5 100644 --- a/src/config/environment_vars.js +++ b/src/config/environment_vars.js @@ -61,7 +61,9 @@ vars = { IMAGE_404: null, // Whitelist arbitrary HTTP source prefixes using EXTERNAL_SOURCE_* - EXTERNAL_SOURCE_WIKIPEDIA: 'https://upload.wikimedia.org/wikipedia/' + EXTERNAL_SOURCE_WIKIPEDIA: 'https://upload.wikimedia.org/wikipedia/', + + USER_AGENT: 'image-resizer' }; diff --git a/src/streams/sources/external.js b/src/streams/sources/external.js index 4c31b46..e8ea358 100644 --- a/src/streams/sources/external.js +++ b/src/streams/sources/external.js @@ -2,8 +2,9 @@ 'use strict'; -var string, stream, util, request; +var env, string, stream, util, request; +env = require('../../config/environment_vars'); string = require('../../utils/string'); stream = require('stream'); util = require('util'); @@ -65,7 +66,13 @@ External.prototype._read = function(){ this.image.log.time('source:' + this.key); - imgStream = request.get(url); + var options = { + url: url, + headers: { + 'User-Agent': env.USER_AGENT + } + }; + imgStream = request.get(options); imgStream.on('data', function(d){ bufs.push(d); }); imgStream.on('error', function(err){ _this.image.error = new Error(err); @@ -91,4 +98,4 @@ External.prototype._read = function(){ }; -module.exports = External; \ No newline at end of file +module.exports = External; diff --git a/src/streams/sources/facebook.js b/src/streams/sources/facebook.js index a2980eb..f7cf1ec 100644 --- a/src/streams/sources/facebook.js +++ b/src/streams/sources/facebook.js @@ -49,7 +49,10 @@ Facebook.prototype._read = function(){ var opts = { url: url, - encoding: null + encoding: null, + headers: { + 'User-Agent': env.USER_AGENT + } }; request(opts, function (err, response, body) { diff --git a/src/streams/sources/twitter.js b/src/streams/sources/twitter.js index 23ddc7f..680863f 100644 --- a/src/streams/sources/twitter.js +++ b/src/streams/sources/twitter.js @@ -87,7 +87,10 @@ Twitter.prototype._read = function(){ var opts = { url: imageUrl, - encoding: null + encoding: null, + headers: { + 'User-Agent': env.USER_AGENT + } }; request(opts, function (err, response, body) { diff --git a/src/streams/sources/vimeo.js b/src/streams/sources/vimeo.js index 41de565..5b4556d 100644 --- a/src/streams/sources/vimeo.js +++ b/src/streams/sources/vimeo.js @@ -42,9 +42,15 @@ Vimeo.prototype._read = function(){ this.image.log.time('source:vimeo'); videoId = this.image.image.split('.')[0]; - url = 'http://vimeo.com/api/v2/video/' + videoId + '.json'; - request(url, function(err, response, body){ + var options = { + url: 'http://vimeo.com/api/v2/video/' + videoId + '.json', + headers: { + 'User-Agent': env.USER_AGENT + } + }; + + request(options, function(err, response, body){ if (err){ _this.image.error = new Error(err); endStream(); @@ -58,7 +64,10 @@ Vimeo.prototype._read = function(){ var opts = { url: imageUrl, - encoding: null + encoding: null, + headers: { + 'User-Agent': env.USER_AGENT + } }; request(opts, function (err, response, body) { diff --git a/src/streams/sources/youtube.js b/src/streams/sources/youtube.js index ae6efa5..9ca2b51 100644 --- a/src/streams/sources/youtube.js +++ b/src/streams/sources/youtube.js @@ -43,7 +43,10 @@ Youtube.prototype._read = function(){ var opts = { url: url, - encoding: null + encoding: null, + headers: { + 'User-Agent': env.USER_AGENT + } }; request(opts, function (err, response, body) {