Guest User

Untitled

a guest
Mar 21st, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import os
  2. from pymongo import MongoClient
  3. import requests
  4. from datetime import datetime, timezone, timedelta
  5.  
  6. # Define the folders to monitor and their corresponding section IDs
  7. folders = {
  8.     "/mnt/content/Movies": 1,
  9.     "/mnt/content/Webseries": 2,
  10.     "/mnt/content/Anime": 3
  11. }
  12.  
  13. # MongoDB data directory
  14. mongo_data_dir = "/root/.nginx/mongodb_data"
  15.  
  16. # Plex server URL and token
  17. plex_url = "http://127.0.0.1:32400"
  18. plex_token = "xxxxxxxxxxxxxxx"
  19.  
  20. # Log file path
  21. log_file = "partial.txt"
  22.  
  23. # Function to connect to MongoDB
  24. def connect_to_mongodb():
  25.     client = MongoClient('mongodb://localhost:27017/', serverSelectionTimeoutMS=2000)
  26.     return client
  27.  
  28. # Function to send a refresh request to Plex
  29. def send_refresh_request(section_id, path):
  30.     url = f"{plex_url}/library/sections/{section_id}/refresh"
  31.     headers = {
  32.         "X-Plex-Token": plex_token
  33.     }
  34.     data = {
  35.         "path": path
  36.     }
  37.     response = requests.get(url, headers=headers, params=data)
  38.     return response.status_code
  39.  
  40. # Function to log messages to a file
  41. def log_message(message):
  42.     with open(log_file, "a") as file:
  43.         file.write(message + "\n")
  44.  
  45. # Function to check for new items (files or directories) in a folder
  46. def check_for_new_items(client, folder):
  47.     db_name = folder.replace('/', '_')
  48.     db = client[db_name]
  49.     collection = db['files']
  50.     new_items = []
  51.  
  52.     # Iterate through files and subdirectories in the specified directory
  53.     for root, dirs, files in os.walk(folder):
  54.         for item in files + dirs:
  55.             item_path = os.path.join(root, item)
  56.             # Check if the item is a file or directory
  57.             if os.path.isfile(item_path) or os.path.isdir(item_path):
  58.                 # Check if the item is already in the collection
  59.                 if not collection.find_one({"name": item}):
  60.                     new_items.append(item_path)
  61.                     # Insert the item name into the collection
  62.                     collection.insert_one({"name": item})
  63.     return new_items
  64.  
  65. # Main function
  66. def main():
  67.     # Connect to MongoDB
  68.     client = connect_to_mongodb()
  69.  
  70.     # Get current time in IST
  71.     ist = timezone(timedelta(hours=5, minutes=30))
  72.     current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
  73.  
  74.     # Flag to track if new items were detected
  75.     new_items_detected = False
  76.  
  77.     # Loop through each folder and check for new items
  78.     for folder, section_id in folders.items():
  79.         new_items = check_for_new_items(client, folder)
  80.         if new_items:
  81.             new_items_detected = True
  82.             log_message(f"New items detected at {current_time} in folder: {folder}")
  83.             # Send refresh request to Plex
  84.             for item in new_items:
  85.                 status_code = send_refresh_request(section_id, item)
  86.                 if status_code == 200:
  87.                     log_message(f"Curl request = 200 for {item}")
  88.                 else:
  89.                     log_message(f"Failed to send curl request for {item}")
  90.  
  91.     # Log message if no new items were detected
  92.     if not new_items_detected:
  93.         log_message(f"No new items detected at {current_time}")
  94.  
  95.     # Close MongoDB connection
  96.     client.close()
  97.  
  98. if __name__ == "__main__":
  99.     main()
Advertisement
Add Comment
Please, Sign In to add comment