mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-08-07 00:33:59 +09:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
1bab7afee6 | |||
441e8c22f9 | |||
376bba9c61 | |||
028b2fed2f | |||
90d5d0f33b | |||
9e955d8d04 |
@ -1,3 +1,7 @@
|
||||
4.0.1 / 2023-03-16
|
||||
------------------
|
||||
* oEmbedの読み込みでエラーが発生した際は、エラーにせずplayerの中身をnullにするように
|
||||
|
||||
4.0.0 / 2023-03-14
|
||||
------------------
|
||||
* oEmbed type=richの制限的なサポート
|
||||
|
42
README.md
42
README.md
@ -98,27 +98,38 @@ See [Permissions Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Permi
|
||||
|
||||
### Example
|
||||
|
||||
``` javascript
|
||||
```javascript
|
||||
import { summaly } from 'summaly';
|
||||
|
||||
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
|
||||
|
||||
console.log(summary); // will be ... ↓
|
||||
/*
|
||||
console.log(summary);
|
||||
```
|
||||
|
||||
will be ... ↓
|
||||
|
||||
```json
|
||||
{
|
||||
title: '【楽曲試聴】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)',
|
||||
icon: 'https://s.ytimg.com/yts/img/favicon-vfl8qSV2F.ico',
|
||||
description: 'http://columbia.jp/idolmaster/ 2018年7月18日発売予定 THE IDOLM@STER CINDERELLA GIRLS CG STAR LIVE Stage Bye Stage 歌:島村卯月、渋谷凛、本田未央 COCC-17495[CD1枚組] ¥1,200+税 収録内容 Tr...',
|
||||
thumbnail: 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg',
|
||||
player: {
|
||||
url: 'https://www.youtube.com/embed/NMIEAhH_fTU',
|
||||
width: 1280,
|
||||
height: 720
|
||||
"title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)",
|
||||
"icon": "https://www.youtube.com/s/desktop/9318de79/img/favicon.ico",
|
||||
"description": "Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...",
|
||||
"thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg",
|
||||
"player": {
|
||||
"url": "https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed",
|
||||
"width": 200,
|
||||
"height": 113,
|
||||
"allow": [
|
||||
"autoplay",
|
||||
"clipboard-write",
|
||||
"encrypted-media",
|
||||
"picture-in-picture",
|
||||
"web-share"
|
||||
]
|
||||
},
|
||||
sitename: 'YouTube',
|
||||
url: 'https://www.youtube.com/watch?v=NMIEAhH_fTU'
|
||||
"sitename": "YouTube",
|
||||
"sensitive": false,
|
||||
"url": "https://www.youtube.com/watch?v=NMIEAhH_fTU"
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
Testing
|
||||
@ -129,5 +140,8 @@ License
|
||||
----------------------------------------------------------------
|
||||
[MIT](LICENSE)
|
||||
|
||||
[mit]: http://opensource.org/licenses/MIT
|
||||
[mit-badge]: https://img.shields.io/badge/license-MIT-444444.svg?style=flat-square
|
||||
[himasaku]: https://himasaku.net
|
||||
[himawari-badge]: https://img.shields.io/badge/%E5%8F%A4%E8%B0%B7-%E5%90%91%E6%97%A5%E8%91%B5-1684c5.svg?style=flat-square
|
||||
[sakurako-badge]: https://img.shields.io/badge/%E5%A4%A7%E5%AE%A4-%E6%AB%BB%E5%AD%90-efb02a.svg?style=flat-square
|
||||
|
@ -15,7 +15,21 @@ async function getOEmbedPlayer($, pageUrl) {
|
||||
if (!href) {
|
||||
return null;
|
||||
}
|
||||
const oEmbed = await get((new URL(href, pageUrl)).href);
|
||||
const oEmbedUrl = (() => {
|
||||
try {
|
||||
return new URL(href, pageUrl);
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
if (!oEmbedUrl) {
|
||||
return null;
|
||||
}
|
||||
const oEmbed = await get(oEmbedUrl.href).catch(() => null);
|
||||
if (!oEmbed) {
|
||||
return null;
|
||||
}
|
||||
const body = (() => {
|
||||
try {
|
||||
return JSON.parse(oEmbed);
|
||||
|
56
package-lock.json
generated
56
package-lock.json
generated
@ -1,17 +1,17 @@
|
||||
{
|
||||
"name": "summaly",
|
||||
"version": "3.0.4",
|
||||
"version": "4.0.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "summaly",
|
||||
"version": "3.0.4",
|
||||
"version": "4.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"cheerio": "1.0.0-rc.12",
|
||||
"escape-regexp": "0.0.1",
|
||||
"got": "^12.5.3",
|
||||
"got": "^12.6.0",
|
||||
"html-entities": "2.3.2",
|
||||
"iconv-lite": "0.6.3",
|
||||
"jschardet": "3.0.0",
|
||||
@ -25,7 +25,6 @@
|
||||
"@types/cheerio": "0.22.18",
|
||||
"@types/debug": "4.1.7",
|
||||
"@types/escape-regexp": "^0.0.1",
|
||||
"@types/html-entities": "1.3.4",
|
||||
"@types/node": "16.11.12",
|
||||
"debug": "^4.3.4",
|
||||
"fastify": "^4.13.0",
|
||||
@ -1393,16 +1392,6 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/html-entities": {
|
||||
"version": "1.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz",
|
||||
"integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==",
|
||||
"deprecated": "This is a stub types definition. html-entities provides its own type definitions, so you do not need this installed.",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"html-entities": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/http-cache-semantics": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
|
||||
@ -1838,9 +1827,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/cacheable-request": {
|
||||
"version": "10.2.7",
|
||||
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz",
|
||||
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==",
|
||||
"version": "10.2.8",
|
||||
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
|
||||
"integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
|
||||
"dependencies": {
|
||||
"@types/http-cache-semantics": "^4.0.1",
|
||||
"get-stream": "^6.0.1",
|
||||
@ -2780,14 +2769,14 @@
|
||||
}
|
||||
},
|
||||
"node_modules/got": {
|
||||
"version": "12.5.3",
|
||||
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz",
|
||||
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==",
|
||||
"version": "12.6.0",
|
||||
"resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
|
||||
"integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
|
||||
"dependencies": {
|
||||
"@sindresorhus/is": "^5.2.0",
|
||||
"@szmarczak/http-timer": "^5.0.1",
|
||||
"cacheable-lookup": "^7.0.0",
|
||||
"cacheable-request": "^10.2.1",
|
||||
"cacheable-request": "^10.2.8",
|
||||
"decompress-response": "^6.0.0",
|
||||
"form-data-encoder": "^2.1.2",
|
||||
"get-stream": "^6.0.1",
|
||||
@ -6322,15 +6311,6 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/html-entities": {
|
||||
"version": "1.3.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz",
|
||||
"integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"html-entities": "*"
|
||||
}
|
||||
},
|
||||
"@types/http-cache-semantics": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
|
||||
@ -6656,9 +6636,9 @@
|
||||
"integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w=="
|
||||
},
|
||||
"cacheable-request": {
|
||||
"version": "10.2.7",
|
||||
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz",
|
||||
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==",
|
||||
"version": "10.2.8",
|
||||
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
|
||||
"integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
|
||||
"requires": {
|
||||
"@types/http-cache-semantics": "^4.0.1",
|
||||
"get-stream": "^6.0.1",
|
||||
@ -7360,14 +7340,14 @@
|
||||
"dev": true
|
||||
},
|
||||
"got": {
|
||||
"version": "12.5.3",
|
||||
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz",
|
||||
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==",
|
||||
"version": "12.6.0",
|
||||
"resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
|
||||
"integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
|
||||
"requires": {
|
||||
"@sindresorhus/is": "^5.2.0",
|
||||
"@szmarczak/http-timer": "^5.0.1",
|
||||
"cacheable-lookup": "^7.0.0",
|
||||
"cacheable-request": "^10.2.1",
|
||||
"cacheable-request": "^10.2.8",
|
||||
"decompress-response": "^6.0.0",
|
||||
"form-data-encoder": "^2.1.2",
|
||||
"get-stream": "^6.0.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "summaly",
|
||||
"version": "4.0.0",
|
||||
"version": "4.0.1",
|
||||
"description": "Get web page's summary",
|
||||
"author": "syuilo <syuilotan@yahoo.co.jp>",
|
||||
"license": "MIT",
|
||||
|
@ -20,7 +20,20 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
|
||||
return null;
|
||||
}
|
||||
|
||||
const oEmbed = await get((new URL(href, pageUrl)).href);
|
||||
const oEmbedUrl = (() => {
|
||||
try {
|
||||
return new URL(href, pageUrl);
|
||||
} catch { return null }
|
||||
})();
|
||||
if (!oEmbedUrl) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const oEmbed = await get(oEmbedUrl.href).catch(() => null);
|
||||
if (!oEmbed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const body = (() => {
|
||||
try {
|
||||
return JSON.parse(oEmbed);
|
||||
|
@ -1,2 +1,3 @@
|
||||
<!DOCTYPE html>
|
||||
<link type="application/json+oembed" href="http://localhost:3060/oembe.json" />
|
||||
<meta property="og:description" content="nonexistent">
|
||||
|
3
test/htmls/oembed-wrong-path.html
Normal file
3
test/htmls/oembed-wrong-path.html
Normal file
@ -0,0 +1,3 @@
|
||||
<!DOCTYPE html>
|
||||
<link type="application/json+oembed" href="http://localhost:+3060/oembed.json" />
|
||||
<meta property="og:description" content="wrong url">
|
@ -1,2 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<link type="application/json+oembed" href="http://localhost+:3060/oembed.json" />
|
@ -322,12 +322,16 @@ describe("oEmbed", () => {
|
||||
|
||||
test('oEmbed with nonexistent path', async () => {
|
||||
await setUpFastify('oembed.json', 'htmls/oembed-nonexistent-path.html');
|
||||
await expect(summaly(host)).rejects.toThrow('404 Not Found');
|
||||
const summary = await summaly(host);
|
||||
expect(summary.player.url).toBe(null);
|
||||
expect(summary.description).toBe('nonexistent');
|
||||
});
|
||||
|
||||
test('oEmbed with wrong path', async () => {
|
||||
await setUpFastify('oembed.json', 'htmls/oembed-wrong-path.html');
|
||||
await expect(summaly(host)).rejects.toThrow();
|
||||
const summary = await summaly(host);
|
||||
expect(summary.player.url).toBe(null);
|
||||
expect(summary.description).toBe('wrong url');
|
||||
});
|
||||
|
||||
test('oEmbed with OpenGraph', async () => {
|
||||
|
Reference in New Issue
Block a user