my-music-list/checkstillexist.py

81 lines
3 KiB
Python

import yt_dlp
import os
import shutil
def validate_youtube_url(url):
if 'youtube.com/watch?v=' not in url and 'music.youtube.com/watch?v=' not in url:
return False, None, None
try:
vid = url.split('v=')[1].split('&')[0].split('#')[0]
if len(vid) != 11:
return False, None, None
except:
return False, None, None
provider = 'YTM' if 'music.youtube.com' in url else 'YT'
yt_opts = {'quiet': True, 'ignoreerrors': True}
with yt_dlp.YoutubeDL(yt_opts) as ydl:
info = ydl.extract_info(url, download=False)
return bool(info), vid, provider
def process_musiclist():
shutil.copyfile('musiclist.txt', 'musiclist_backup.txt')
temp_lines = []
with open('musiclist.txt', 'r') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if not line:
temp_lines.append(line)
continue
try:
musicname, old_vid, provider = line.split('---')
musicname = musicname.strip()
old_vid = old_vid.strip()
provider = provider.strip()
except:
print(f"⚠️ Invalid line format (line {line_num}): {line}")
temp_lines.append(line)
continue
base_url = f'https://{"music." if provider=="YTM" else ""}youtube.com/watch?v={old_vid}'
is_valid, _, _ = validate_youtube_url(base_url)
if is_valid:
temp_lines.append(line)
continue
print(f"\n🔴 Invalid: {musicname}")
print(f"Current URL: {base_url}")
while True:
new_url = input("Enter new YouTube URL (cancel with 'x'): ").strip()
if new_url.lower() == 'x':
temp_lines.append(line)
print("Cancelled, original entry preserved")
break
is_valid, vid, detected_provider = validate_youtube_url(new_url)
if is_valid:
new_line = f"{musicname} --- {vid} --- {detected_provider}"
temp_lines.append(new_line)
print(f"Updated: {new_line}")
break
print("❌ Invalid URL or video! Try again.")
print("\n" + ""*50)
print(f"Total {len(temp_lines)} entries processed")
confirmation = input("Save changes? (Y/N): ").strip().lower()
if confirmation == 'y':
with open('musiclist.txt', 'w') as f:
f.write('\n'.join(temp_lines))
print("✔️ Music list updated successfully!")
print("Backup of original file: musiclist_backup.txt")
else:
print("❌ Changes discarded, file unchanged")
if __name__ == "__main__":
print("A backup of your original file will be created automatically\n")
if not os.path.exists('musiclist.txt'):
print("Error: musiclist.txt not found!")
exit()
process_musiclist()