Skip to content

Commit

Permalink
refactor(logo): use google logo endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 14, 2022
1 parent d783e68 commit 9fb6a71
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
19 changes: 10 additions & 9 deletions packages/metascraper-logo-favicon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const memoize = require('@keyvhq/memoize')
const {
logo,
parseUrl,
memoizeOne,
normalizeUrl,
toRule,
url: urlFn
Expand Down Expand Up @@ -106,19 +105,23 @@ const pickBiggerSize = sizes => {
pickBiggerSize.sortBySize = collection =>
orderBy(collection, ['size.priority'], ['desc'])

const getFaviconUrl = memoizeOne(url => logo('/favicon.ico', { url }))

const createGetLogo = ({ gotOpts, keyvOpts }) => {
const getLogo = async url => {
const faviconUrl = getFaviconUrl(url)
const faviconUrl = logo('/favicon.ico', { url })
if (!faviconUrl) return

const response = await reachableUrl(faviconUrl, gotOpts)
return reachableUrl.isReachable(response) ? faviconUrl : undefined
let response = await reachableUrl(faviconUrl, gotOpts)
if (reachableUrl.isReachable(response)) return faviconUrl

response = await reachableUrl(
`https://www.google.com/s2/favicons?domain_url=${url}&sz=128`,
gotOpts
)

return reachableUrl.isReachable(response) ? response.url : undefined
}

return memoize(getLogo, keyvOpts, {
key: getFaviconUrl,
value: value => (value === undefined ? null : value)
})
}
Expand All @@ -145,5 +148,3 @@ module.exports = ({ gotOpts, keyvOpts, pickFn = pickBiggerSize } = {}) => {
]
}
}

module.exports.createGetLogo = createGetLogo
32 changes: 10 additions & 22 deletions packages/metascraper-logo-favicon/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const test = require('ava')

const createMetascraper = opts => require('metascraper')([require('..')(opts)])

const { createGetLogo } = require('..')

const createHtml = meta =>
`<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -54,18 +52,6 @@ test('`pickFn` gets the bigger size by default', async t => {
)
})

test('.createGetLogo dedupes key from input', async t => {
const cache = new Map()
const getLogo = createGetLogo({ keyvOpts: { store: cache } })

await getLogo('https://help.kinopio.club/about/')
await getLogo('https://help.kinopio.club/legal/')
await getLogo('https://help.kinopio.club/')
await getLogo('https://kinopio.club/')

t.is(cache.size, 2)
})

test('create an absolute favicon url if the logo is not present', async t => {
const url = 'https://www.nytimes.com'
const metascraper = createMetascraper()
Expand Down Expand Up @@ -231,17 +217,19 @@ test('use non square logo as in the worst scenario', async t => {
t.is(metadata.logo, 'https://s.yimg.com/kw/assets/eng-e-558x270.png')
})

test('returns null if logo is not detected', async t => {
const url = 'https://liu.edu/Brooklyn'
const html = await readFile(resolve(__dirname, 'fixtures/liuedu.html'))
test('resolve logo using favicon associated with the domain', async t => {
const url = 'https://cdn.teslahunt.io/foo/bar'
const metascraper = createMetascraper()
const metadata = await metascraper({ url, html })
t.is(metadata.logo, null)
const metadata = await metascraper({ url })
t.is(metadata.logo, 'https://teslahunt.io/favicon.ico')
})

test('resolve domain favicon', async t => {
const url = 'https://cdn.teslahunt.io/foo/bar'
test('resolve logo using from google associated with the domain', async t => {
const url = 'https://escritopor.elenatorro.com'
const metascraper = createMetascraper()
const metadata = await metascraper({ url })
t.is(metadata.logo, 'https://teslahunt.io/favicon.ico')
t.is(
metadata.logo,
'https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://elenatorro.com/&size=128'
)
})
3 changes: 2 additions & 1 deletion packages/metascraper/test/integration/silicon-beat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const url = 'http://www.siliconbeat.com/2016/04/19/time-come-woman'

test('silicon-beat', async t => {
const html = await readFile(resolve(__dirname, 'input.html'))
const metadata = await metascraper({ html, url })
const { logo, ...metadata } = await metascraper({ html, url })
t.is(typeof logo, 'string')
t.snapshot(metadata)
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Generated by [AVA](https://avajs.dev).
description: 'It’s no secret that few women hold positions of power at venture capital firms. A new study quantifies what many in the industry have reported anecdotally. According to CrunchBase data released Tuesday on …',
image: 'http://www.siliconbeat.com/wp-content/uploads/2016/04/Panel.jpg',
lang: 'en',
logo: null,
publisher: 'SiliconBeat',
title: 'VC: Time to ‘come out as a woman’',
url: 'http://www.siliconbeat.com/2016/04/19/time-come-woman/',
Expand Down
Binary file not shown.

0 comments on commit 9fb6a71

Please sign in to comment.