import os


# file name : masstik_caammmyyy_1310_655_going blonde wednesdayyyy.mp4
# file name : masstiktok_aleksandraverse__#fyp #trending #viral #foryou.mp4
# where the first item is prefix, second is username and after those is the tiktok title

processed_dir = 'processed_tiktoks'
os.makedirs(processed_dir, exist_ok=True)

users = os.listdir('tiktoks')

for user in users:
    files = os.path.join('tiktoks', user)
    for file in os.listdir(files):
        if 'masstik' not in file and 'masstiktok' not in file:
            print(f"Skipping {file}")
            continue
        
        filepath = os.path.join(files, file)
        file_ext = os.path.splitext(file)[1]
        data = file.split('_')
        prefix = data[0]
        username = data[1]
        username = username.replace('@', '')
        title = ' '.join(data[2:])
        title = os.path.splitext(title)[0]
        
        print("="*100)
        title = title.encode('utf-8', 'ignore').decode('utf-8')
        print(f"Prefix: {prefix}\nUsername: {username}\nTitle: {title}")
        print("="*100)
        
        new_filename = f"{username}~{title}.{file_ext}"
        new_filepath = os.path.join(processed_dir, new_filename)
        
        os.rename(filepath, new_filepath)
        print(f"Renamed {file} to {new_filename}")