-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplexTvExport.py
More file actions
97 lines (82 loc) · 4.21 KB
/
plexTvExport.py
File metadata and controls
97 lines (82 loc) · 4.21 KB
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
import csv
import re
from plexapi.server import PlexServer
import shlex
# Replace these with your Plex server information
PLEX_URL = 'http://192.168.1.1:32400'
PLEX_TOKEN = 'PLEX_TOKEN'
# Connect to the Plex server
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
# Get the 'TV Shows' library section
tv_library = plex.library.section('TV Shows')
# Define the CSV output file
csv_file = 'C:/Users/craig/Downloads/tv_library_info.csv'
# Define the headers for the CSV
fieldnames = [
'Show Title',
'Show Year',
'Show IMDb ID',
'Season Number',
'Episode Number',
'Episode Title',
'IMDb ID',
'File Path',
'Formatted File Path', # New field for formatted file path
'File Name',
'File Friendly Title'
]
# Write data to the CSV file
with open(csv_file, mode='w', newline='', encoding='utf-8') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
# Iterate through all shows in the library
for show in tv_library.all():
# Create a folder/file friendly show title
show_folder_friendly_title = re.sub(r'[^a-zA-Z0-9 ]', '', show.title)
for season in show.seasons():
for episode in season.episodes():
# Extracting TheMovieDB, IMDb, and TheTVDB IDs
imdb_id = next((guid.id.split('//')[-1] for guid in episode.guids if 'imdb' in guid.id), '')
thetvdb_id = next((guid.id.split('//')[-1] for guid in episode.guids if 'thetvdb' in guid.id), '')
# Create folder/file friendly title by removing special characters and replacing spaces with underscores
folder_friendly_title = re.sub(r'[^a-zA-Z0-9 ]', '', episode.title)
print(f"Exporting: {show.title} - s{season.index}e{episode.index} - {episode.title}")
# Handle multiple media parts
if episode.media:
for media in episode.media:
for part in media.parts:
file_path = part.file if part else 'N/A'
formatted_file_path = shlex.quote(file_path) if file_path != 'N/A' else 'N/A' # Format the file path for shell usage
file_name = file_path.split('/')[-1] if file_path != 'N/A' else 'N/A'
resolution = media.videoResolution if media else 'N/A'
imdb_id_for_row = next((guid.id.split('//')[-1] for guid in episode.guids if 'imdb' in guid.id), 'N/A')
# Write episode data to CSV
writer.writerow({
'Show Title': show.title,
'Show Year': show.year,
'Show IMDb ID': show.guid.split('//')[-1],
'Season Number': season.index,
'Episode Number': episode.index,
'Episode Title': episode.title,
'IMDb ID': imdb_id_for_row,
'File Path': file_path,
'Formatted File Path': formatted_file_path,
'File Name': file_name,
'File Friendly Title': folder_friendly_title
})
else:
# Write row even if it has no media files.
writer.writerow({
'Show Title': show.title,
'Show Year': show.year,
'Show IMDb ID': show.guid.split('//')[-1],
'Season Number': season.index,
'Episode Number': episode.index,
'Episode Title': episode.title,
'IMDb ID': 'N/A',
'File Path': 'N/A',
'Formatted File Path': 'N/A',
'File Name': 'N/A',
'File Friendly Title': folder_friendly_title
})
print("Finished retrieving TV show library information and saving to CSV.")