90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
import os
|
||
import subprocess
|
||
import re
|
||
import sys
|
||
import mariadb
|
||
|
||
# Configuration – update these values as needed.
|
||
LOCAL_DIR = "/shared/transcriptor/clips" # Local folder where clips are stored
|
||
REMOTE_NAME = "gdrive" # rclone remote name for Google Drive
|
||
|
||
# Connect to the MariaDB database
|
||
try:
|
||
conn = mariadb.connect(
|
||
host=os.environ.get("DB_HOST", "192.168.0.187"),
|
||
user=os.environ.get("DB_USER", "t0is"),
|
||
password=os.environ.get("DB_PASS", "Silenceisgolden555"),
|
||
database=os.environ.get("DB_NAME", "transcriptor"),
|
||
port=int(os.environ.get("DB_PORT", 3306))
|
||
)
|
||
cursor = conn.cursor()
|
||
except mariadb.Error as e:
|
||
print(f"Error connecting to MariaDB: {e}")
|
||
sys.exit(1)
|
||
|
||
def get_rclone_link(relative_path):
|
||
"""
|
||
Uses rclone to generate a shareable link for the file at the given relative path.
|
||
"""
|
||
remote_path = f"{REMOTE_NAME}:{relative_path}"
|
||
try:
|
||
result = subprocess.run(
|
||
["rclone", "link", remote_path],
|
||
stdout=subprocess.PIPE,
|
||
stderr=subprocess.PIPE,
|
||
text=True,
|
||
check=True
|
||
)
|
||
return result.stdout.strip()
|
||
except subprocess.CalledProcessError as e:
|
||
print(f"Error obtaining rclone link for {remote_path}: {e.stderr}")
|
||
return None
|
||
|
||
def extract_file_id(link):
|
||
"""
|
||
Extracts the Google Drive file ID from the shareable URL.
|
||
Expected URL format: https://drive.google.com/file/d/FILE_ID/view?usp=sharing
|
||
"""
|
||
match = re.search(r"/d/([^/]+)/", link)
|
||
if match:
|
||
return match.group(1)
|
||
return None
|
||
|
||
def update_database(filename, file_id):
|
||
"""
|
||
Updates the clips table in the database with the provided Google Drive file ID.
|
||
This example uses the base name of the file to match the record.
|
||
Adjust the query as needed for your schema.
|
||
"""
|
||
base = os.path.basename(filename)
|
||
query = "UPDATE clips SET gdrive_file_id = ? WHERE filename LIKE ?"
|
||
like_pattern = f"%{base}%"
|
||
try:
|
||
cursor.execute(query, (file_id, like_pattern))
|
||
conn.commit()
|
||
print(f"Updated {base} with file_id: {file_id}")
|
||
except mariadb.Error as e:
|
||
print(f"Database update failed for {base}: {e}")
|
||
|
||
def main():
|
||
# Walk through the local directory recursively
|
||
for root, dirs, files in os.walk(LOCAL_DIR):
|
||
for file in files:
|
||
full_path = os.path.join(root, file)
|
||
# Compute the relative path to preserve folder structure in the remote
|
||
rel_path = os.path.relpath(full_path, LOCAL_DIR)
|
||
print(f"Processing file: {full_path} (relative: {rel_path})")
|
||
link = get_rclone_link(rel_path)
|
||
if link:
|
||
file_id = extract_file_id(link)
|
||
if file_id:
|
||
update_database(rel_path, file_id)
|
||
else:
|
||
print(f"Could not extract file ID from link: {link}")
|
||
else:
|
||
print(f"No link generated for file: {full_path}")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
cursor.close()
|
||
conn.close() |