network-avatar-picker
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -5,3 +5,8 @@ 'use strict'; | ||
var networks = require('./networks'); | ||
var facebook = require('./providers/facebook'); | ||
var github = require('./providers/github'); | ||
var instagram = require('./providers/instagram'); | ||
var tumblr = require('./providers/tumblr'); | ||
var twitter = require('./providers/twitter'); | ||
var vimeo = require('./providers/vimeo'); | ||
@@ -11,7 +16,8 @@ var NetworkAvatarPicker = function NetworkAvatarPicker() { | ||
this.twitter = networks.init('twitter'); | ||
this.instagram = networks.init('instagram'); | ||
this.tumblr = networks.init('tumblr'); | ||
this.vimeo = networks.init('vimeo'); | ||
this.facebook = networks.init('facebook'); | ||
this.facebook = facebook.init(); | ||
this.github = github.init(); | ||
this.instagram = instagram.init(); | ||
this.tumblr = tumblr.init(); | ||
this.twitter = twitter.init('twitter'); | ||
this.vimeo = vimeo.init('vimeo'); | ||
if (new.target === NetworkAvatarPicker) { | ||
@@ -18,0 +24,0 @@ Object.freeze(this); |
@@ -6,12 +6,11 @@ 'use strict'; | ||
var cheerio = require('cheerio'); | ||
var _require2 = require('../utils/errorsService'), | ||
handleRequestErrors = _require2.handleRequestErrors; | ||
var downloadImage = async function downloadImage(url, network) { | ||
var downloadImage = async function downloadImage(url, provider) { | ||
try { | ||
var response = await getPromisified({ url: url, encoding: null }); | ||
handleRequestErrors(response, network); | ||
if (network === 'twitter' && response.headers && response.headers['content-type'] && response.headers['content-type'].includes('text/html')) { | ||
throw new Error('Get ' + network + ' avatar no response body.'); | ||
} | ||
handleRequestErrors(response, provider); | ||
return response.body; | ||
@@ -23,4 +22,29 @@ } catch (error) { | ||
var extractProfileImageUrl = async function extractProfileImageUrl(provider, profileUrl) { | ||
try { | ||
if (!provider) { | ||
throw new Error('Bad provider'); | ||
} | ||
var response = await getPromisified({ | ||
url: profileUrl, | ||
headers: { | ||
'User-Agent': 'request' | ||
}, | ||
encoding: null | ||
}); | ||
handleRequestErrors(response, provider); | ||
var $ = cheerio.load(response.body); | ||
var ogImage = $('meta[property="og:image"]').attr('content'); | ||
if (!ogImage) { | ||
throw new Error(provider + ' get avatar image url not found.'); | ||
} | ||
return ogImage; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
module.exports = { | ||
extractProfileImageUrl: extractProfileImageUrl, | ||
downloadImage: downloadImage | ||
}; |
@@ -1,10 +0,13 @@ | ||
"use strict"; | ||
'use strict'; | ||
var handleRequestErrors = function handleRequestErrors(response, network) { | ||
var handleRequestErrors = function handleRequestErrors(response, provider) { | ||
if (response.statusCode !== 200) { | ||
throw new Error("Get " + network + " avatar failed with status: " + response.statusCode + "."); | ||
throw new Error('Get ' + provider + ' avatar failed with status: ' + response.statusCode + '.'); | ||
} | ||
if (!response.body) { | ||
throw new Error("Get " + network + " avatar unable find image."); | ||
throw new Error('Get ' + provider + ' avatar unable find image.'); | ||
} | ||
if (provider === 'twitter' && response.headers && response.headers['content-type'] && response.headers['content-type'].includes('text/html')) { | ||
throw new Error('Get ' + provider + ' avatar no response body.'); | ||
} | ||
}; | ||
@@ -11,0 +14,0 @@ |
{ | ||
"name": "network-avatar-picker", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "A picker for user's networks profile image.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,11 @@ # network-avatar-picker | ||
## Supported Providers | ||
- github | ||
- tumblr | ||
- vimeo | ||
## Usage | ||
@@ -26,4 +35,6 @@ | ||
- `avatarPicker.vimeo.getAvatar(username)` | ||
- `avatarPicker.facebook.getAvatar(userId)` (you should send fb user id!) | ||
- `avatarPicker.facebook.getAvatar(username)` | ||
- `avatarPicker.github.getAvatar(username)` | ||
## Example | ||
@@ -44,2 +55,1 @@ | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19822
151
53