PYREF/pyref.py

104 lines
3.1 KiB
Python
Raw Normal View History

2025-02-04 16:25:13 +00:00
import json
import os
import subprocess
import requests
import time
from config_pyref import CONFIG
def get_mediainfo_json(mkv_file, mediainfo_path):
try:
command = [mediainfo_path, '--full', '--Output=JSON', mkv_file]
result = subprocess.run(command, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error executing MediaInfo for file {mkv_file}: {e}")
return None
except json.JSONDecodeError as e:
print(f"Error decoding JSON output from MediaInfo for file {mkv_file}: {e}")
return None
def save_json(data, output_path):
try:
with open(output_path, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
except IOError as e:
print(f"Error saving JSON to {output_path}: {e}")
def send_json_to_form(json_file_path, nzb_file_path, rlsname, api_key, api_url):
url = f"{api_url}?apikey={api_key.lower()}"
try:
files = {
'generated_nfo_json': open(json_file_path, 'rb'),
'nzb': open(nzb_file_path, 'rb') if nzb_file_path else None
}
data = {'rlsname': rlsname, 'upload': '1'}
response = requests.post(url, files=files, data=data)
return response.text
except requests.RequestException as e:
print(f"Error sending data to the API: {e}")
return None
except IOError as e:
print(f"Error opening files for upload: {e}")
return None
finally:
for file in files.values():
if file:
file.close()
def find_mkv_recursive(video_folder, rlsname):
for root, dirs, files in os.walk(video_folder):
if f"{rlsname}.mkv" in files:
return os.path.join(root, f"{rlsname}.mkv")
return None
def process_nzb_files(video_folder, nfo_folder, nzb_folder, api_key, mediainfo_path, api_url):
if not os.path.exists(nfo_folder):
try:
os.makedirs(nfo_folder)
except OSError as e:
print(f"Error creating directory {nfo_folder}: {e}")
return
for nzb_file in os.listdir(nzb_folder):
if nzb_file.endswith('.nzb'):
nzb_file_path = os.path.join(nzb_folder, nzb_file)
rlsname = os.path.splitext(nzb_file)[0]
corresponding_mkv = find_mkv_recursive(video_folder, rlsname)
if corresponding_mkv:
print(f'Processing NZB: {nzb_file_path} with corresponding MKV: {corresponding_mkv}')
metadata = get_mediainfo_json(corresponding_mkv, mediainfo_path)
if metadata is None:
continue
output_file_name = f"{rlsname}.json"
output_path = os.path.join(nfo_folder, output_file_name)
save_json(metadata, output_path)
print(f'Saved metadata to: {output_path}')
response = send_json_to_form(output_path, nzb_file_path, rlsname, api_key, api_url)
if response:
print(f'Response from the site: {response}')
else:
print(f"Failed to process NZB: {nzb_file_path}")
else:
print(f'No corresponding MKV file found for NZB: {nzb_file_path}')
time.sleep(10)
if __name__ == '__main__':
try:
process_nzb_files(
CONFIG['video_folder'],
CONFIG['nfo_folder'],
CONFIG['nzb_folder'],
CONFIG['api_key'],
CONFIG['mediainfo_path'],
CONFIG['api_url']
)
except Exception as e:
print(f"An unexpected error occurred: {e}")