Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '
- ' Simple script to export all playlists from Plex and save them in the
- ' same folder that this script is run from.
- '
- ' Instructions for use:
- ' 1. Modify serverUrl to match that of your Plex server.
- ' 2. Set token to be your Plex API token.
- ' 3. Save this as "export_plex_playlists.vbs".
- ' 4. Double-click on the file, click on Okay and it'll run silently. You'll get a pop-up at the end.
- '
- ' Notes:
- ' - Runs on Windows only.
- ' - Existing m3u files in the same folder will be deleted first.
- ' - Playlists are exported in UTF-8 format.
- ' - Playlists are simple M3U format only. Extended M3U would require a lot more code!
- '
- ' Configuration starts here:
- '
- ' serverUrl = The address and port number of your Plex server. Only http:// is supported, so you need
- ' to have "Preferred" set for "Secure Connections" within Plex. Use localhost if you are
- ' running this script on the same machine that hosts your Plex server.
- Dim serverUrl : serverUrl = "http://localhost:32400"
- ' token = Your Plex API token, required to authenticate with Plex.
- ' See https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
- Dim token : token = "YOUR_PLEX_TOKEN"
- ' You don't need to change anything below here!
- ' ------------------------------------------------------------------------
- Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
- Dim answer
- answer = MsgBox("This script will export all playlists from Plex at " & serverUrl & vbCrLf & vbCrLf & "Are you ready to start?", vbYesNo + vbQuestion, "Export Playlists")
- If answer <> vbYes Then WScript.Quit
- DeleteExistingM3UFiles
- ExportPlaylists
- MsgBox "All playlists successfully exported from Plex.", vbInformation, "Export successful"
- Set fso = Nothing
- WScript.Quit 0
- Sub ExportPlaylists()
- Dim playlistsXml, playlistNodes, playlistNode, i
- Set playlistsXml = CallPlexApi("/playlists")
- If playlistsXml Is Nothing Then
- MsgBox "Unable to retrieve playlists from Plex." + vbCrLf + vbCrLf + "Have you configured your serverUrl and token correctly?", vbExclamation, "Export Failed"
- WScript.Quit 1
- End If
- Set playlistNodes = playlistsXml.getElementsByTagName("Playlist")
- For i = 0 To playlistNodes.Length - 1
- Set playlistNode = playlistNodes.Item(i)
- ExportPlaylist playlistNode.getAttribute("key"), playlistNode.getAttribute("title")
- Next
- End Sub
- Sub ExportPlaylist(key, title)
- Dim xml, items, item, i, mediaPath, utf8File
- Set xml = CallPlexApi(key)
- If xml Is Nothing Then
- MsgBox "Failed to get playlist items for: " & title, vbExclamation, "Export Error"
- WScript.Quit 1
- End If
- Set items = xml.getElementsByTagName("Part")
- Set utf8File = CreateObject("ADODB.Stream")
- utf8File.Type = 2 ' Text
- utf8File.Charset = "utf-8"
- utf8File.Open
- For i = 0 To items.Length - 1
- Set item = items.Item(i)
- mediaPath = item.getAttribute("file")
- If Not IsEmpty(mediaPath) Then
- utf8File.WriteText mediaPath & vbCrLf
- End If
- Next
- utf8File.SaveToFile SanitizeFilename(title) & ".m3u", 2 ' Overwrite
- utf8File.Close
- Set utf8File = Nothing
- End Sub
- Function CallPlexApi(endpoint)
- Dim http, fullUrl
- Set http = CreateObject("MSXML2.XMLHTTP")
- fullUrl = serverUrl & endpoint & "?X-Plex-Token=" & token
- On Error Resume Next
- http.Open "GET", fullUrl, False
- http.Send
- If http.Status = 200 Then
- Set CallPlexApi = http.responseXML
- Else
- Set CallPlexApi = Nothing
- End If
- On Error GoTo 0
- End Function
- Sub DeleteExistingM3UFiles()
- Dim folder, files, file
- Set folder = fso.GetFolder(fso.GetAbsolutePathName("."))
- Set files = folder.Files
- For Each file In files
- If LCase(fso.GetExtensionName(file.Name)) = "m3u" Then
- file.Delete True
- End If
- Next
- End Sub
- Function SanitizeFilename(name)
- Dim invalidChars, c, i
- invalidChars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
- SanitizeFilename = name
- For i = 0 To UBound(invalidChars)
- c = invalidChars(i)
- SanitizeFilename = Replace(SanitizeFilename, c, "_")
- Next
- End Function
Advertisement
Add Comment
Please, Sign In to add comment