5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

実は複雑なYouTubeチャンネルURLの種類とバリデーション

Last updated at Posted at 2021-09-26

YouTube関連のコンテンツをまとめるとき、YouTubeチャンネルURLのバリデーションをしたいことがあります。
しかし、意外と大変である割に、ググっても情報が出てこなかったので、独自に調べた情報をまとめておこうと思います。

URLの種類

YouTubeチャンネルには、4パターンのURLが存在します。

名前 概要
チャンネルURL(IDに基づくURL) 全てのチャンネルに割り当てられているチャンネルIDを含むURL。 youtube.com/channel/{{チャンネルID}}
以前のユーザ名のURL チャンネルを作成するときにユーザ名の指定が必要だったころの名残。 youtube.com/user/{{ユーザID}}
カスタムURL URLを覚えやすくしたり、短縮する目的で使用される。 ユーザがYouTubeに申請することで利用できるようになる。 youtube.com/c/{{カスタムチャンネルID}}
短縮バージョンのカスタムURL 以前のユーザ名のURL、もしくはカスタムURLが存在するときに自動的に割り振られる。ただし、両方存在した場合には、別のチャンネルであってもカスタムURLが優先される。 youtube.com/{{カスタムチャンネルID}}

補足

  • カスタムURLと以前のユーザ名のURLは独立しています。一つのチャンネルに対して両方が作成されている場合、片方だけが作成されている場合、両方作成されていない場合があります。
  • 作成されていないURLに対してアクセスした場合、404が返ります。
  • カスタムユーザURLとカスタムチャンネルURLのIDには同じIDを指定することもできます。
  • 現在、以前のユーザ名のURLを新規作成することはできません。
  • 既に存在する以前のユーザ名のURLに含まれるIDを、カスタムURLとして申請できます。その場合、短縮バージョンのカスタムURLのルーティングは、以前のユーザ名のURLからカスタムURLに上書きされます。

URL判別フローチャート

youtube.png

JavaScriptコードサンプル

有効なURLか判別

URLを受け取り初期化するクラス。チャンネルURLの種類は問わない。
(若干TypeScriptの記法が混ざっています)

export class YoutubeChannelUrl {
  private _url: string;

  // 全部じゃないけど、www.youtube.com/にある予約されたパス
  private _rootReservedWords = [
    'watch',
    'feed',
    'playlist',
    'user',
    'c',
    'channel',
    'paid_memberships'
  ];

  constructor (rawUrl: string) {
    const url = encodeURI(rawUrl)

    // チャンネルURL
    const patternChannel = /youtube.com\/channel\/UC[\w-]{22}/
    const patternChannelMatched = url.match(patternChannel)?.[0]
    // 以前のユーザ名のURL
    const patternUser = /youtube.com\/user\/[\w-%]+/
    const patternUserMatched = url.match(patternUser)?.[0]
    // カスタムURL
    const patternCustom = /youtube.com\/c\/[\w-%]+/
    const patternCustomMatched = url.match(patternCustom)?.[0]
    // 短縮バージョンのカスタムURL
    const patternShort = /youtube.com\/([\w-%]+)/
    const patternShortMatched = url.match(patternShort)

    if (patternChannelMatched) {
      this._url = patternChannelMatched
    } else if (patternUserMatched) {
      this._url = patternUserMatched
    } else if (patternCustomMatched) {
      this._url = patternCustomMatched
    } else if (patternShortMatched) {
      console.log(patternShortMatched)
      if (this._rootReservedWords.includes(patternShortMatched[1])) {
        throw new Error(
          `指定されたYouTubeチャンネルURLは不正なフォーマットです。(${url})`
        )
      }
      this._url = patternShortMatched[0]
    } else {
      throw new Error(
        `指定されたYouTubeチャンネルURLは不正なフォーマットです。(${url})`
      )
    }
  }

  toString (): string {
    return this._url
  }
}

const url1 = new YoutubeChannelUrl('https://www.youtube.com/channel/UC9ohoh_uz___-ulOac6kk8w')
const url2 = new YoutubeChannelUrl('https://www.youtube.com/user/hoge')
const url3 = new YoutubeChannelUrl('https://www.youtube.com/c/hoge')
const url4 = new YoutubeChannelUrl('https://www.youtube.com/hoge')

注意点

コードの_rootReservedWordsを見ればわかる通り、「短縮バージョンのカスタムURL」がチャンネルURLか判別するためにはYouTubeが使用しているページURLを把握する必要があるので、完全に漏れを許さないシステムでは「短縮バージョンのカスタムURL」をバリデーションするべきではないと思います。

また、私の調べた限りだと、「カスタムURL」から「チャンネルURL(IDに基づくURL)」をAPI経由で取得する方法は存在しません。(YouTube Channel APIにおいて、カスタムURLでチャンネルを特定することができません。)
「以前のユーザ名のURL」であれば、forUsernameパラメータを用いてチャンネルを特定することができます。

手動で「カスタムURL」から「チャンネルURL(IDに基づくURL)」を取得する方法としては、一旦ブラウザで「カスタムURL」にアクセスし、そのチャンネルの動画ページに移動し、そのページ内のチャンネルアイコンをクリックすることで、「チャンネルURL(IDに基づくURL)」を取得できます。(最初と同じページだが、URLが変わっている)

参考

5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?