# yt meta data crap
from googleapiclient.discovery import build
DEVELOPER_KEY = "AIzaSyBZG9siUQ0tTX-4DkqRJOHzAEW-3D2zdp8"
import music_tag
def get_video_details(video_id):
        youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
        request = youtube.videos().list(part='snippet,statistics', id=video_id)
        details = request.execute()
        title = details['items'][0]['snippet']['title']
        auther=details["items"][0]['snippet']["channelTitle"]
        year=details["items"][0]['snippet']["publishedAt"]
        if auther.find("- Topic"):auther=auther.replace(" - Topic","")
        return [title,auther,year]
def setmetadata(file,name,url):
 f = music_tag.load_file(file)
 try:
  title,auther,year=get_video_details(url)
 except:
  print("error bad yt")
  title=name
  year=2026
  auther=""
 #purge junk
 if auther!="":
  temp=re.sub("(( *|)(-*|\_*|x|\|)( *|) |)"+auther+"( ( *|)(-*|\_*|x|\|)( *|)|)","",title)
  if temp!=title:title=temp
  temp=re.sub("(\[|\(|)(original |official |original song|official song|)(remix|lyric video|music video|animated music vidio)(\]|\)|)","",title,flags=re.IGNORECASE)
  if temp!=title:title=temp
  temp=re.sub("(\[|\(|)(original |official |original song|official song|)(remix|lyric video|music video|animated music vidio)(\]|\)|)","",title,flags=re.IGNORECASE)
  if temp!=title:title=temp
  temp=re.sub("by "+auther+"$","",title,flags=re.IGNORECASE)
  if temp!=title:title=temp
 title.strip()
 f["artist"]=auther
 f["title"]=title
 f["tracktitle"]=title
 f["comment"]=url
 f.save()
import re
import sys
import os
path = sys.argv[1]
songs=[]
contents = os.listdir(path)
for x in contents:
 if x.find(" [")!=-1:
  y=x.split(" [")[0]
  z=""
  if x.find("]")!=-1:z=x.split(" [")[1].split("]")[0]
  if y in songs:
   print(x+"is a dupe")
   os.system('del "'+path+'\\'+x+'"')
  else:setmetadata(''+path+'\\'+x+'',y,z)
  print(y)
  songs.append(y)

