File size: 2,060 Bytes
179f432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from tqdm import tqdm
from itertools import islice
from youtube_comment_downloader import *
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline

import matplotlib.pyplot as plt
import csv
import streamlit as st
import pandas as pd
import base64

st.title("Youtube Comment Downloader")

# Input URL video
video_url = st.text_input("Masukkan URL video YouTube:")

# Input jumlah komentar yang ingin diambil
num_comments = st.number_input("Jumlah komentar yang ingin diambil:", min_value=1, value=10)

if st.button("Ambil Data Komentar"):
    # Inisialisasi YoutubeCommentDownloader
    downloader = YoutubeCommentDownloader()

    # Mendapatkan komentar
    comments = downloader.get_comments_from_url(video_url, sort_by=SORT_BY_POPULAR)

    # Membuka file CSV untuk menulis
    with open('comments.csv', mode='w', encoding='utf-8', newline='') as file:
        # Membuat objek writer
        writer = csv.DictWriter(file, fieldnames=['cid', 'text', 'time', 'author', 'channel', 'votes', 'photo', 'heart', 'reply'])
        
        # Menulis header
        writer.writeheader()
        
        # Menulis data komentar
        for comment in tqdm(islice(comments, num_comments)):
            # Menghapus kolom 'time_parsed' dari komentar
            comment.pop('time_parsed', None)
            writer.writerow(comment)

    st.success(f"Komentar berhasil diunduh dan disimpan dalam file 'comments.csv'")
    
    # Membaca data dari file CSV
    comments_df = pd.read_csv('comments.csv')
    
    # Menampilkan tabel dengan menggunakan st.table()
    st.subheader("Data Komentar")
    st.table(comments_df)
    
   # Menyiapkan link untuk mendownload file CSV
    csv = comments_df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode() 
    href = f'<a style="background-color: #008CBA; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; font-weight: bold;" href="data:file/csv;base64,{b64}" download="comments.csv">Download File CSV</a>'
    st.markdown(href, unsafe_allow_html=True)