Spaces:
Sleeping
Sleeping
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 |