Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 19, 2018
1 parent da04a11 commit fd54287
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
35 changes: 35 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const isEmail = require('is-email-like')
const aigle = require('aigle')
const got = require('got')

const { services, servicesBy } = require('./services')
const send = require('./send')

const getAvatarUrl = key => {
const collection = isEmail(key) ? servicesBy.email : servicesBy.username
return aigle
.resolve(collection)
.map(service => services[service](key))
.find(url => got.head(url))
}

const createGetAvatarUrl = ({
urlFn = getAvatarUrl,
isJSON = false
} = {}) => async (req, res) => {
const { key } = req.params
let url = null

try {
url = await urlFn(key)
} catch (err) {}

return send({ url, req, res, isJSON, key, isError: url === null })
}

module.exports = {
getAvatarUrl,
createGetAvatarUrl
}
53 changes: 7 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
'use strict'

const isEmail = require('is-email-like')
const { forEach } = require('lodash')
const aigle = require('aigle')
const got = require('got')

const isProduction = process.env.NODE_ENV === 'production'

const { services, servicesBy } = require('./services')

const sendAvatar = ({ url, res }) => {
const stream = got.stream(url)
stream.on('response', resAvatar =>
res.set('Content-Type', resAvatar.headers['content-type'])
)
return stream.pipe(res)
}

const createAvatarFromService = (fn, { json }) => async (req, res) => {
const { key } = req.params

try {
const url = await fn(key)
return json ? res.json({ url }) : sendAvatar({ res, url })
} catch (err) {
return json ? res.json({ url: null }) : res.status(404).send()
}
}

const createAvatarBy = ({ json }) => async (req, res) => {
const { key } = req.params
const collection = isEmail(key) ? servicesBy.email : servicesBy.username

try {
const url = await aigle
.resolve(collection)
.map(service => services[service](key))
.find(url => got.head(url))
return json ? res.json({ url }) : sendAvatar({ res, url })
} catch (err) {
return json ? res.json({ url: null }) : res.status(404).send()
}
}
const { createGetAvatarUrl } = require('./helpers')
const { services } = require('./services')

module.exports = (app, express) => {
app
Expand All @@ -56,15 +20,12 @@ module.exports = (app, express) => {
app.get('/robots.txt', (req, res) => res.status(204).send())
app.get('/favicon.txt', (req, res) => res.status(204).send())

app.get(`/:key`, createAvatarBy({ json: false }))
app.get(`/:key/json`, createAvatarBy({ json: true }))
app.get(`/:key`, createGetAvatarUrl())
app.get(`/:key/json`, createGetAvatarUrl({ json: true }))

forEach(services, (fn, service) => {
app.get(`/${service}/:key`, createAvatarFromService(fn, { json: false }))
app.get(
`/${service}/:key/json`,
createAvatarFromService(fn, { json: true })
)
forEach(services, (urlFn, service) => {
app.get(`/${service}/:key`, createGetAvatarUrl({ urlFn }))
app.get(`/${service}/:key/json`, createGetAvatarUrl({ urlFn, json: true }))
})

app.use(express.static('static'))
Expand Down
26 changes: 26 additions & 0 deletions src/send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const got = require('got')

const sendAvatar = ({ url, res }) => {
const stream = got.stream(url)
stream.on('response', resAvatar =>
res.set('Content-Type', resAvatar.headers['content-type'])
)
return stream.pipe(res)
}

const sendError = ({ url, req, res, isJSON }) => {
return isJSON ? res.json({ url }) : res.status(404).send()
}

const sendSuccess = ({ url, req, res, isJSON }) => {
return isJSON ? res.json({ url }) : sendAvatar({ res, url })
}

const send = ({ url, req, res, isJSON, isError }) => {
const sendMethod = isError ? sendError : sendSuccess
return sendMethod({ url, req, res, isJSON, isError })
}

module.exports = send
11 changes: 7 additions & 4 deletions src/services/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ const { get } = require('lodash')
const { JSDOM } = require('jsdom')
const got = require('got')

const AVATAR_URL = {
normal:
'_sharedData.entry_data.ProfilePage[0].graphql.user.profile_pic_url_hd',
bigger: '_sharedData.entry_data.ProfilePage[0].graphql.user.profile_pic_url'
}

module.exports = async username => {
const { body } = await got(`https://www.instagram.com/${username}`)
const { window } = new JSDOM(body, { runScripts: 'dangerously' })
return get(
window,
'_sharedData.entry_data.ProfilePage[0].graphql.user.profile_pic_url_hd'
)
return get(window, AVATAR_URL.bigger) || get(window, AVATAR_URL.normal)
}

module.exports.supported = {
Expand Down

0 comments on commit fd54287

Please sign in to comment.