from BunnyCDN.Storage import Storage
import os, json

altpins_obj_storage = Storage('577cb82d-8176-4ccf-935ce0a574bf-fe4c-4012', 'altpins')
obj_storage = Storage('345697f9-d9aa-4a6b-a5ec8bffc16d-ceaf-453e', 'storysave')

medias = json.load(open('db_pins.json', 'r'))

count = 0
print(f"Found {len(medias)} files to process.")

cacheDir = 'old_altpins_cache'
for media in medias:
    count += 1
    username = media['title']
    mediaID = media['photo_id']
    mediaURL = media['url']
    extension = mediaURL.split('.')[-1]
    
    serverPath = mediaURL.replace("https://altpins.b-cdn.net/", '').replace('//', '/').replace('\\', '/').replace('https://altpins.b-cdn.net/', '')
    localFilePath = os.path.join(cacheDir, os.path.basename(serverPath))
    
    if os.path.exists(localFilePath):
        continue

    altpins_obj_storage.DownloadFile(storage_path=serverPath, download_path=cacheDir)
    print(f"Downloaded {count}/{len(medias)}: {localFilePath}")