|
1 | 1 | //import { promises as fsPromises } from 'fs'; |
2 | | -import Router from '@koa/router'; |
| 2 | +import handlebars from "handlebars"; |
| 3 | +import Router from "@koa/router"; |
3 | 4 | //import * as path from 'path'; |
4 | 5 |
|
| 6 | +import * as util from "../util.js"; |
| 7 | +import config from "../config.js"; |
| 8 | + |
5 | 9 | const infoRouter = new Router(); |
6 | 10 |
|
7 | | -infoRouter.get('/info/', async (ctx) => { |
8 | | - ctx.redirect('/info/index.html'); |
| 11 | +infoRouter.get("/info/", async (ctx) => { |
| 12 | + ctx.redirect("/info/index.html"); |
| 13 | +}); |
| 14 | + |
| 15 | +infoRouter.get("/info/index.html", async (ctx) => { |
| 16 | + ctx.redirect("/tools.html#info"); |
9 | 17 | }); |
10 | 18 |
|
11 | | -infoRouter.get('/info/index.html', async (ctx) => { |
12 | | - ctx.redirect('/tools.html#info'); |
| 19 | +infoRouter.get("/info/glossary.html", async (ctx: any) => { |
| 20 | + ctx.body = await ctx.render("info/glossary.hbs", { |
| 21 | + title: "Glossary", |
| 22 | + }); |
13 | 23 | }); |
14 | 24 |
|
15 | | -infoRouter.get('/info/glossary.html', async (ctx:any) => { |
16 | | - ctx.body = await ctx.render('info/glossary.hbs', { |
17 | | - title: 'Glossary', |
18 | | - }); |
| 25 | +infoRouter.get("/info/steps.html", async (ctx: any) => { |
| 26 | + ctx.body = await ctx.render("info/steps.hbs", { |
| 27 | + title: "Steps to get a web page", |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +infoRouter.get("/info/random-logo.html", async (ctx: any) => { |
| 32 | + |
| 33 | + const resp = await fetch("https://siterank.redirect2.me/api/random.json"); |
| 34 | + const json = await resp.json(); |
| 35 | + const domain = json.domain; |
| 36 | + ctx.redirect(`/info/logo-api-comparison.html?domain=${encodeURIComponent(domain)}`); |
19 | 37 | }); |
20 | 38 |
|
21 | | -infoRouter.get('/info/steps.html', async (ctx:any) => { |
22 | | - ctx.body = await ctx.render('info/steps.hbs', { |
23 | | - title: 'Steps to get a web page', |
24 | | - }); |
| 39 | +type LogoResult = { |
| 40 | + provider: string; |
| 41 | + providerUrl: string; |
| 42 | + logoUrl: string; |
| 43 | +} |
| 44 | + |
| 45 | +infoRouter.get("/info/logo-api-comparison.html", async (ctx: any) => { |
| 46 | + let domain = ctx.request.query.domain; |
| 47 | + let loadLogos = true; |
| 48 | + if (!domain) { |
| 49 | + ctx.flash("info", new handlebars.SafeString(`This is a comparison of <a href="https://andrew.marcuse.info/blog/2024/2024-12-11-logo-apis.html">Logo APIs</a> that have free/public tiers.`)); |
| 50 | + loadLogos = false; |
| 51 | + } |
| 52 | + |
| 53 | + if (loadLogos && !util.hasValidPublicSuffix(domain)) { |
| 54 | + try { |
| 55 | + const url = new URL(domain); |
| 56 | + domain = url.hostname; |
| 57 | + } catch (err) { |
| 58 | + ctx.flash( |
| 59 | + "error", |
| 60 | + `Unable to extract a domain from "${handlebars.escapeExpression( |
| 61 | + domain |
| 62 | + )}"!` |
| 63 | + ); |
| 64 | + loadLogos = false; |
| 65 | + } |
| 66 | + if (!util.hasValidPublicSuffix(domain)) { |
| 67 | + ctx.flash( |
| 68 | + "error", |
| 69 | + `${handlebars.escapeExpression( |
| 70 | + domain |
| 71 | + )} is not a valid domain!` |
| 72 | + ); |
| 73 | + loadLogos = false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + const results: LogoResult[] = []; |
| 78 | + if (loadLogos) { |
| 79 | + if (config.get("brandfetchApiKey")) { |
| 80 | + results.push({ |
| 81 | + provider: "Brandfetch", |
| 82 | + providerUrl: "https://brandfetch.com/developers", |
| 83 | + logoUrl: `https://cdn.brandfetch.io/${domain}/w/256/h/256?c=${config.get( |
| 84 | + "brandfetchApiKey" |
| 85 | + )}`, |
| 86 | + }); |
| 87 | + } |
| 88 | + results.push({ |
| 89 | + provider: "Clearbit", |
| 90 | + providerUrl: "https://clearbit.com/", |
| 91 | + logoUrl: `https://logo.clearbit.com/${domain}`, |
| 92 | + }); |
| 93 | + results.push({ |
| 94 | + provider: "CUFinder", |
| 95 | + providerUrl: "https://cufinder.io/enrichment-engine/logo-api", |
| 96 | + logoUrl: `https://api.cufinder.io/logo/${domain}`, |
| 97 | + }); |
| 98 | + if (config.get("logodevToken")) { |
| 99 | + results.push({ |
| 100 | + provider: "logo.dev", |
| 101 | + providerUrl: "https://logo.dev", |
| 102 | + logoUrl: `https://img.logo.dev/${domain}?token=${config.get( |
| 103 | + "logodevToken" |
| 104 | + )}`, |
| 105 | + }); |
| 106 | + } |
| 107 | + results.push({ |
| 108 | + provider: "Lucky Logo", |
| 109 | + providerUrl: "https://lucky.logosear.ch/", |
| 110 | + logoUrl: `https://lucky.logosear.ch/logo?url=${domain}`, |
| 111 | + }); |
| 112 | + results.push({ |
| 113 | + provider: "Uplead", |
| 114 | + providerUrl: "https://www.uplead.com/free-company-logo-api/", |
| 115 | + logoUrl: `https://logo.uplead.com/${domain}`, |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + ctx.body = await ctx.render("info/logo-api-comparison.hbs", { |
| 120 | + title: "Logo API comparision", |
| 121 | + domain, |
| 122 | + results, |
| 123 | + }); |
25 | 124 | }); |
26 | 125 |
|
27 | | -function getUrls():string[] { |
| 126 | +function getUrls(): string[] { |
28 | 127 | return [ |
29 | 128 | "/info/glossary.html", |
30 | 129 | "/info/steps.html", |
| 130 | + "/info/logo-api-comparison.html", |
31 | 131 | ]; |
32 | 132 | } |
33 | 133 |
|
34 | | -export { |
35 | | - getUrls, |
36 | | - infoRouter |
37 | | -} |
| 134 | +export { getUrls, infoRouter }; |
0 commit comments