Advertisement
Najeebsk

YOUTUB-CHANNEL-SCRAP3.py

Mar 14th, 2024
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. import os
  2. import webbrowser
  3. from tkinter import Tk, Label, Button, Text, Entry, Scrollbar, messagebox
  4. from pytube import Playlist
  5.  
  6. def install_missing_modules():
  7.     try:
  8.         from pytube import Playlist
  9.     except ModuleNotFoundError:
  10.         os.system('pip install pytube')
  11.         messagebox.showinfo("Modules Installed", "Required modules have been installed. Please restart the application.")
  12.         exit()
  13.  
  14. def save_playlist_info(playlists):
  15.     with open('playlist_info.txt', 'w', encoding='utf-8') as f:
  16.         for playlist_url in playlists:
  17.             playlist = Playlist(playlist_url)
  18.             f.write(f"Playlist: {playlist.title}\n")
  19.             for video in playlist.videos:
  20.                 f.write(f"Title: {video.title}\n")
  21.                 f.write(f"URL: {video.watch_url}\n")
  22.                 f.write("\n")
  23.  
  24.     messagebox.showinfo("Success", "Playlist information successfully saved to playlist_info.txt")
  25.     display_playlist_info()
  26.  
  27. def display_playlist_info():
  28.     text.delete(1.0, "end")  # Clear previous content
  29.     with open('playlist_info.txt', 'r', encoding='utf-8') as f:
  30.         text.insert("end", f.read())
  31.  
  32. def open_url(event):
  33.     url_index = text.index("current")
  34.     line_num = int(url_index.split('.')[0]) - 1
  35.     url_line = text.get(f"{line_num + 1}.0", f"{line_num + 1}.end")
  36.     if url_line.startswith("URL:"):
  37.         url = url_line.strip().split(": ")[1]
  38.         webbrowser.open(url)
  39.  
  40. def get_playlist_info():
  41.     playlists = entry.get().split(',')
  42.     save_playlist_info(playlists)
  43.  
  44. def on_closing():
  45.     if messagebox.askokcancel("Quit", "Do you want to quit?"):
  46.         root.destroy()
  47.  
  48. # GUI setup
  49. root = Tk()
  50. root.title("Najeeb Youtube Channel Url Scraping")
  51. root.protocol("WM_DELETE_WINDOW", on_closing)
  52. root.configure(bg='pink')
  53.  
  54. label = Label(root, text="Enter playlist URLs (comma separated):", bg='black', fg='white')
  55. label.pack()
  56.  
  57. entry = Entry(root, width=100)
  58. entry.pack()
  59.  
  60. button = Button(root, text="Save Playlist Info", command=get_playlist_info, bg='#4CAF50', fg='white')
  61. button.pack()
  62.  
  63. scrollbar = Scrollbar(root, orient="vertical")
  64. scrollbar.pack(side="right", fill="y")
  65.  
  66. text = Text(root, height=20, width=80, yscrollcommand=scrollbar.set)
  67. text.pack()
  68.  
  69. scrollbar.config(command=text.yview)
  70.  
  71. text.bind("<Double-1>", open_url)
  72.  
  73. root.mainloop()
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement