UNPKG

2.53 kBPlain TextView Raw
1import { Cooparser, ParseResponse } from './type';
2import * as cheerio from 'cheerio';
3import axios from 'axios';
4
5class CooparserImpl implements Cooparser {
6 private static instance: CooparserImpl;
7 private constructor () { }
8
9 public static getInstance () {
10 return this.instance || (this.instance = new this());
11 }
12
13 public async parse (url: string): Promise<ParseResponse> {
14 const html = await this.returnHTML(url);
15
16 const $ = cheerio.load(html);
17 const title = $("meta[property='og:title']").attr('content') ?? $('title').text();
18 const content = $("meta[property='og:description']").attr('content') ?? '';
19 const provider = $("meta[property='og:site_name']").attr('content') ?? '-';
20 const favicon = await this.findFavicon(html, $, url);
21
22 let thumbnail = $("meta[property='og:image']").attr('content') ?? '';
23 if (thumbnail.length >= 2 && thumbnail[0] === '/' && thumbnail[1] === '/') {
24 thumbnail = 'https:' + thumbnail;
25 }
26
27 const data = {
28 title,
29 content,
30 link: url,
31 thumbnail,
32 favicon,
33 provider
34 };
35
36 return data;
37 };
38
39 public async parseList (urlList: string[]): Promise<ParseResponse[]> {
40 return await Promise.all(urlList.map(url => {
41 return this.parse(url);
42 }))
43 }
44
45 private async returnHTML (url: string) {
46 const response = await this.getHTML(url);
47 return response.data;
48 };
49
50 private async getHTML (url: string) {
51 try {
52 return await axios.get(url);
53 } catch (error) {
54 console.log('[GET HTML] ERROR: ', error);
55 }
56 };
57
58 private async findFavicon (html: any, $: cheerio.Root, url: string) {
59 const shortcutIconURL = $(`link[rel="shortcut icon"]`).attr('href') ?? undefined;
60 const appleIconURL = $(`link[rel="apple-touch-icon"]`).attr('href') ?? undefined;
61 const iconURL = (shortcutIconURL ? shortcutIconURL : appleIconURL) ?? '';
62 if (iconURL === '' || iconURL[0] !== '/') {
63 return iconURL;
64 }
65 const httpPrefix = 'https:';
66 const faviconURL = iconURL[1] === '/' ? (httpPrefix + iconURL) : (this.getURLDomain(url) + iconURL);
67 return faviconURL;
68 };
69
70 private getURLDomain (url: string) {
71 const end = url.indexOf('/', url.indexOf('/') + 2);
72 return url.substring(0, end);
73 };
74}
75
76export default CooparserImpl;
\No newline at end of file