aboutsummaryrefslogtreecommitdiff
path: root/web/ytlist/ytlist.js
blob: 9354b023454d3518ae610701ee8996b156e7b345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let db_file="youtube_stuffs.db"
let gen_thumb=true

function insert_item(item,imgurl){
    let imgtag="<br><br>"
    if(imgurl){
        imgtag=`<img src="${imgurl}"></img>`
    }
    the_box.innerHTML+=`<div>
        <a href="https://youtube.com/watch?v=${item[2]}" target="_BLANK">
        <span class="video-title">${item[0]}: ${item[1]}</span>
        <span class="video-channelname">${item[3]}</span>
        <br>
        ${imgtag}
        </a>
        </div>`
}

function get_item(item){
    if(gen_thumb==true){
        fetch(`/ytlist/thumbnails/${item[2]}.webp`,
            { method: "HEAD" }
        ).then((res) => {
            if (res.ok) {
                insert_item(item,res.url);
            } else {
                fetch(`/ytlist/thumbnails/${item[2]}.jpg`,
                    { method: "HEAD" }
                ).then((res) => {
                    if (res.ok) {
                        insert_item(item,res.url);
                    } else {
                        insert_item(item,null);
                    }
                });
            }
        });
    } else {
        insert_item(item,null)
    }
}

async function search(){
    const sqlPromise = initSqlJs({
        locateFile: file => `/ytlist/${file}`
    });
    const dataPromise = fetch("/ytlist/"+db_file).then(res => res.arrayBuffer());
    const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])
    const db = new SQL.Database(new Uint8Array(buf));
    //search_box.style.display="none";

    category=document.querySelector('input[name="category_choice"]:checked').value;
    let query="SELECT rowid,* FROM ytlist"
    if (category!="ALL"){
        query+=` WHERE category='${category}'`
        if (search_in.value != ""){
            query+=` AND title LIKE '%${search_in.value}%'`
        }
    } else if (search_in.value != ""){
        query+=` WHERE title LIKE '%${search_in.value}%'`
    }

    let res = db.exec(query);

    the_box.innerHTML="";
    gen_thumb=gen_thumb_check.checked
    res[0].values.forEach(item=>{get_item(item)});
}

let first=false
async function init(){
    const sqlPromise = initSqlJs({
        locateFile: file => `/ytlist/${file}`
    });
    const dataPromise = fetch("/ytlist/"+db_file).then(res => res.arrayBuffer());
    const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])
    const db = new SQL.Database(new Uint8Array(buf));

    const res = db.exec("SELECT category FROM ytlist");

    let categories=[]
    category_box.innerHTML=`<input type="radio" name="category_choice" value="ALL" checked> ALL<br>`
    res[0].values.forEach(item=>{
        if (categories.indexOf(item[0]) < 0) {
            categories.push(item[0]);

            category_box.innerHTML+=`<input type="radio" name="category_choice" value="${item[0]}"> ${item[0]}<br>`
        }
    })

    if(first==false){
        db_box.innerHTML=`<input type="radio" name="db_choice" value="youtube_stuffs.db" checked> youtube_stuffs.db<br>`
        fetch(`/ytlist/youtube_stuffs_2.db`,
            { method: "HEAD" }
        ).then((res) => {
            if (res.ok) {
                db_box.innerHTML+=`<input type="radio" name="db_choice" value="youtube_stuffs_2.db"> youtube_stuffs_2.db<br>`
            }
        });
    }
    first=true
}
init()

document.body.addEventListener('change', function(event){
    if(event.target.name=="db_choice"){
        db_file=event.target.value
        init()
    }
})