scripts/source/find_tweet_user/find_tweet_user.py
2024-12-06 13:37:00 -06:00

72 lines
2.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import shutil
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
# Path to your Firefox profile
firefox_profile_path = "/home/jawz/.mozilla/firefox/yw8fhvh4.default-release"
# Initialize the Firefox driver with the existing profile
options = webdriver.FirefoxOptions()
options.add_argument("-profile")
options.add_argument(firefox_profile_path)
service = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(service=service, options=options)
# Directory containing the image files
source_dir = Path("/srv/server/jawz/Pictures/To Organize")
target_dir = Path("/srv/server/pool/scrapping/JawZ/gallery-dl")
delete_dir = Path("/srv/server/jawz/Pictures/To Organize/to_delete/")
# Function to get Twitter username from tweet status ID
def get_username_from_tweet_id(tweet_id):
url = f"https://x.com/anyuser/status/{tweet_id}"
driver.get(url)
time.sleep(2.5) # Wait for the page to load
fixed_url = driver.current_url
username = fixed_url.replace("https://x.com/", "").replace(
f"/status/{tweet_id}", ""
)
return username
# Extract tweet IDs, ensure they are unique and sorted
file_ids = map(lambda x: x.name.split("_")[0], source_dir.iterdir())
tweet_ids = sorted(set(file_ids))[:49]
# Iterate through unique and sorted tweet IDs
for tweet_id in tweet_ids:
username = get_username_from_tweet_id(tweet_id)
if username is None:
continue
if username == "anyuser":
continue
dest_dir = Path(target_dir, username)
if not dest_dir.exists():
continue
if not dest_dir.is_dir():
continue
files_to_move = filter(lambda x: tweet_id in x.name, source_dir.iterdir())
for file in files_to_move:
target_file = Path(dest_dir, file.name)
if target_file.exists():
delete_file = Path(delete_dir, file.name)
shutil.move(str(file.resolve()), str(delete_file.resolve()))
print(f"Moved {file.name} to {str(delete_file.resolve())}")
continue
# If file does not exist in target_dir, move it from source_dir to target_dir
shutil.move(str(file.resolve()), str(target_file.resolve()))
print(f"Moved {file.name} to {str(target_file.resolve())}")
# Close the driver
driver.quit()