File size: 2,687 Bytes
e5ab635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import vimeo
from tqdm import tqdm
import time
from dotenv import load_dotenv
import os
load_dotenv()

v = vimeo.VimeoClient(
    token=os.environ.get("VIMEO_TOKEN"),
    key=os.environ.get("VIMEO_KEY"),
    secret=os.environ.get("VIMEO_SECRET")
)

user_id = 'pedrombr'  # Replace with the actual user ID
endpoint = f'/users/{user_id}/videos'
# Make the GET request
response1 = v.get(f'{endpoint}?per_page=100').json()
response2 = v.get(f'{endpoint}?per_page=100&page=2').json()

response = response1
response['data'].extend(response2['data'])

def rename_video(video_id):
    try:
        # Get the current video details
        video_response = v.get(f"/videos/{video_id}")
        
        if video_response.status_code != 200:
            print(f"Failed to fetch video details: {video_response.json()}")
            return
        
        video_data = video_response.json()
        current_name = video_data.get("name", "")
        
        # Check if the name starts with 'GEM_'
        if current_name.startswith("GEM_"):
            new_name = current_name.replace("GEM_", f"{video_id}_", 1)  # Remove the 'GEM_' prefix
            # Update the video's name
            patch_data = {"name": new_name}
            response = v.patch(f"/videos/{video_id}", data=patch_data)
            
            if response.status_code == 200:
                print(f"Video renamed successfully to: {new_name}")
            else:
                print(f"Failed to rename video: {response.json()}")
        elif current_name.startswith("Vista_"):
            new_name = current_name.replace("Vista_", f"{video_id}_", 1)
            patch_data = {"name": new_name}
            response = v.patch(f"/videos/{video_id}", data=patch_data)
            if response.status_code == 200:
                print(f"Video renamed successfully to: {new_name}")
            else:
                print(f"Failed to rename video: {response.json()}")
        else:
            print("The video name does not start with 'GEM_' or 'Vista_'. No changes made.")
    except Exception as e:
        print(f"An error occurred: {e}")
        time.sleep(60)  # Wait for 60 seconds before retrying
        rename_video(video_id)  # Retry the operation

gem_videos = [
    ("_".join(v['name'].split("_")[1:]), v['uri'].split("/")[-1]) for v in response['data'] if v['name'].startswith('GEM')
]

vista_videos = [
    ("_".join(v['name'].split("_")[1:]), v['uri'].split("/")[-1]) for v in response['data'] if v['name'].startswith('Vista')
]

# assert len(gem_videos) == len(vista_videos) and len(gem_videos) == 99

for video in gem_videos + vista_videos:
    rename_video(video[1])  # Pass the video ID to the rename function