@madeinbruno/music-plug 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -81
- package/dist/index.d.ts +79 -1
- package/dist/index.js +119 -0
- package/dist/player.d.ts +63 -5
- package/dist/player.js +293 -58
- package/package.json +50 -50
- package/styles.css +426 -232
package/README.md
CHANGED
|
@@ -1,81 +1,108 @@
|
|
|
1
|
-
# @madeinbruno/music-plug
|
|
2
|
-
|
|
3
|
-
글 속 노래 한 곡을, 듣는 사람이 쓰는 음원 앱에서 듣게 하는 플러그예요. [Music plug](https://musicplug.madeinbruno.com)의 듣기 플러그를 다른 사이트에 붙이는 패키지예요.
|
|
4
|
-
|
|
5
|
-
```
|
|
6
|
-
♪ 밤편지 · 아이유 ← 처음 누르면 Music plug 듣기 플러그가 뜬다
|
|
7
|
-
♪ 밤편지 · 아이유 멜론 · 바꾸기 ← 고른 뒤에는 누르면 바로 멜론에서
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
- 처음 누르면 **듣기 플러그**(Music plug 창)가 떠서 어디서 들을지 골라요: YouTube(기본) · 멜론 · 지니 · 벅스 · FLO · VIBE · Spotify · Apple Music · YouTube Music. YouTube는 유튜브 영상이 있으면 이 페이지에서 바로, 없으면 YouTube로 열어요.
|
|
11
|
-
- 고르면 그곳에서 이 곡을 열어요 (폰에서는 앱으로). Music plug가 곡을 찾아 보내요: YouTube·YouTube Music은 그 곡을 바로(시작 시각까지), Apple Music은 곡 페이지, 나머지는 그곳의 검색 결과.
|
|
12
|
-
- 고른 곳은 기억해서 다음부터는 누르면 바로 그곳에서 열어요. 곡 옆의 **바꾸기**로 다시 골라요. madeinbruno.com 아래에서는 Music plug와 같이 기억해요 (쿠키 `mp_service`).
|
|
13
|
-
- YouTube를 고르면 글 안에 유튜브 플레이어를 열어요. 읽어 내려가면 왼쪽 아래에 멈추기·다시 듣기 단추가 떠요. 브라우저가 소리 있는 자동재생을 막아서 한 번은 눌러야 나오고, 유튜브 정책상 플레이어는 보이게 둬요.
|
|
14
|
-
- 곡 정보를 모르면(유튜브에서 제목을 못 받으면) 다른 곳에서 찾을 수 없어서 이 페이지에서 YouTube로만 틀어요.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
import {
|
|
28
|
-
import '@madeinbruno/music-plug/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
|
47
|
-
|
|
48
|
-
| `
|
|
49
|
-
| `
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
듣기
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
1
|
+
# @madeinbruno/music-plug
|
|
2
|
+
|
|
3
|
+
글 속 노래 한 곡을, 듣는 사람이 쓰는 음원 앱에서 듣게 하는 플러그예요. [Music plug](https://musicplug.madeinbruno.com)의 듣기 플러그를 다른 사이트에 붙이는 패키지예요.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
♪ 밤편지 · 아이유 ← 처음 누르면 Music plug 듣기 플러그가 뜬다
|
|
7
|
+
♪ 밤편지 · 아이유 멜론 · 바꾸기 ← 고른 뒤에는 누르면 바로 멜론에서
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- 처음 누르면 **듣기 플러그**(Music plug 창)가 떠서 어디서 들을지 골라요: YouTube(기본) · 멜론 · 지니 · 벅스 · FLO · VIBE · Spotify · Apple Music · YouTube Music. YouTube는 유튜브 영상이 있으면 이 페이지에서 바로, 없으면 YouTube로 열어요.
|
|
11
|
+
- 고르면 그곳에서 이 곡을 열어요 (폰에서는 앱으로). Music plug가 곡을 찾아 보내요: YouTube·YouTube Music은 그 곡을 바로(시작 시각까지), Apple Music은 곡 페이지, 나머지는 그곳의 검색 결과.
|
|
12
|
+
- 고른 곳은 기억해서 다음부터는 누르면 바로 그곳에서 열어요. 곡 옆의 **바꾸기**로 다시 골라요. madeinbruno.com 아래에서는 Music plug와 같이 기억해요 (쿠키 `mp_service`).
|
|
13
|
+
- YouTube를 고르면 글 안에 유튜브 플레이어를 열어요. 읽어 내려가면 왼쪽 아래에 멈추기·다시 듣기 단추가 떠요. 브라우저가 소리 있는 자동재생을 막아서 한 번은 눌러야 나오고, 유튜브 정책상 플레이어는 보이게 둬요.
|
|
14
|
+
- 곡 정보를 모르면(유튜브에서 제목을 못 받으면) 다른 곳에서 찾을 수 없어서 이 페이지에서 YouTube로만 틀어요.
|
|
15
|
+
- **곡은 유튜브 주소뿐 아니라 여러 곳의 곡 링크로 정할 수 있어요** (`songFromUrl`): YouTube · YouTube Music · 멜론 · 지니 · 벅스 · FLO · VIBE · Spotify · Apple Music. 쓰는 사람은 자기가 쓰는 곳의 곡 링크를 넣고, 듣는 사람은 자기가 쓰는 곳에서 들어요. 듣는 사람이 고른 곳이 곡을 정한 링크와 같은 곳이면 찾지 않고 그 링크를 바로 열어요. 유튜브 링크가 아니면 글 안의 플레이어는 없고, YouTube를 고르면 YouTube에서 그 곡을 찾아 열어요.
|
|
16
|
+
|
|
17
|
+
## 설치
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @madeinbruno/music-plug
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 쓰기 (Next.js 예)
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
// 서버 컴포넌트
|
|
27
|
+
import { songFromUrl } from '@madeinbruno/music-plug';
|
|
28
|
+
import { PlugPlayer } from '@madeinbruno/music-plug/player';
|
|
29
|
+
import '@madeinbruno/music-plug/styles.css'; // 앱에서 한 번만
|
|
30
|
+
|
|
31
|
+
export default async function Post({ music }: { music: string }) {
|
|
32
|
+
const link = await songFromUrl(music); // 곡 링크(유튜브·멜론·Spotify …) → 곡 제목·가수 (하루 캐시). 곡 링크가 아니면 null
|
|
33
|
+
if (!link) return null;
|
|
34
|
+
return (
|
|
35
|
+
<PlugPlayer
|
|
36
|
+
song={link.song} // 못 읽었으면 null
|
|
37
|
+
fallbackLabel="테마 음악"
|
|
38
|
+
youtube={link.youtube} // 유튜브 링크일 때만. 있으면 글 안에서 바로 튼다
|
|
39
|
+
source={link} // 듣는 사람이 같은 곳을 고르면 이 링크를 바로 연다
|
|
40
|
+
avoid=".mib-footer"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
| 내보내는 것 | 설명 |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `PlugPlayer` (`/player`) | 곡 한 줄과 듣기 플러그. `song`(제목·가수, 모르면 `null`), `fallbackLabel`, `youtube`(`id`, `start` 초. 있으면 YouTube를 글 안에서, 없으면 YouTube로 연다), `source`(곡을 정한 링크의 `service`·`url`. 듣는 사람이 같은 곳을 고르면 그 링크를 바로 연다), `avoid`(이 요소가 보이면 멈추기 단추를 숨김), `base`(Music plug 주소, 기본 `https://musicplug.madeinbruno.com`) |
|
|
49
|
+
| `PlugSheet` (`/player`) | 듣기 플러그 창만 따로 |
|
|
50
|
+
| `listenPageUrl(song, youtube?)` | Music plug의 고르는 화면 주소 (`/listen`). 링크로만 걸고 싶을 때. 전에 고른 곳이 있으면 그 화면 없이 바로 그곳으로 가요 |
|
|
51
|
+
| `listenGoUrl(service, song, youtube?)` | 고른 곳에서 이 곡을 여는 주소 (`/listen/go`) |
|
|
52
|
+
| `songFromUrl(link)` | 곡 링크 하나로 그 곡을 읽어요 (서버에서): `{ service, url, song, youtube? }`. 유튜브는 oEmbed로 짐작하고, 그 밖의 곳은 Music plug(`/api/song`)가 그곳의 곡 화면에서 읽어 줘요. 곡 링크가 아니면 `null`, 곡을 못 읽으면 `song`이 `null` |
|
|
53
|
+
| `linkService(link)` | 링크가 어느 곳의 곡 링크인지 주소 모양만 보고 알려 줘요 (입력 칸에서 바로 확인할 때). 곡 링크가 아니면 `null` |
|
|
54
|
+
| `youtubeId(url)`, `youtubeStart(url)` | youtube.com · youtu.be · music.youtube.com · shorts 주소에서 영상 번호와 시작 시각(`t=`) |
|
|
55
|
+
| `songFromYoutube(id)` | 유튜브 oEmbed로 영상 제목·채널을 받아 곡 제목·가수를 짐작 (서버에서). 못 받으면 `null` |
|
|
56
|
+
| `songFromVideo(title, channel)`, `cleanTitle(title)` | 위의 짐작만 따로 ("가수 - 제목", "가수 '제목'", 유튜브 뮤직의 "가수 - Topic" 채널, `(Official MV)` 같은 꼬리표 지우기) |
|
|
57
|
+
| `SERVICES`, `getService(id)` | 들을 수 있는 곳 |
|
|
58
|
+
|
|
59
|
+
곡 줄은 둘러싼 글의 색을 따르고, 듣기 플러그 창은 Music plug 색이에요. 멈추기 단추의 바탕은 `--mp-ground`로 바꿀 수 있어요.
|
|
60
|
+
|
|
61
|
+
## 곡 고르기 (쓰는 사람 화면)
|
|
62
|
+
|
|
63
|
+
인스타그램처럼 곡을 검색해서 고르는 칸이에요. form 안에 두면 고른 곡이 숨은 입력(`name`)에 JSON으로 들어가요.
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
'use client';
|
|
67
|
+
import { SongField } from '@madeinbruno/music-plug/player';
|
|
68
|
+
|
|
69
|
+
<SongField name="musicSong" defaultValue={saved ?? null} placeholder="테마 음악 (곡 이름이나 가수)" />
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
// 저장할 때 (서버)
|
|
74
|
+
import { readPickedSong } from '@madeinbruno/music-plug';
|
|
75
|
+
const song = readPickedSong(form.get('musicSong')); // 믿을 수 있는 모양만 남긴다. 없으면 null
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- 치면 Music plug(`/api/search`, Apple 목록)에서 찾아 앨범 그림과 함께 보여 주고, ▶로 30초 미리 들을 수 있어요.
|
|
79
|
+
- 고르면 Music plug(`/api/match`)가 그 곡의 유튜브 영상을 찾아 같이 담아요 (`youtube.id`). 글 화면에서는 `<PlugPlayer song={...} youtube={song.youtube} source={{ service: 'apple', url: song.apple }} />`처럼 쓰면 YouTube는 글 안에서 무료로, Apple Music은 그 곡으로 바로 열어요.
|
|
80
|
+
- 고른 곡: `PickedSong` = `{ title, artist, album?, artwork?, apple?, youtube?: { id } }`. 따로 부르려면 `searchSongs(q)`, `matchYoutube(song)`.
|
|
81
|
+
|
|
82
|
+
## Music plug 쪽
|
|
83
|
+
|
|
84
|
+
듣기 플러그의 본체는 Music plug 앱(`apps/music-plug`)에 있어요.
|
|
85
|
+
- `/listen?t=제목&a=가수[&v=유튜브번호][&start=초][&pick=1]`: 어디서 들을지 고르는 화면. 전에 고른 곳이 있으면 바로 그곳으로 (`pick=1`이면 다시 고른다). 공유받은 플레이리스트의 곡마다 붙은 '듣기'도 여기로 와요.
|
|
86
|
+
- `/api/song?url=곡링크`: 곡 링크 하나로 제목·가수를 읽어 줘요 (`src/server/song`). `songFromUrl`이 불러요.
|
|
87
|
+
- `/api/search?q=…`: 곡 검색 (Apple iTunes 검색, 하루 캐시). `/api/match?t=제목&a=가수`: 그 곡의 유튜브 영상 찾기 (YouTube Data API, Music plug 환경변수 `YOUTUBE_API_KEY`, 하루 약 100곡, 같은 곡은 30일 캐시, madeinbruno.com 아래 화면에서만).
|
|
88
|
+
- `/listen/go?s=고른곳&…[&remember=1]`: 그곳에서 이 곡을 찾아 보내요 (`src/server/listen.ts`). `remember=1`이면 고른 곳을 기억해요.
|
|
89
|
+
|
|
90
|
+
## 개발
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pnpm --filter @madeinbruno/music-plug test # 곡 정보 짐작 시험
|
|
94
|
+
pnpm --filter @madeinbruno/music-plug build # dist/ 생성
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 배포 (npm)
|
|
98
|
+
|
|
99
|
+
atelier와 같아요 (`.github/workflows/publish-music-plug.yml`). `package.json`의 `version`을 올려 `main`에 푸시하면 Actions가 npm 승인 대기열에 올리고, npmjs.com에서 승인하면 공개돼요.
|
|
100
|
+
|
|
101
|
+
**처음 한 번은 손으로 올려야 해요.** npm의 trusted publishing은 이미 있는 패키지에만 설정할 수 있어서요.
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
npm login
|
|
105
|
+
cd packages/music-plug && pnpm install && pnpm build && npm publish --access public
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
그다음 npmjs.com → `@madeinbruno/music-plug` → Settings → Trusted publishing에 저장소 `codingbotPark/madeinbruno`, 워크플로 `publish-music-plug.yml`을 등록해 두면, 다음 버전부터는 Actions가 올려요.
|
package/dist/index.d.ts
CHANGED
|
@@ -68,4 +68,82 @@ declare const songQuery: (song: Song) => string;
|
|
|
68
68
|
*/
|
|
69
69
|
declare function songFromYoutube(id: string): Promise<Song | null>;
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
/**
|
|
72
|
+
* 곡 링크. 테마 음악을 유튜브뿐 아니라 쓰는 사람이 쓰는 음원 앱의 곡 링크로도 정할 수 있게 한다.
|
|
73
|
+
* 받는 곳: YouTube · YouTube Music · 멜론 · 지니 · 벅스 · FLO · VIBE · Spotify · Apple Music
|
|
74
|
+
*/
|
|
75
|
+
type SongLink = {
|
|
76
|
+
/** 링크가 가리키는 곳 ('ytvideo'는 YouTube 영상, 'youtube'는 YouTube Music) */
|
|
77
|
+
service: ServiceId;
|
|
78
|
+
/** 곡 주소 (짧은 주소는 풀어서) */
|
|
79
|
+
url: string;
|
|
80
|
+
/** 곡 제목·가수. 못 읽었으면 null */
|
|
81
|
+
song: Song | null;
|
|
82
|
+
/** 유튜브 링크일 때만: 영상 번호와 시작 시각(초). 있으면 글 안에서 바로 틀 수 있다 */
|
|
83
|
+
youtube?: {
|
|
84
|
+
id: string;
|
|
85
|
+
start?: number;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* 이 링크가 어느 곳의 곡 링크인지 (주소 모양만 본다. 화면에서 입력을 바로 확인할 때 쓴다). 곡 링크가 아니면 null.
|
|
90
|
+
* 앱의 공유 버튼이 주는 짧은 주소(kko.to, flomuz.io, spotify.link, naver.me)는 열어 봐야 알아서 그곳의 곡으로 친다.
|
|
91
|
+
*/
|
|
92
|
+
declare function linkService(input: string | null | undefined): ServiceId | null;
|
|
93
|
+
/**
|
|
94
|
+
* 곡 링크 하나로 그 곡을 읽는다 (서버에서). 곡 링크가 아니면 null.
|
|
95
|
+
* - 유튜브·YouTube Music: 유튜브 oEmbed로 제목·채널을 받아 짐작한다 (songFromYoutube)
|
|
96
|
+
* - 그 밖의 곳: Music plug(/api/song)가 그곳의 곡 화면에서 제목·가수를 읽어 준다
|
|
97
|
+
* 곡을 못 읽어도(song이 null) 링크는 돌려주니, 그 링크로는 들을 수 있다. Next.js에서는 하루 캐시한다.
|
|
98
|
+
*/
|
|
99
|
+
declare function songFromUrl(input: string | null | undefined, { base }?: {
|
|
100
|
+
base?: string | undefined;
|
|
101
|
+
}): Promise<SongLink | null>;
|
|
102
|
+
/**
|
|
103
|
+
* 곳마다 직접 정해 둔 곡 링크 (예: { melon: 'https://www.melon.com/song/detail.htm?songId=…', apple: '…' }).
|
|
104
|
+
* 곡을 못 찾아 곡까지 가지 않는 곳이 있을 때, 쓰는 사람이 그곳의 곡 링크를 직접 정해 둔다. 정해 둔 곳은 찾지 않고 그 링크를 연다.
|
|
105
|
+
*/
|
|
106
|
+
type SongLinks = Partial<Record<ServiceId, string>>;
|
|
107
|
+
/** 곡 링크를 직접 정할 수 있는 곳 (YouTube는 테마 음악의 유튜브 영상을 쓰므로 뺀다) */
|
|
108
|
+
declare const LINKABLE_SERVICES: ServiceId[];
|
|
109
|
+
/**
|
|
110
|
+
* 받은 링크들에서 그곳의 곡 링크가 맞는 것만 남긴다 (폼·저장된 값을 믿지 않고 거른다). 남은 게 없으면 null.
|
|
111
|
+
* 글과 함께 복사된 것("제목 https://…")이면 주소만 남긴다.
|
|
112
|
+
*/
|
|
113
|
+
declare function cleanSongLinks(input: unknown): SongLinks | null;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 곡 고르기(SongField)로 고른 곡. 글에 그대로 저장한다.
|
|
117
|
+
* - youtube: 고를 때 Music plug가 찾아 준 유튜브 영상 (있으면 글 안에서 무료로 바로 튼다)
|
|
118
|
+
* - apple: Apple Music 곡 주소 (듣는 사람이 Apple Music을 고르면 찾지 않고 바로 연다)
|
|
119
|
+
*/
|
|
120
|
+
type PickedSong = {
|
|
121
|
+
title: string;
|
|
122
|
+
artist: string;
|
|
123
|
+
album?: string;
|
|
124
|
+
artwork?: string;
|
|
125
|
+
apple?: string;
|
|
126
|
+
youtube?: {
|
|
127
|
+
id: string;
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
/** 곡 검색 결과 한 줄 (미리 듣기는 고르는 동안만 쓴다) */
|
|
131
|
+
type FoundSong = Omit<PickedSong, 'youtube'> & {
|
|
132
|
+
preview?: string;
|
|
133
|
+
};
|
|
134
|
+
/** 저장해 둔 값(JSON)을 믿을 수 있는 모양으로 (모르는 것은 버린다). 곡 제목이 없으면 null */
|
|
135
|
+
declare function readPickedSong(value: unknown): PickedSong | null;
|
|
136
|
+
/** Music plug 곡 검색 (/api/search) */
|
|
137
|
+
declare function searchSongs(query: string, { base, signal }?: {
|
|
138
|
+
base?: string;
|
|
139
|
+
signal?: AbortSignal;
|
|
140
|
+
}): Promise<FoundSong[]>;
|
|
141
|
+
/** 고른 곡의 유튜브 영상 찾기 (/api/match). 못 찾으면 null */
|
|
142
|
+
declare function matchYoutube(song: {
|
|
143
|
+
title: string;
|
|
144
|
+
artist: string;
|
|
145
|
+
}, { base }?: {
|
|
146
|
+
base?: string | undefined;
|
|
147
|
+
}): Promise<string | null>;
|
|
148
|
+
|
|
149
|
+
export { type FoundSong, LINKABLE_SERVICES, MUSIC_PLUG_URL, type PickedSong, SERVICES, type Service, type ServiceId, type Song, type SongLink, type SongLinks, cleanSongLinks, cleanTitle, getService, linkService, listenGoUrl, listenPageUrl, matchYoutube, readPickedSong, searchSongs, songFromUrl, songFromVideo, songFromYoutube, songQuery, youtubeId, youtubeStart };
|
package/dist/index.js
CHANGED
|
@@ -88,13 +88,132 @@ async function songFromYoutube(id) {
|
|
|
88
88
|
return null;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
+
|
|
92
|
+
// src/link.ts
|
|
93
|
+
var is = (host, domain) => host === domain || host.endsWith(`.${domain}`);
|
|
94
|
+
function firstUrl(input) {
|
|
95
|
+
const found = input?.match(/https?:\/\/[^\s<>"']+/)?.[0];
|
|
96
|
+
if (!found) return null;
|
|
97
|
+
try {
|
|
98
|
+
return new URL(found);
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function linkService(input) {
|
|
104
|
+
const url = firstUrl(input);
|
|
105
|
+
if (!url) return null;
|
|
106
|
+
const host = url.hostname.toLowerCase();
|
|
107
|
+
const path = url.pathname;
|
|
108
|
+
if (is(host, "youtube.com") || is(host, "youtu.be")) {
|
|
109
|
+
if (!youtubeId(url.href)) return null;
|
|
110
|
+
return is(host, "music.youtube.com") ? "youtube" : "ytvideo";
|
|
111
|
+
}
|
|
112
|
+
if (is(host, "melon.com")) return url.searchParams.has("songId") || /^s/i.test(url.searchParams.get("type") ?? "") ? "melon" : null;
|
|
113
|
+
if (is(host, "kko.to")) return "melon";
|
|
114
|
+
if (is(host, "genie.co.kr")) return url.searchParams.has("xgnm") || !/playlist/i.test(path) ? "genie" : null;
|
|
115
|
+
if (is(host, "bugs.co.kr")) return /\/track\/\d+/.test(path) ? "bugs" : null;
|
|
116
|
+
if (is(host, "music-flo.com")) return /\/detail\/track\//.test(path) ? "flo" : null;
|
|
117
|
+
if (is(host, "flomuz.io")) return "flo";
|
|
118
|
+
if (is(host, "vibe.naver.com")) return /\/track\/\d+/.test(path) ? "vibe" : null;
|
|
119
|
+
if (is(host, "naver.me")) return "vibe";
|
|
120
|
+
if (is(host, "open.spotify.com")) return /\/track\//.test(path) ? "spotify" : null;
|
|
121
|
+
if (is(host, "spotify.link")) return "spotify";
|
|
122
|
+
if (is(host, "music.apple.com")) return url.searchParams.has("i") || /\/song\//.test(path) ? "apple" : null;
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
async function songFromUrl(input, { base = MUSIC_PLUG_URL } = {}) {
|
|
126
|
+
const service = linkService(input);
|
|
127
|
+
const url = firstUrl(input);
|
|
128
|
+
if (!service || !url) return null;
|
|
129
|
+
const id = youtubeId(url.href);
|
|
130
|
+
if (id) {
|
|
131
|
+
const start = youtubeStart(url.href);
|
|
132
|
+
return { service, url: url.href, song: await songFromYoutube(id), youtube: { id, ...start ? { start } : {} } };
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const res = await fetch(`${base}/api/song?url=${encodeURIComponent(url.href)}`, {
|
|
136
|
+
next: { revalidate: 86400 },
|
|
137
|
+
signal: AbortSignal.timeout(1e4)
|
|
138
|
+
});
|
|
139
|
+
if (res.ok) {
|
|
140
|
+
const data = await res.json();
|
|
141
|
+
if (data.title) {
|
|
142
|
+
return { service: data.service ?? service, url: data.url ?? url.href, song: { title: data.title, artist: data.artist ?? "" } };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
} catch {
|
|
146
|
+
}
|
|
147
|
+
return { service, url: url.href, song: null };
|
|
148
|
+
}
|
|
149
|
+
var LINKABLE_SERVICES = ["melon", "genie", "bugs", "flo", "vibe", "spotify", "apple", "youtube"];
|
|
150
|
+
function cleanSongLinks(input) {
|
|
151
|
+
if (!input || typeof input !== "object") return null;
|
|
152
|
+
const out = {};
|
|
153
|
+
for (const id of LINKABLE_SERVICES) {
|
|
154
|
+
const raw = input[id];
|
|
155
|
+
if (typeof raw !== "string") continue;
|
|
156
|
+
const url = firstUrl(raw.slice(0, 1e3));
|
|
157
|
+
if (url && linkService(url.href) === id) out[id] = url.href;
|
|
158
|
+
}
|
|
159
|
+
return Object.keys(out).length ? out : null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/picked.ts
|
|
163
|
+
var str = (v, max) => typeof v === "string" && v.trim() ? v.trim().slice(0, max) : void 0;
|
|
164
|
+
var https = (v) => typeof v === "string" && v.startsWith("https://") && v.length < 1e3 ? v : void 0;
|
|
165
|
+
function readPickedSong(value) {
|
|
166
|
+
let v = value;
|
|
167
|
+
if (typeof v === "string") {
|
|
168
|
+
try {
|
|
169
|
+
v = JSON.parse(v);
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!v || typeof v !== "object") return null;
|
|
175
|
+
const o = v;
|
|
176
|
+
const title = str(o.title, 200);
|
|
177
|
+
if (!title) return null;
|
|
178
|
+
const yt = o.youtube?.id;
|
|
179
|
+
return {
|
|
180
|
+
title,
|
|
181
|
+
artist: str(o.artist, 200) ?? "",
|
|
182
|
+
...str(o.album, 200) ? { album: str(o.album, 200) } : {},
|
|
183
|
+
...https(o.artwork) ? { artwork: https(o.artwork) } : {},
|
|
184
|
+
...https(o.apple)?.startsWith("https://music.apple.com/") ? { apple: https(o.apple) } : {},
|
|
185
|
+
...typeof yt === "string" && /^[\w-]{11}$/.test(yt) ? { youtube: { id: yt } } : {}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
async function searchSongs(query, { base = MUSIC_PLUG_URL, signal } = {}) {
|
|
189
|
+
const res = await fetch(`${base}/api/search?q=${encodeURIComponent(query)}`, { signal });
|
|
190
|
+
const data = await res.json().catch(() => null);
|
|
191
|
+
if (!res.ok || !data?.songs) throw new Error(data?.error ?? "\uC9C0\uAE08\uC740 \uCC3E\uC9C0 \uBABB\uD588\uC5B4\uC694");
|
|
192
|
+
return data.songs;
|
|
193
|
+
}
|
|
194
|
+
async function matchYoutube(song, { base = MUSIC_PLUG_URL } = {}) {
|
|
195
|
+
try {
|
|
196
|
+
const res = await fetch(`${base}/api/match?t=${encodeURIComponent(song.title)}&a=${encodeURIComponent(song.artist)}`);
|
|
197
|
+
const data = await res.json();
|
|
198
|
+
return typeof data.youtube === "string" && /^[\w-]{11}$/.test(data.youtube) ? data.youtube : null;
|
|
199
|
+
} catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
91
203
|
export {
|
|
204
|
+
LINKABLE_SERVICES,
|
|
92
205
|
MUSIC_PLUG_URL,
|
|
93
206
|
SERVICES,
|
|
207
|
+
cleanSongLinks,
|
|
94
208
|
cleanTitle,
|
|
95
209
|
getService,
|
|
210
|
+
linkService,
|
|
96
211
|
listenGoUrl,
|
|
97
212
|
listenPageUrl,
|
|
213
|
+
matchYoutube,
|
|
214
|
+
readPickedSong,
|
|
215
|
+
searchSongs,
|
|
216
|
+
songFromUrl,
|
|
98
217
|
songFromVideo,
|
|
99
218
|
songFromYoutube,
|
|
100
219
|
songQuery,
|
package/dist/player.d.ts
CHANGED
|
@@ -21,6 +21,54 @@ type Song = {
|
|
|
21
21
|
artist: string;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* 곳마다 직접 정해 둔 곡 링크 (예: { melon: 'https://www.melon.com/song/detail.htm?songId=…', apple: '…' }).
|
|
26
|
+
* 곡을 못 찾아 곡까지 가지 않는 곳이 있을 때, 쓰는 사람이 그곳의 곡 링크를 직접 정해 둔다. 정해 둔 곳은 찾지 않고 그 링크를 연다.
|
|
27
|
+
*/
|
|
28
|
+
type SongLinks = Partial<Record<ServiceId, string>>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 곡 고르기(SongField)로 고른 곡. 글에 그대로 저장한다.
|
|
32
|
+
* - youtube: 고를 때 Music plug가 찾아 준 유튜브 영상 (있으면 글 안에서 무료로 바로 튼다)
|
|
33
|
+
* - apple: Apple Music 곡 주소 (듣는 사람이 Apple Music을 고르면 찾지 않고 바로 연다)
|
|
34
|
+
*/
|
|
35
|
+
type PickedSong = {
|
|
36
|
+
title: string;
|
|
37
|
+
artist: string;
|
|
38
|
+
album?: string;
|
|
39
|
+
artwork?: string;
|
|
40
|
+
apple?: string;
|
|
41
|
+
youtube?: {
|
|
42
|
+
id: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 곡 고르기: 인스타그램처럼 곡을 검색해서 고른다 (쓰는 사람 화면).
|
|
48
|
+
*
|
|
49
|
+
* ♪ [곡 이름이나 가수 ]
|
|
50
|
+
* [앨범] 밤편지 ▶
|
|
51
|
+
* 아이유 · Palette
|
|
52
|
+
*
|
|
53
|
+
* - 치면 Music plug(/api/search, Apple 목록)에서 찾아 앨범 그림과 함께 보여 준다. ▶로 30초 미리 듣기.
|
|
54
|
+
* - 고르면 Music plug(/api/match)가 그 곡의 유튜브 영상을 찾아 같이 담는다 (글 안에서 무료로 바로 틀 수 있게).
|
|
55
|
+
* - 고른 곡은 앨범 그림·제목·가수로 보이고, '바꾸기'로 다시 찾고 '빼기'로 비운다.
|
|
56
|
+
* - form 안에 두면 숨은 입력(name)에 고른 곡이 JSON으로 들어간다 (비우면 빈 값). 저장할 때 readPickedSong으로 읽는다.
|
|
57
|
+
* 스타일은 styles.css의 mp-pick. 둘러싼 글의 색을 따른다.
|
|
58
|
+
*/
|
|
59
|
+
type SongFieldProps = {
|
|
60
|
+
/** 숨은 입력의 이름 */
|
|
61
|
+
name: string;
|
|
62
|
+
defaultValue?: PickedSong | null;
|
|
63
|
+
/** 고르거나 뺄 때마다 */
|
|
64
|
+
onChange?: (song: PickedSong | null) => void;
|
|
65
|
+
placeholder?: string;
|
|
66
|
+
/** Music plug 주소 (기본 https://musicplug.madeinbruno.com) */
|
|
67
|
+
base?: string;
|
|
68
|
+
className?: string;
|
|
69
|
+
};
|
|
70
|
+
declare function SongField({ name, defaultValue, onChange, placeholder, base, className }: SongFieldProps): react.JSX.Element;
|
|
71
|
+
|
|
24
72
|
/**
|
|
25
73
|
* 글 속 노래 한 곡의 '음악 듣기'.
|
|
26
74
|
*
|
|
@@ -32,8 +80,11 @@ type Song = {
|
|
|
32
80
|
* - 고르면 그곳에서 이 곡을 연다 (Music plug가 곡을 찾아 보낸다. 폰에서는 앱으로). YouTube면 글 안에서 유튜브 플레이어가 열린다.
|
|
33
81
|
* - 고른 곳은 기억해서 다음부터는 누르면 바로 그곳에서 연다. 곡 옆에 옅게 '멜론 · 바꾸기'가 보이고, 바꾸기로 다시 고른다.
|
|
34
82
|
* 기억은 쿠키 mp_service(madeinbruno.com 아래에서는 Music plug와 같이)와 이 브라우저(localStorage)에 둔다.
|
|
35
|
-
* - 곡을 모르면(song이 null) 다른 곳에서 찾을 수 없어서 이 페이지에서 YouTube로만
|
|
36
|
-
* -
|
|
83
|
+
* - 곡을 모르면(song이 null) 다른 곳에서 찾을 수 없어서 이 페이지에서 YouTube로만 튼다 (유튜브가 아닌 곡 링크면 그 링크를 연다).
|
|
84
|
+
* - 테마 음악을 정한 곡 링크(source)가 있으면, 듣는 사람이 같은 곳을 골랐을 때 찾지 않고 그 링크를 바로 연다.
|
|
85
|
+
* 곳마다 직접 정해 둔 곡 링크(links)가 있으면 그곳도 찾지 않고 그 링크를 연다 (곡을 못 찾는 곳을 위해).
|
|
86
|
+
* - 유튜브로 듣는 중에 읽어 내려가 플레이어가 화면 밖으로 나가면 왼쪽 아래에 작은 음악 바(▶/❚❚ + 곡 제목)가 뜬다
|
|
87
|
+
* (avoid로 준 요소가 보일 때는 숨긴다). 제목이 길면 왼쪽으로 흘러가며 뒷부분을 보여 준다.
|
|
37
88
|
*
|
|
38
89
|
* 스타일은 '@madeinbruno/music-plug/styles.css' (클래스는 모두 mp-). 곡 줄은 둘러싼 글의 색을 따르고, 듣기 플러그는 Music plug 색이다.
|
|
39
90
|
*/
|
|
@@ -47,13 +98,20 @@ type PlugPlayerProps = {
|
|
|
47
98
|
id: string;
|
|
48
99
|
start?: number;
|
|
49
100
|
};
|
|
50
|
-
/**
|
|
101
|
+
/** 이 곡을 정한 곡 링크 (songFromUrl이 준 service·url). 듣는 사람이 같은 곳을 고르면 그 링크를 바로 연다 */
|
|
102
|
+
source?: {
|
|
103
|
+
service: ServiceId;
|
|
104
|
+
url: string;
|
|
105
|
+
};
|
|
106
|
+
/** 곳마다 직접 정해 둔 곡 링크. 듣는 사람이 그곳을 고르면 찾지 않고 이 링크를 연다 (source보다 먼저) */
|
|
107
|
+
links?: SongLinks | null;
|
|
108
|
+
/** 유튜브 플레이어가 화면 밖일 때 뜨는 음악 바를, 이 요소가 보이면 숨긴다 (예: '.mib-footer') */
|
|
51
109
|
avoid?: string;
|
|
52
110
|
/** Music plug 주소 (기본 https://musicplug.madeinbruno.com) */
|
|
53
111
|
base?: string;
|
|
54
112
|
className?: string;
|
|
55
113
|
};
|
|
56
|
-
declare function PlugPlayer({ song, fallbackLabel, youtube, avoid, base, className }: PlugPlayerProps): react.JSX.Element;
|
|
114
|
+
declare function PlugPlayer({ song, fallbackLabel, youtube, source, links, avoid, base, className }: PlugPlayerProps): react.JSX.Element;
|
|
57
115
|
/**
|
|
58
116
|
* Music plug 듣기 플러그: 어디서 들을지 고르는 창. Music plug의 /listen 화면과 같은 모양.
|
|
59
117
|
* 바깥을 누르거나 Esc로 닫는다.
|
|
@@ -66,4 +124,4 @@ declare function PlugSheet({ song, services, current, onChoose, onClose, }: {
|
|
|
66
124
|
onClose: () => void;
|
|
67
125
|
}): react.JSX.Element;
|
|
68
126
|
|
|
69
|
-
export { PlugPlayer, type PlugPlayerProps, PlugSheet };
|
|
127
|
+
export { PlugPlayer, type PlugPlayerProps, PlugSheet, SongField, type SongFieldProps };
|