-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plexAutomation.py
131 lines (109 loc) · 4.87 KB
/
plexAutomation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# /usr/bin/python3
"""
Script Name : plexAutomation.py
Version : 0.1.0
Description : Python scripts to help automate the process of
encoding media files and transferring to your Plex Media Server.
Author : Tyler Wasick (github.com/tylerwasick)
Date : 09/01/2023
License : GNU General Public License v3.0
Site : https://github.com/tylerwasick/Plex-Automation
=======================================================================================
Version History :
09/01/2023 : Initial Release
"""
## Standard library imports
import configparser
import requests
import shutil
import sys
import os
from pid import PidFile
## Third-party library imports
## Custom library imports
import scripts.appSetup as appSetup
import scripts.encodeMedia as encodeMedia
## Variables ##
projectPath = BASE_DIR = os.path.dirname(os.path.abspath(__file__))
userProfile = os.environ["HOME"]
plexMedia = {"plexMount": "/Volumes/plex"}
plexHost = "vpn.tylerwasick.com"
s3Bucket = "s3://plexutil/"
s3ConfigFile = "~/.s3cfg"
s3Media = {
"movieSource" : s3Bucket + "Movies/",
"tvSource" : s3Bucket + "TV/",
"otherSource" : s3Bucket + "Other/",
"moviePlexDestination" : "/plex/Movies/",
"tvPlexDestination" : "/plex/TV/",
"otherPlexDestination" : "",
"movieEncodeDestination" : "/plextemp/Encoded/Movies/",
"tvEncodeDestination" : "/plextemp/Encoded/TV/",
"otherEncodeDestination" : "/plextemp/Encoded/Other/",
"archiveDestination" : s3Bucket + "Archived/",
"movieTmpDestination" : "/plextemp/Temp/Movies/",
"tvTmpDestination" : "/plextemp/Temp/TV/",
"otherTmpDestination" : "/plextemp/Temp/Other/"
}
movies = []
shows = []
others = []
encodingExt = (".mkv", ".mp4", ".m4v")
encodingRString = ".mp4.mkv.m4v"
encodedExt = ".m4v"
handBrakeCLIDir = projectPath + "/downloads/"
handBrakeFlatPakPath = handBrakeCLIDir + "HandBrakeCLI-1.6.1-x86_64.flatpak"
handBrakeFlatPakGit = "https://github.com/HandBrake/HandBrake/releases/download/1.6.1/HandBrake-1.6.1-x86_64.flatpak"
handbrakeSHA256 = "b96fe8b363be2398f62efc1061f08992f93f748540f30262557889008b806009"
handBrakeProfile = " --preset-import-gui settings/Plex-HD.json --crop-mode none"
regularExpPattern = r"^([\w\s]+)\s-\sS(\d+)E"
configFile = projectPath + "/settings/config.ini"
configFileGitURL = "https://raw.githubusercontent.com/tylerwasick/Plex-Automation/main/config.ini"
## Functions
def main():
# Check if the config file exists
if os.path.isfile(configFile):
print("Config file exists")
print(configFile)
else:
# If file does not exist, create the file with a template from Github
print("Config file does not exist, creating one")
request = requests.get(configFileGitURL, allow_redirects=True)
# Save file to the project directory
open(configFile, 'wb').write(request.content)
# Verify the file was created
if os.path.isfile(configFile):
print("Config file created successfully")
else:
print("Failed to create config file. Aborting!")
sys.exit()
# Load the config file and parse the values
config = configparser.ConfigParser()
# Prompt the user for the information and create the config file
# Run the app requirements setup
print("Setting up requirements")
setup = appSetup.appRequirements(s3Bucket, s3ConfigFile, handBrakeCLIDir)
if setup:
print("App requirement setup complete")
else:
print("App requirement setup failed")
sys.exit()
# Download Handbrake
download = appSetup.downloadHandbrake(handBrakeCLIDir, handBrakeFlatPakPath, handBrakeFlatPakGit)
# Verify Handbrake downloads successfully
if download:
# If successful, encode media
print("Encoding media")
# encodeMedia.encodeMedia(s3Media, movies, shows, others, plexHost, handBrakeProfile, regularExpPattern, encodingExt, encodingRString, encodedExt)
else:
# Else exit
print("Failed to download Handbrake. Aborting!")
sys.exit()
## Main entry point
if __name__ == "__main__":
# Check if the script is already running
with PidFile(piddir="."):
try:
main()
except Exception as e:
print(f"An error occurred: {e}")