-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #13 Based on #21 Changes compared with the original PR: - Used `got` instead of `request`. - Resolve YouTube usernames, no need to be prefixed with `/user`. In addition, I setup `domain: false` it's oriented for getting a generic url domain avatar and in this case you are using it for getting specific channel avatar based on url. I feel I can refactor `domain` into a more generic field called `url` for adding this behavior.
- Loading branch information
Showing
7 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"name": "unavatar", | ||
"public": true, | ||
"alias": "unavatar.now.sh" | ||
"alias": "unavatar.now.sh", | ||
"env": { | ||
"YOUTUBE_API_KEY": "@youtube_api_key" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const { isNil, chain, get } = require('lodash') | ||
const pAny = require('p-any') | ||
const url = require('url') | ||
const got = require('got') | ||
|
||
const { youtubeApiKey } = require('../constant') | ||
const { URLSearchParams } = url | ||
|
||
const getUrl = async (username, { slugProp }) => { | ||
const parts = url.parse(username).pathname.split('/') | ||
const slug = chain(parts) | ||
.filter(p => p !== '') | ||
.last() | ||
.value() | ||
|
||
const query = new URLSearchParams([ | ||
['part', 'id,snippet'], | ||
[slugProp, slug], | ||
['key', youtubeApiKey] | ||
]).toString() | ||
|
||
const { body } = await got( | ||
'https://content.googleapis.com/youtube/v3/channels', | ||
{ | ||
json: true, | ||
query | ||
} | ||
) | ||
|
||
const avatarUrl = get(body, 'items[0].snippet.thumbnails.medium.url') | ||
if (isNil(avatarUrl)) { | ||
throw new Error(`YouTube avatar not detected for '${slugProp}'`) | ||
} | ||
return avatarUrl | ||
} | ||
|
||
module.exports = async username => { | ||
return pAny([ | ||
getUrl(username, { slugProp: 'forUsername' }), | ||
getUrl(username, { slugProp: 'id' }) | ||
]) | ||
} | ||
|
||
module.exports.supported = { | ||
email: false, | ||
username: true, | ||
domain: false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters