Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Installations
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from dotenv import dotenv_values
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
+
from docx import Document
|
| 6 |
+
|
| 7 |
+
# Load environment variables from the .env file into a dictionary
|
| 8 |
+
environment_vars = dotenv_values('.env')
|
| 9 |
+
|
| 10 |
+
# Get the credential values from the '.env' file
|
| 11 |
+
my_api_key = environment_vars.get("API_KEY")
|
| 12 |
+
|
| 13 |
+
# Enter your API key here
|
| 14 |
+
api_key = my_api_key
|
| 15 |
+
|
| 16 |
+
# Create a YouTube API client
|
| 17 |
+
youtube = build('youtube', 'v3', developerKey=api_key)
|
| 18 |
+
|
| 19 |
+
def main():
|
| 20 |
+
st.title("YouTube Playlist Video Organizer")
|
| 21 |
+
|
| 22 |
+
# Get the playlist URL from the user
|
| 23 |
+
playlist_url = st.text_input("Enter the playlist URL:")
|
| 24 |
+
|
| 25 |
+
# Get repetitive terms from the user
|
| 26 |
+
repetitive_terms = st.text_input("Enter repetitive terms (comma-separated):")
|
| 27 |
+
repetitive_terms = [term.strip().upper() for term in repetitive_terms.split(',')]
|
| 28 |
+
|
| 29 |
+
if st.button("Organize Videos"):
|
| 30 |
+
organized_doc = organize_videos(playlist_url, repetitive_terms)
|
| 31 |
+
st.markdown(get_download_link(organized_doc), unsafe_allow_html=True)
|
| 32 |
+
display_organized_videos(organized_doc)
|
| 33 |
+
|
| 34 |
+
def organize_videos(playlist_url, repetitive_terms):
|
| 35 |
+
# Create a Word document
|
| 36 |
+
doc = Document()
|
| 37 |
+
|
| 38 |
+
# Get the playlist ID from the URL
|
| 39 |
+
playlist_id = playlist_url.split("list=")[1]
|
| 40 |
+
|
| 41 |
+
# Define the repetitive terms and headings
|
| 42 |
+
headings = {}
|
| 43 |
+
|
| 44 |
+
# Get all the video details from the playlist
|
| 45 |
+
videos = []
|
| 46 |
+
next_page_token = None
|
| 47 |
+
|
| 48 |
+
while True:
|
| 49 |
+
# Get the playlist items
|
| 50 |
+
playlist_items = youtube.playlistItems().list(
|
| 51 |
+
part="contentDetails",
|
| 52 |
+
playlistId=playlist_id,
|
| 53 |
+
maxResults=50,
|
| 54 |
+
pageToken=next_page_token
|
| 55 |
+
).execute()
|
| 56 |
+
|
| 57 |
+
# Get the video IDs
|
| 58 |
+
video_ids = [item['contentDetails']['videoId'] for item in playlist_items['items']]
|
| 59 |
+
|
| 60 |
+
# Get the video details
|
| 61 |
+
video_details = youtube.videos().list(
|
| 62 |
+
part="snippet",
|
| 63 |
+
id=','.join(video_ids)
|
| 64 |
+
).execute()
|
| 65 |
+
|
| 66 |
+
# Add the videos to the list
|
| 67 |
+
for item in video_details['items']:
|
| 68 |
+
title = item['snippet']['title']
|
| 69 |
+
video_id = item['id']
|
| 70 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 71 |
+
|
| 72 |
+
# Group videos based on the repetitive terms
|
| 73 |
+
found = False
|
| 74 |
+
for term in repetitive_terms:
|
| 75 |
+
if term in title.upper():
|
| 76 |
+
if term not in headings:
|
| 77 |
+
headings[term] = chr(len(headings) + 65)
|
| 78 |
+
videos.append({"title": title, "url": video_url, "group": headings[term]})
|
| 79 |
+
found = True
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
# Add videos without repetitive terms to Introduction or Conclusion
|
| 83 |
+
if not found:
|
| 84 |
+
if item == video_details['items'][0]:
|
| 85 |
+
videos.append({"title": title, "url": video_url, "group": "Introduction"})
|
| 86 |
+
elif item == video_details['items'][-1]:
|
| 87 |
+
videos.append({"title": title, "url": video_url, "group": "Conclusion"})
|
| 88 |
+
|
| 89 |
+
# Check if there are more pages
|
| 90 |
+
next_page_token = playlist_items.get('nextPageToken')
|
| 91 |
+
|
| 92 |
+
if not next_page_token:
|
| 93 |
+
break
|
| 94 |
+
|
| 95 |
+
# Print the video titles and URLs grouped by the headings
|
| 96 |
+
for group in sorted(headings.values()):
|
| 97 |
+
doc.add_paragraph(f"\n{group}. {list(headings.keys())[list(headings.values()).index(group)]}")
|
| 98 |
+
count = 1
|
| 99 |
+
for video in videos:
|
| 100 |
+
if video['group'] == group:
|
| 101 |
+
doc.add_paragraph(f"{group}.{count} {video['title']} - {video['url']}")
|
| 102 |
+
count += 1
|
| 103 |
+
|
| 104 |
+
# Print videos without repetitive terms under Introduction or Conclusion
|
| 105 |
+
for group in ["Introduction", "Conclusion"]:
|
| 106 |
+
doc.add_paragraph(f"\n{group}")
|
| 107 |
+
count = 1
|
| 108 |
+
for video in videos:
|
| 109 |
+
if video['group'] == group:
|
| 110 |
+
doc.add_paragraph(f"{group}.{count} {video['title']} - {video['url']}")
|
| 111 |
+
count += 1
|
| 112 |
+
|
| 113 |
+
return doc
|
| 114 |
+
|
| 115 |
+
def get_download_link(doc):
|
| 116 |
+
doc.save("organized_videos.docx")
|
| 117 |
+
doc_download_link = f'<a href="organized_videos.docx" download>Click here to download the organized videos document</a>'
|
| 118 |
+
return doc_download_link
|
| 119 |
+
|
| 120 |
+
def display_organized_videos(doc):
|
| 121 |
+
st.subheader("Organized Videos")
|
| 122 |
+
for paragraph in doc.paragraphs:
|
| 123 |
+
st.write(paragraph.text)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
main()
|