This commit is contained in:
Halhadus 2024-10-05 08:48:49 +00:00
commit ffbe866bc6
3 changed files with 132 additions and 0 deletions

18
checkduplicate.py Normal file
View file

@ -0,0 +1,18 @@
duplicate = []
with open("musiclist.txt", "r") as file:
lines = file.readlines()
for line in lines:
url = line.split("---")[1]
a = 0
for line2 in lines:
url2 = line2.split("---")[1]
if url == url2:
a += 1
if a >= 2:
if not line in duplicate:
duplicate.append(line)
else:
continue
for duplines in duplicate:
print(duplines)

75
main.py Normal file
View file

@ -0,0 +1,75 @@
import yt_dlp
import os
musiclist = []
with open('musiclist.txt', 'r') as f:
for line in f:
if line.strip() == '':
continue
musicname, videoid, provider = line.strip().split('---')
musiclist.append({"musicname": musicname.strip(), "videoid": videoid.strip(), "provider": provider.strip()})
if os.path.exists('/mnt/sdcard/Music'):
download_location = '/mnt/sdcard/Music'
elif os.path.exists('/storage/emulated/0/Music'):
download_location = '/storage/emulated/0/Music'
elif os.path.exists('/sdcard/Music'):
download_location = '/sdcard/Music'
elif os.environ.get('HOME') is not None:
if os.path.exists(f'{os.environ["HOME"]}/Music'):
download_location = f'{os.environ["HOME"]}/Music'
else:
os.mkdir(f'{os.environ["HOME"]}/Music')
download_location = f'{os.environ["HOME"]}/Music'
else:
download_location = os.getcwd() + '/music'
print(f'Downloading music to {download_location}')
for music in musiclist:
if music["provider"] == 'YT':
youtube_link = f'https://www.youtube.com/watch?v={music["videoid"]}'
elif music["provider"] == 'YTM':
youtube_link = f'https://music.youtube.com/watch?v={music["videoid"]}'
music_name = music["musicname"]
# find anothor better way to remove not allowed characters and remove this stupid part
music_name = music_name.replace('/', '-')
music_name = music_name.replace('@', '')
music_name = music_name.replace('#', '')
music_name = music_name.replace('%', '')
music_name = music_name.replace('$', '')
music_name = music_name.replace('^', '')
music_name = music_name.replace('?', '')
music_name = music_name.replace('"', '')
music_name = music_name.replace("'", '')
music_name = music_name.replace(':', '')
music_name = music_name.replace('|', '')
music_name = music_name.replace('>', '')
music_name = music_name.replace('<', '')
music_name = music_name.replace('\\', '')
music_name = music_name.replace('&', '')
music_name = music_name.replace('+', '')
music_name = music_name.replace('=', '')
music_name = music_name.replace('~', '')
music_name = music_name.replace('`', '')
music_name = music_name.replace(';', '')
music_name = music_name.replace('*', '')
if os.path.exists(f'{download_location}/{music_name}.opus'):
print(f'{music_name} already exists')
continue
print(f'Downloading {music_name} from {youtube_link}')
yt_opts = {
'format': 'bestaudio/best',
'outtmpl': f'{music_name}.%(ext)s',
'postprocessors': [
{'key': 'FFmpegExtractAudio', 'preferredcodec': 'opus'},
{'key': 'FFmpegMetadata', 'add_metadata': True},
{'key': 'EmbedThumbnail'}
],
'writethumbnail': True,
'quiet': True
}
yt = yt_dlp.YoutubeDL(yt_opts)
yt.download([youtube_link])
print(f'{music_name} downloaded')
os.system(f'mv "{music_name}.opus" "{download_location}/{music_name}.opus"')

39
playlisttotxt.py Normal file
View file

@ -0,0 +1,39 @@
import csv
import os
import sys
if len(sys.argv) != 2:
print('Usage: python3 playlisttotxt.py <playlist.csv>')
sys.exit(1)
else:
csvname = sys.argv[1]
with open(csvname, 'r') as file:
reader = csv.reader(file)
next(reader)
playlist = []
for row in reader:
artist_name = row[3]
track_name = row[1]
playlist.append(f'{artist_name} - {track_name}')
formatted_lines = []
if os.path.exists('musiclist.txt'):
with open('musiclist.txt', 'r') as file:
lines = file.readlines()
for line in lines:
formatted_lines.append(line.split(' --- ')[0])
with open('musiclist.txt', 'a') as file:
for track in playlist:
if track in formatted_lines:
continue
else:
url = input(f'URL for {track}: ')
urlid = url.split('v=')[1]
if url.startswith('https://youtube.com/watch?v=') or url.startswith('https://www.youtube.com/watch?v='):
file.write(f'{track} --- {urlid} --- YT\n')
elif url.startswith('https://music.youtube.com/watch?v='):
file.write(f'{track} --- {urlid} --- YTM\n')
else:
print('Invalid URL')