This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
321 additions
and
362 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -22,5 +22,4 @@ jobs: | |
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm run build | ||
- run: npm test |
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 @@ | ||
const url = require('url'); | ||
|
||
const axios = require('axios').default; | ||
const fileType = require('file-type'); | ||
|
||
/** | ||
* @param {string} siteUrl | ||
*/ | ||
function getSiteDomain(siteUrl) { | ||
return url.parse(siteUrl).hostname; | ||
} | ||
|
||
/** | ||
* @param {string} iconUrl | ||
*/ | ||
async function downloadIcon(iconUrl) { | ||
let iconResponse; | ||
try { | ||
iconResponse = await axios.get(iconUrl, { | ||
responseType: 'arraybuffer', | ||
//'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36' | ||
}); | ||
} catch (error) { | ||
if (error.response && error.response.status === 404) { | ||
return null; | ||
} | ||
throw error; | ||
} | ||
|
||
const iconData = await iconResponse.data; | ||
if (!iconData) { | ||
return null; | ||
} | ||
|
||
const fileDetails = await fileType.fromBuffer(iconData); | ||
if (!fileDetails) { | ||
return null; | ||
} | ||
|
||
return { | ||
source: iconUrl, | ||
name: getSiteDomain(iconUrl), | ||
data: iconData, | ||
size: iconData.length, | ||
ext: `.${fileDetails.ext}`, // add `.` to ext | ||
mime: fileDetails.mime, | ||
}; | ||
} | ||
|
||
module.exports = downloadIcon; |
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,12 @@ | ||
const downloadIcon = require('./downloadIcon'); | ||
|
||
function downloadIcons(iconUrls) { | ||
const promises = iconUrls.map(downloadIcon); | ||
|
||
return Promise.all(promises) | ||
.then((iconPaths) => { | ||
return iconPaths.filter(element => !!element); | ||
}); | ||
} | ||
|
||
module.exports = downloadIcons; |
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,24 @@ | ||
function sortIconsBySize(icons) { | ||
return icons.sort((a, b) => { | ||
if (a.size < b.size) { | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
}); | ||
} | ||
|
||
function findBestIcon(icons, ext) { | ||
const sorted = sortIconsBySize(icons); | ||
if (ext) { | ||
for (let icon of sorted) { | ||
if (icon.ext === ext) { | ||
return icon; | ||
} | ||
} | ||
} | ||
|
||
return sorted[0]; | ||
} | ||
|
||
module.exports = findBestIcon; |
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,57 @@ | ||
const url = require('url'); | ||
|
||
const cheerio = require('cheerio'); | ||
|
||
function hrefIsIcon(href) { | ||
return /((icon.*\.(png|jpg))|(\w+\.ico))/.test(href); | ||
} | ||
|
||
function getDomainUrl(someUrl) { | ||
const parsedUrl = url.parse(someUrl); | ||
parsedUrl.pathname = null; | ||
return parsedUrl.format(); | ||
} | ||
|
||
function linkTagLinks($) { | ||
const links = []; | ||
$('link').each(function(index, element) { | ||
const href = $(element).attr('href'); | ||
if (!hrefIsIcon(href)) { | ||
return; | ||
} | ||
links.push(href); | ||
}); | ||
return links; | ||
} | ||
|
||
function metaTagLinks($) { | ||
const links = []; | ||
$('meta').each((index, element) => { | ||
const property = $(element).attr('property'); | ||
if (property !== 'og:image') { | ||
return; | ||
} | ||
|
||
const graphImageUrl = $(element).attr('content'); | ||
links.push(graphImageUrl); | ||
}); | ||
|
||
return links | ||
} | ||
|
||
function getIconLinks(rootUrl, dom) { | ||
var $ = cheerio.load(dom); | ||
let iconLinks = []; | ||
|
||
iconLinks = iconLinks.concat(linkTagLinks($)); | ||
iconLinks = iconLinks.concat(metaTagLinks($)); | ||
|
||
iconLinks = iconLinks.map(iconLink => { | ||
return url.resolve(rootUrl, iconLink); | ||
}); | ||
|
||
iconLinks.push(url.resolve(getDomainUrl(rootUrl), 'apple-touch-icon.png')); | ||
return iconLinks; | ||
} | ||
|
||
module.exports = getIconLinks; |
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,15 @@ | ||
const axios = require('axios').default; | ||
|
||
function getPage(pageUrl) { | ||
return new Promise(function(resolve, reject) { | ||
axios.get(pageUrl) | ||
.then(function (response) { | ||
resolve(response.data); | ||
}) | ||
.catch(function (response) { | ||
reject(response); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = getPage; |
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,41 +1,40 @@ | ||
import 'source-map-support/register'; | ||
|
||
const url = require('url'); | ||
const getPage = require('./modules/getPage'); | ||
const getIconLinks = require('./modules/getIconLinks'); | ||
const downloadIcons = require('./modules/download/downloadIcons'); | ||
const findBestIcon = require('./modules/findBestIcon'); | ||
|
||
const downloadIcons = require('./downloadIcons'); | ||
const findBestIcon = require('./findBestIcon'); | ||
const getIconLinks = require('./getIconLinks'); | ||
const getPage = require('./getPage'); | ||
|
||
function isHttps(pageUrl) { | ||
return url.parse(pageUrl).protocol === 'https:'; | ||
return url.parse(pageUrl).protocol === 'https:'; | ||
} | ||
|
||
function makeHttps(pageUrl) { | ||
const parsed = url.parse(pageUrl); | ||
parsed.protocol = 'https:'; | ||
return url.format(parsed); | ||
const parsed = url.parse(pageUrl); | ||
parsed.protocol = 'https:'; | ||
return url.format(parsed); | ||
} | ||
|
||
function main(pageUrl, options={}) { | ||
|
||
const bestWithPref = function(icons) { | ||
return findBestIcon(icons, options.ext); | ||
}; | ||
|
||
return getPage(pageUrl) | ||
.then(function (dom) { | ||
return getIconLinks(pageUrl, dom); | ||
}) | ||
.then(downloadIcons) | ||
.then(bestWithPref) | ||
.then(result => { | ||
if (result || isHttps(pageUrl)) { | ||
return result; | ||
} | ||
|
||
const httpsUrl = makeHttps(pageUrl); | ||
return main(httpsUrl, options); | ||
}); | ||
const bestWithPref = function(icons) { | ||
return findBestIcon(icons, options.ext); | ||
}; | ||
|
||
return getPage(pageUrl) | ||
.then(function (dom) { | ||
return getIconLinks(pageUrl, dom); | ||
}) | ||
.then(downloadIcons) | ||
.then(bestWithPref) | ||
.then(result => { | ||
if (result || isHttps(pageUrl)) { | ||
return result; | ||
} | ||
|
||
const httpsUrl = makeHttps(pageUrl); | ||
return main(httpsUrl, options); | ||
}); | ||
} | ||
|
||
module.exports = main; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.