Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to V2 #5

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 54 additions & 34 deletions EmbyArrSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@
env_file = 'config/EmbyArrSync.env'
load_dotenv(dotenv_path=env_file)

HANDLE_TV = os.getenv('HANDLE_TV', 'False') == 'True'
HANDLE_MOVIES = os.getenv('HANDLE_MOVIES', 'False') == 'True'
TV_DELETE = os.getenv('TV_DELETE', 'False') == 'True'
MOVIE_DELETE = os.getenv('MOVIE_DELETE', 'False') == 'True'

SONARR_API_KEY = os.getenv('SONARR_API_KEY')
SONARR_URL = os.getenv('SONARR_URL')

RADARR_API_KEY = os.getenv('RADARR_API_KEY')
RADARR_URL = os.getenv('RADARR_URL')

EMBY_API_KEY = os.getenv('EMBY_API_KEY')
EMBY_URL = os.getenv('EMBY_URL')
EMBY_USER_ID = os.getenv('EMBY_USER_ID')
EMBY_DELETE = os.getenv('EMBY_DELETE', 'False') == 'True' # Convert string to boolean
LIMIT = int(os.getenv('LIMIT', '1000000000'))

TVDB_API_KEY = os.getenv('TVDB_API_KEY')
TVDB_PIN = os.getenv('TVDB_PIN')
TMDB_API_KEY = os.getenv('TMDB_API_KEY')

DAYS = int(os.getenv('DAYS', 14)) # Default to 14 if not defined
EMBY_DELETE = os.getenv('EMBY_DELETE', 'False') == 'True' # Convert string to boolean
LIMIT = int(os.getenv('LIMIT', '1000000000'))
# Load blacklists from JSON file
with open('config/blacklists.json', 'r') as file:
blacklists = json.load(file)

BLACKLISTED_TV_SHOWS = blacklists.get('BLACKLISTED_TV_SHOWS', [])
BLACKLISTED_MOVIES = blacklists.get('BLACKLISTED_MOVIES', [])
BLACKLISTED_PATHS = blacklists.get('BLACKLISTED_PATHS', [])

def get_watched_items(user_id):
url = f"{EMBY_URL}/Users/{user_id}/Items/Latest"
Expand Down Expand Up @@ -263,52 +273,62 @@ def delete_movie_file(radarr_id, movie_name):
print(f"Failed to delete movie ID {movie_name} from Radarr: {response.status_code}, {response.text}")

def main():
# Check and print the status of HANDLE_TV and HANDLE_MOVIES at the start
if not HANDLE_TV:
print("Handling of TV shows is disabled, Skipping to Movies.")
if not HANDLE_MOVIES:
print("Handling of movies is disabled, Ending script.")
watched_items = get_watched_items(EMBY_USER_ID)
if watched_items:
for item in watched_items:
if item['Type'] == 'Episode':
if item['Type'] == 'Episode' and HANDLE_TV:
series_name = item['SeriesName']
episode_name = item['Name']
season_number = item['ParentIndexNumber']
episode_number = item['IndexNumber']
item_info = f"{episode_name} from {series_name}"
if any(blacklisted_path in item['Path'] for blacklisted_path in BLACKLISTED_PATHS):
print(f"Skipping item in blacklisted path: {item['Name']}")
continue
if series_name in BLACKLISTED_TV_SHOWS:
print(f"Skipping blacklisted show: {series_name}")
continue # Skip this iteration if the show is blacklisted
elif series_name not in BLACKLISTED_TV_SHOWS:
# Fetch TVDB ID using series name
tvdb_id = get_tvdb_id(series_name)
if tvdb_id:
# Fetch Sonarr series ID using TVDB ID
series_id = get_series_id_by_tvdb(tvdb_id)
if series_id:
# Fetch episode info from Sonarr to get episode ID
episode_info = get_episode_info(series_id, season_number)
#print(episode_info)
for ep in episode_info:
if ep['seasonNumber'] == season_number and ep['episodeNumber'] == episode_number:
episode_id = ep['id']
episode_ids = [episode_id]
air_date_utc = datetime.strptime(ep['airDateUtc'], '%Y-%m-%dT%H:%M:%SZ')
do_not_delete = datetime.now() - timedelta(days=DAYS)
# Unmonitor this specific episode in Sonarr
unmonitor_episodes([episode_id])
print(f"Unmonitored episode: {item_info}")
if air_date_utc < do_not_delete:
#delete the episode from Emby (OPTIONAL), Sonarr and file system
delete_episode_file(series_id, season_number, episode_number, episode_name, series_name)
if EMBY_DELETE:
delete_item(item['Id'], item_info)
if not EMBY_DELETE:
print(f"Emby library update handled by Sonarr skipping Emby library delete for {series_name}: {episode_name} of Season{season_number}")
else:
print(f"{series_name}: {episode_name} aired within the past {DAYS} days. Not Deleted")
else:
print(f"TVDB ID not found for series '{series_name}'.")
tvdb_id = get_tvdb_id(series_name)
if tvdb_id:
# Fetch Sonarr series ID using TVDB ID
series_id = get_series_id_by_tvdb(tvdb_id)
if series_id:
# Fetch episode info from Sonarr to get episode ID
episode_info = get_episode_info(series_id, season_number)
#print(episode_info)
for ep in episode_info:
if ep['seasonNumber'] == season_number and ep['episodeNumber'] == episode_number:
episode_id = ep['id']
episode_ids = [episode_id]
air_date_utc = datetime.strptime(ep['airDateUtc'], '%Y-%m-%dT%H:%M:%SZ')
do_not_delete = datetime.now() - timedelta(days=DAYS)
# Unmonitor this specific episode in Sonarr
unmonitor_episodes([episode_id])
print(f"Unmonitored episode: {item_info}")
if air_date_utc < do_not_delete and TV_DELETE:
#delete the episode from Emby (OPTIONAL), Sonarr and file system
delete_episode_file(series_id, season_number, episode_number, episode_name, series_name)
if EMBY_DELETE:
delete_item(item['Id'], item_info)
if not EMBY_DELETE:
print(f"Emby library update handled by Sonarr skipping Emby library delete for {series_name}: {episode_name} of Season{season_number}")
else:
print(f"{series_name}: {episode_name} aired within the past {DAYS} days. Not Deleted")
else:
print(f"TVDB ID not found for series '{series_name}'.")

elif item['Type'] == 'Movie':
elif item['Type'] == 'Movie' and HANDLE_MOVIES:
movie_name = item['Name']
item_info = movie_name
if any(blacklisted_path in item['Path'] for blacklisted_path in BLACKLISTED_PATHS):
print(f"Skipping item in blacklisted path: {item['Name']}")
continue
if movie_name in BLACKLISTED_MOVIES:
print(f"Skipping blacklisted Movie: {movie_name}")
continue # Skip this iteration if the Movie is blacklisted
Expand All @@ -327,7 +347,7 @@ def main():
# Unmonitor movies in Radarr
unmonitor_movies(radarr_id, movie_name)
print(f"Unmonitored movie: {movie_name} with TMDB ID {tmdb_id}")
if earliest_release_date < Do_not_delete:
if earliest_release_date < Do_not_delete and MOVIE_DELETE:
#delete the episode from Emby(OPTIONAL), Radarr and file system
delete_movie_file(radarr_id, movie_name)
if EMBY_DELETE:
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ https://thetvdb.com/dashboard/account/apikey

https://www.themoviedb.org/settings/api

#### Get a Emby User ID

Login to Emby, go to Dashboard and then to users, select the user you want the script to run for and then in the url get the userId=XXXXXXXXXXXXXXXXXX and place the X's in the EMBY_USER_ID

#### Edit Env Variables

Open EmbyArrSync.env in the config Folder with your prefered text editor and Replace the placeholders with the correct variables
Expand All @@ -57,6 +61,13 @@ sudo nano /opt/EmbyArrSync/config/EmbyArrSync.env
```

```py
# TV and Movie Handling Logic
HANDLE_TV = True # Set to false to disable the script from touching TV shows
HANDLE_MOVIES = True # Set to false to disable the script from touching Movies shows
TV_DELETE = True # Set to false to disable the script from Deleting TV shows
MOVIE_DELETE = True # Set to false to disable the script from Deleting Movies shows


# Sonarr API details
SONARR_API_KEY = 'SONARR_API_KEY'
SONARR_URL = 'http://IP:PORT/api/v3'
Expand All @@ -68,8 +79,8 @@ RADARR_URL = 'http://IP:PORT/api/v3'
# Emby API details
EMBY_API_KEY = 'EMBY_API_KEY'
EMBY_URL = 'http://IP:PORT/emby'
EMBY_USER_ID = 'EMBY_USER_ID'
##Currently set to the highest number this is the maximum number of watched items to fetch from emby, change this if you only want to get the last X watched items
EMBY_USER_ID = 'EMBY_USER_ID' # This is the UID for a user not username get this from the user= section of the URL when you click on a user in the users tab of the dashboard
# Currently set to the highest number this is the maximum number of watched items to fetch from emby, change this if you only want to get the last X watched items
LIMIT = 1000000000
# boolean value (True/False) Set to true if you want the script to handle deleting from emby library,
# set to false if you are using the Sonarr/Radarr connect functions to handle emby library updates
Expand All @@ -82,7 +93,6 @@ TMDB_API_KEY = 'TMDB_API_KEY'

# If Aired in this time period dont delete
# Set the number of days to the time period from show air date that you want to be blacklisted from deleting
# Needs to be a whole number (do not use calculations)

DAYS = 14
```
Expand All @@ -109,6 +119,10 @@ sudo nano /opt/EmbyArrSync/config/blacklists.json
"Example TV Show 2",
"Example TV Show 3",
"Example TV Show 4"
],
"BLACKLISTED_PATHS": [
"/a/path/to/blacklist",
"/another/path/to/blacklist"
]
}

Expand Down
11 changes: 9 additions & 2 deletions config/EmbyArrSync.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# TV and Movie Handling Logic
HANDLE_TV = True # Set to false to disable the script from touching TV shows
HANDLE_MOVIES = True # Set to false to disable the script from touching Movies shows
TV_DELETE = True # Set to false to disable the script from Deleting TV shows
MOVIE_DELETE = True # Set to false to disable the script from Deleting Movies shows


# Sonarr API details
SONARR_API_KEY = 'SONARR_API_KEY'
SONARR_URL = 'http://IP:PORT/api/v3'
Expand All @@ -9,8 +16,8 @@ RADARR_URL = 'http://IP:PORT/api/v3'
# Emby API details
EMBY_API_KEY = 'EMBY_API_KEY'
EMBY_URL = 'http://IP:PORT/emby'
EMBY_USER_ID = 'EMBY_USER_ID'
##Currently set to the highest number this is the maximum number of watched items to fetch from emby, change this if you only want to get the last X watched items
EMBY_USER_ID = 'EMBY_USER_ID' # This is the UID for a user not username get this from the user= section of the URL when you click on a user in the users tab of the dashboard
# Currently set to the highest number this is the maximum number of watched items to fetch from emby, change this if you only want to get the last X watched items
LIMIT = 1000000000
# boolean value (True/False) Set to true if you want the script to handle deleting from emby library,
# set to false if you are using the Sonarr/Radarr connect functions to handle emby library updates
Expand Down
4 changes: 4 additions & 0 deletions config/blacklists.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"Example TV Show 2",
"Example TV Show 3",
"Example TV Show 4"
],
"BLACKLISTED_PATHS": [
"/a/path/to/blacklist",
"/another/path/to/blacklist"
]
}