import json,jsonpath,tekore,os,eyed3,time
from core.config import *
from core.lib import *
from pytube import YouTube
from eyed3.id3.frames import ImageFrame

sp_url="https://open.spotify.com/track/4FYbo4yQzu3hGkj2Ix4ucW?si=7411017cb4b44681"

track_info=get_spotifyTrack_info(sp_url=sp_url)

track_name=track_info['name']
track_artist=track_info['artist']
album_name=track_info['album_name']

title=f'{track_name}-{track_artist}'

art_url=get_spotifyTrack_album_art(sp_url=sp_url)

yt_url=get_yt_url(videotitle=title)

tempdirname = f"{title}"

os.chdir('./temp')
os.mkdir(f'{title}')

art = open(f'./{title}/{title}.png','wb')
art.write(urllib.request.urlopen(art_url).read())
art.close()        

YouTube(yt_url).streams.filter(only_audio=True).first().download(output_path=f'./{title}',filename=f'{title}.mp3')



audio=eyed3.load(f'./{title}/{title}.mp3')
audio.tag.title=f'{track_name}'
audio.tag.album=f'{album_name}'
audio.tag.artist=f'{track_artist}'
audio.tag.images.set(3, open(f"{title}.png", 'rb').read(), 'image/png')
audio.tag.save(version=eyed3.id3.ID3_V2_3)



os.chdir(TopPath)
