Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from pymongo import MongoClient
- import requests
- from datetime import datetime, timezone, timedelta
- # Define the folders to monitor and their corresponding section IDs
- folders = {
- "/mnt/content/Movies": 1,
- "/mnt/content/Webseries": 2,
- "/mnt/content/Anime": 3
- }
- # MongoDB data directory
- mongo_data_dir = "/root/.nginx/mongodb_data"
- # Plex server URL and token
- plex_url = "http://127.0.0.1:32400"
- plex_token = "xxxxxxxxxxxxxxx"
- # Log file path
- log_file = "partial.txt"
- # Function to connect to MongoDB
- def connect_to_mongodb():
- client = MongoClient('mongodb://localhost:27017/', serverSelectionTimeoutMS=2000)
- return client
- # Function to send a refresh request to Plex
- def send_refresh_request(section_id, path):
- url = f"{plex_url}/library/sections/{section_id}/refresh"
- headers = {
- "X-Plex-Token": plex_token
- }
- data = {
- "path": path
- }
- response = requests.get(url, headers=headers, params=data)
- return response.status_code
- # Function to log messages to a file
- def log_message(message):
- with open(log_file, "a") as file:
- file.write(message + "\n")
- # Function to check for new items (files or directories) in a folder
- def check_for_new_items(client, folder):
- db_name = folder.replace('/', '_')
- db = client[db_name]
- collection = db['files']
- new_items = []
- # Iterate through files and subdirectories in the specified directory
- for root, dirs, files in os.walk(folder):
- for item in files + dirs:
- item_path = os.path.join(root, item)
- # Check if the item is a file or directory
- if os.path.isfile(item_path) or os.path.isdir(item_path):
- # Check if the item is already in the collection
- if not collection.find_one({"name": item}):
- new_items.append(item_path)
- # Insert the item name into the collection
- collection.insert_one({"name": item})
- return new_items
- # Main function
- def main():
- # Connect to MongoDB
- client = connect_to_mongodb()
- # Get current time in IST
- ist = timezone(timedelta(hours=5, minutes=30))
- current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
- # Flag to track if new items were detected
- new_items_detected = False
- # Loop through each folder and check for new items
- for folder, section_id in folders.items():
- new_items = check_for_new_items(client, folder)
- if new_items:
- new_items_detected = True
- log_message(f"New items detected at {current_time} in folder: {folder}")
- # Send refresh request to Plex
- for item in new_items:
- status_code = send_refresh_request(section_id, item)
- if status_code == 200:
- log_message(f"Curl request = 200 for {item}")
- else:
- log_message(f"Failed to send curl request for {item}")
- # Log message if no new items were detected
- if not new_items_detected:
- log_message(f"No new items detected at {current_time}")
- # Close MongoDB connection
- client.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment