So, I found the solution, in 3 parts:
Plex API Dev – I’m not sure if this is an official resource, but it helped out a ton
Plex Debug Logs – I renamed a few files manually and then inspected the logs to see what it was doing, and this also helped out a ton.
ChatGPT – I’m not gonna lie and say I did it all myself. ChatGPT was instrumental in helping me fix some syntax and make other suggetions. I, for one, accept and value our robot overlords, and welcome them with open arms.
Okay, so here’s how you do the thing … (my instructions apply to tv shows, but its probably similar for movies too)
Step 1:
Get the metadata ID
- Plex WebUI > Library
- Hover over the series and right-click to “copy link”
- Paste the URL somewhere, and pull out the key:
http://10.0.0.1:32400/web/index.html#!/server/1234abcd5678efgh9012ijkl3456mnop7890qrst/details?key=%2Flibrary%2Fmetadata%2F39119&context=source%3Acontent.library~1~10
- The part you’re interested in is the
%2Flibrary%2Fmetadata%2F39119
- aka:
/library/metadata/39119
- The id (for me)
39119 is for “Johnny Harris”
Step 2:
Craft a query URL
- With that metadata id, prepare a URL string that we’ll use later
Step 3:
Write a script to get all the episodes
I realize that this script is slightly misleading as you’re using one ID just to fetch another, but as long as you understand that, I think you’ll be fine.
#!/usr/bin/env python3.11
import requests
import json
rating_key_url = "http://10.0.0.1:32400/library/metadata/39119/children"
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Plex-Token': '_fGHJt7-kLkk_T6hYTHL' #No, I'm not that dumb
}
rating_key_response = requests.get(url=rating_key_url, headers=headers)
rating_key_data = rating_key_response.json()
# Extract the ratingKey from the first item in the Metadata list
rating_key = rating_key_data["MediaContainer"]["Metadata"][0]["ratingKey"]
series_data_url = f"http://10.0.0.1:32400/library/metadata/{rating_key}/children"
series_data_response = requests.get(url=series_data_url, headers=headers)
series_data_json = json.dumps(series_data_response.json(), indent=2)
with open('data.json', 'w') as f:
f.write(series_data_json)
Okay, so now you should have a file called data.json and it should look something like this.
Step 4:
Do the rename
Now you could either just do this as a one off, like I did my first time, and as a test – or I’m sure some brain-nugget on here will figure out a way to loop through it all … and I’m sure I’ll figure that out eventually too. Just takes time.
#!/usr/bin/env python3.11
import requests
episode_update_url = 'http://10.0.0.1:32400/library/sections/6/all' # '6' is the value of my 'tv.docs' library
params = {
'type': '4', # I don't know what this is, or why its needed.
'id': '39124', # This is the ratingId of the episode (['MediaContainer']['Metadata'][0]['ratingKey'])
'includeExternalMedia': '1',
'title.value': 'Why the US Sells Weapons to 103 Countries', # I typed this manually, but it'll be automated in the future
'titleSort.value': 'Why the US Sells Weapons to 103 Countries'
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Plex-Token': '_fGHJt7-kLkk_T6hYTHL' #No, I'm not that dumb
}
episode_update_response = requests.put(url=episode_update_url, headers=headers, params=params)
print(episode_update_response.status_code) # Should get back '200'
And that’s the story of the time I ignored my actual job for an afternoon and just did a thing on Plex.