-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2m.py
More file actions
63 lines (53 loc) · 2.17 KB
/
p2m.py
File metadata and controls
63 lines (53 loc) · 2.17 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
#!/usr/bin/env python3
import argparse
import configparser
import pathlib
from plexapi.myplex import MyPlexAccount
def cli_parser():
global CLI
parser = argparse.ArgumentParser(description='Export Plex playlists to m3u files')
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
parser.add_argument('-l', '--list', action='store_true', help='List all Plex playlists and exit')
parser.add_argument('-o', '--output', type=pathlib.Path, default=pathlib.Path('.'),
help='Output directory for m3u files (default: current directory)')
parser.add_argument('playlist_name', nargs='?', default=None,
help='Name of Plex playlist to export, or omit to export all')
CLI = parser.parse_args()
return CLI
def connect():
config = configparser.ConfigParser()
config.read('plex_account.ini')
account = MyPlexAccount(config['PLEX']['login'], config['PLEX']['password'])
return account.resource(config['PLEX']['server']).connect()
def export_playlist(plex, playlist, output_dir):
filename = output_dir / f"{playlist.title}.m3u"
tracks = playlist.items()
if len(tracks) == 0:
print(f"Skipping '{playlist.title}' — empty playlist")
return
with open(filename, 'w', encoding='utf-8') as f:
for track in tracks:
path = track.media[0].parts[0].file
f.write(path + '\n')
print(f"Exported '{playlist.title}' — {len(tracks)} tracks → {filename}")
def main():
cli_parser()
plex = connect()
audio_playlists = plex.playlists(playlistType='audio')
if CLI.list:
print("Plex audio playlists:")
for pl in audio_playlists:
print(f" {pl.title} ({pl.leafCount} tracks)")
return
CLI.output.mkdir(parents=True, exist_ok=True)
if CLI.playlist_name:
match = [pl for pl in audio_playlists if pl.title == CLI.playlist_name]
if not match:
print(f"Playlist '{CLI.playlist_name}' not found in Plex")
return
export_playlist(plex, match[0], CLI.output)
else:
for pl in audio_playlists:
export_playlist(plex, pl, CLI.output)
if __name__ == '__main__':
main()