|
|
|
import streamlit as st |
|
from dotenv import dotenv_values |
|
from googleapiclient.discovery import build |
|
from docx import Document |
|
|
|
|
|
environment_vars = dotenv_values('.env') |
|
|
|
|
|
my_api_key = environment_vars.get("API_KEY") |
|
|
|
|
|
api_key = my_api_key |
|
|
|
|
|
youtube = build('youtube', 'v3', developerKey=api_key) |
|
|
|
def main(): |
|
st.title("YouTube Playlist Video Organizer") |
|
|
|
|
|
st.markdown(""" |
|
## YouTube Playlist Video Organizer |
|
|
|
Welcome to the YouTube Playlist Video Organizer app! |
|
This tool helps you organize videos in a playlist based on repetitive terms. |
|
|
|
To get started, enter the playlist URL and the repetitive terms in the respective input fields. |
|
Click the "Organize Videos" button to generate a document with organized videos. |
|
|
|
**How to Use:** |
|
- Enter the playlist URL. |
|
- Specify repetitive terms (comma-separated). |
|
- Click the "Organize Videos" button. |
|
- Download the organized videos document. |
|
|
|
Enjoy organizing your YouTube playlist effortlessly! |
|
""") |
|
|
|
|
|
playlist_url = st.text_input("Enter the playlist URL:") |
|
|
|
|
|
repetitive_terms = st.text_input("Enter repetitive terms (comma-separated):") |
|
repetitive_terms = [term.strip().upper() for term in repetitive_terms.split(',')] |
|
|
|
if st.button("Organize Videos"): |
|
organized_doc = organize_videos(playlist_url, repetitive_terms) |
|
st.markdown(get_download_link(organized_doc), unsafe_allow_html=True) |
|
display_organized_videos(organized_doc) |
|
|
|
def organize_videos(playlist_url, repetitive_terms): |
|
|
|
doc = Document() |
|
|
|
|
|
playlist_id = playlist_url.split("list=")[1] |
|
|
|
|
|
headings = {} |
|
|
|
|
|
videos = [] |
|
next_page_token = None |
|
|
|
while True: |
|
|
|
playlist_items = youtube.playlistItems().list( |
|
part="contentDetails", |
|
playlistId=playlist_id, |
|
maxResults=50, |
|
pageToken=next_page_token |
|
).execute() |
|
|
|
|
|
video_ids = [item['contentDetails']['videoId'] for item in playlist_items['items']] |
|
|
|
|
|
video_details = youtube.videos().list( |
|
part="snippet", |
|
id=','.join(video_ids) |
|
).execute() |
|
|
|
|
|
for item in video_details['items']: |
|
title = item['snippet']['title'] |
|
video_id = item['id'] |
|
video_url = f"https://www.youtube.com/watch?v={video_id}" |
|
|
|
|
|
found = False |
|
for term in repetitive_terms: |
|
if term in title.upper(): |
|
if term not in headings: |
|
headings[term] = chr(len(headings) + 65) |
|
videos.append({"title": title, "url": video_url, "group": headings[term]}) |
|
found = True |
|
break |
|
|
|
|
|
if not found: |
|
if item == video_details['items'][0]: |
|
videos.append({"title": title, "url": video_url, "group": "Introduction"}) |
|
elif item == video_details['items'][-1]: |
|
videos.append({"title": title, "url": video_url, "group": "Conclusion"}) |
|
|
|
|
|
next_page_token = playlist_items.get('nextPageToken') |
|
|
|
if not next_page_token: |
|
break |
|
|
|
|
|
for group in sorted(headings.values()): |
|
doc.add_paragraph(f"\n{group}. {list(headings.keys())[list(headings.values()).index(group)]}") |
|
count = 1 |
|
for video in videos: |
|
if video['group'] == group: |
|
doc.add_paragraph(f"{group}.{count} {video['title']} - {video['url']}") |
|
count += 1 |
|
|
|
|
|
for group in ["Introduction", "Conclusion"]: |
|
doc.add_paragraph(f"\n{group}") |
|
count = 1 |
|
for video in videos: |
|
if video['group'] == group: |
|
doc.add_paragraph(f"{group}.{count} {video['title']} - {video['url']}") |
|
count += 1 |
|
|
|
return doc |
|
|
|
def get_download_link(doc): |
|
doc.save("organized_videos.docx") |
|
doc_download_link = f'<a href="organized_videos.docx" download>Click here to download the organized videos document</a>' |
|
return doc_download_link |
|
|
|
def display_organized_videos(doc): |
|
st.subheader("Organized Videos") |
|
for paragraph in doc.paragraphs: |
|
st.write(paragraph.text) |
|
|
|
if __name__ == "__main__": |
|
main() |