Skip to content

Commit

Permalink
Merge pull request tripviss#31 from musescore/fix-30
Browse files Browse the repository at this point in the history
fix tripviss#30: add environment variable to define user agent
  • Loading branch information
teohhanhui authored Nov 21, 2016
2 parents 7bff1c2 + a787d52 commit d226667
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/config/environment_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

};

Expand Down
13 changes: 10 additions & 3 deletions src/streams/sources/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -91,4 +98,4 @@ External.prototype._read = function(){
};


module.exports = External;
module.exports = External;
5 changes: 4 additions & 1 deletion src/streams/sources/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/streams/sources/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions src/streams/sources/vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/streams/sources/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d226667

Please sign in to comment.