Spaces:
Sleeping
Sleeping
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) |