Project

General

Profile

Download (1.57 KB) Statistics
| Branch: | Revision:

hydrilla-fixes-bundle / src / yewtube_urls.js @ bac96457

1
/**
2
 * SPDX-License-Identifier: CC0-1.0
3
 *
4
 * Library to convert youtube.com URLs to yewtu.be URLs.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the CC0 1.0 Universal License as published by
10
 * the Creative Commons Corporation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * CC0 1.0 Universal License for more details.
16
 */
17

    
18
function decode_query_options(url)
19
{
20
    const query_options = {};
21
    const match = /^[^?]*\?(.*)/.exec(url);
22
    if (!match)
23
	return query_options;
24

    
25
    for (const opt of match[1].split("&")) {
26
	const [key, val] =
27
	      /^([^=]*)=?(.*)/.exec(opt).splice(1, 2).map(decodeURIComponent);
28
	query_options[key] = val;
29
    }
30

    
31
    return query_options;
32
}
33

    
34
function encode_query_options(query_options)
35
{
36
    return Object.entries(query_options)
37
	.map(ar => ar.map(encodeURIComponent).join("=")).join("&");
38
}
39

    
40
function make_yewtube_url(youtube_url)
41
{
42
    const query_options = decode_query_options(youtube_url);
43

    
44
    let endpoint = "";
45

    
46
    const match = /^(?:(?:https?:)?\/\/)?[^/]*\/([^?]*)/.exec(youtube_url);
47
    if (match)
48
	endpoint = match[1];
49

    
50
    if (/^embed\//.test(query_options.v))
51
	endpoint = query_options.v;
52

    
53
    if (/^embed\/.+/.test(endpoint))
54
	delete query_options.v;
55

    
56
    const encoded_options = encode_query_options(query_options);
57
    return `https://yewtu.be/${endpoint}?${encoded_options}`;
58
}
(21-21/22)