-
Notifications
You must be signed in to change notification settings - Fork 6
/
plex2letterboxd.py
63 lines (51 loc) · 2.07 KB
/
plex2letterboxd.py
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
"""Export watched Plex movies to the Letterboxd import format."""
import argparse
import configparser
import csv
import sys
from plexapi.server import PlexServer
def parse_args():
parser = argparse.ArgumentParser(
description='Export watched Plex movies to the Letterboxd import '
'format.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--ini', default='config.ini',
help='config file')
parser.add_argument('-o', '--output', default='letterboxd.csv',
help='file to output to')
parser.add_argument('-s', '--sections', default=['Movies'], nargs='+',
help='sections to grab from')
return parser.parse_args()
def parse_config(ini):
"""Read and validate config file."""
config = configparser.ConfigParser()
config.read(ini)
auth = config['auth']
missing = {'baseurl', 'token'} - set(auth.keys())
if missing:
print(f'Missing the following config values: {missing}')
sys.exit(1)
return auth
def write_csv(sections, output):
"""Generate Letterboxd import CSV."""
with open(output, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Title', 'Year', 'Rating10', 'WatchedDate'])
count = 0
for section in sections:
for movie in section.search(sort='lastViewedAt', unwatched=False):
date = None
if movie.lastViewedAt is not None:
date = movie.lastViewedAt.strftime('%Y-%m-%d')
rating = movie.userRating
if rating is not None:
rating = f'{movie.userRating:.0f}'
writer.writerow([movie.title, movie.year, rating, date])
count += 1
print(f'Exported {count} movies to {output}.')
def main():
args = parse_args()
auth = parse_config(args.ini)
plex = PlexServer(auth['baseurl'], auth['token'])
sections = [plex.library.section(s) for s in args.sections]
write_csv(sections, args.output)