Skip to content

Commit

Permalink
Merge pull request #160 from booxter/dirs
Browse files Browse the repository at this point in the history
get-directors: append if output file is present
  • Loading branch information
booxter authored Dec 5, 2024
2 parents 3ab8167 + 598f289 commit decec5d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/letsrolld/cmd/get_directors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import argparse
import csv
import os
import sys
import time
import traceback

from letsrolld import film
from letsrolld import filmlist
from letsrolld.directorlist import read_director_list

_SEC_WAIT_ON_FAIL = 5

Expand All @@ -23,8 +25,9 @@ def get_directors_by_films(film_list):
directors[director.base_url] = director
yield director
break
except Exception as e:
# TODO: deduplicate error handling with update script
except (
Exception
) as e: # TODO: deduplicate error handling with update script
traceback.print_exception(e)
print(f"Retrying in {_SEC_WAIT_ON_FAIL} seconds...")
time.sleep(_SEC_WAIT_ON_FAIL)
Expand All @@ -42,11 +45,26 @@ def main():

film_list = list(filmlist.read_film_list(args.input))

with open(args.output, "w", newline="") as csvfile:
directors = set()
if os.path.exists(args.output):
print(f"Output file {args.output} already exists, appending to it...")
sys.stdout.flush()

directors = {d.uri for d in read_director_list(args.output)}

mode = "a" if directors else "w"
with open(args.output, mode, newline="") as csvfile:
writer = csv.writer(csvfile, dialect=csv.unix_dialect)
writer.writerow(["Name", "Letterboxd URI"])
if mode == "w":
writer.writerow(["Name", "Letterboxd URI"])
else:
csvfile.seek(0, os.SEEK_END)

for i, director_ in enumerate(get_directors_by_films(film_list), start=1):
if director_.base_url in directors:
print(f"Skipping director #{i}: {director_.name}")
sys.stdout.flush()
continue
print(f"Adding director #{i}: {director_.name}")
sys.stdout.flush()
writer.writerow([director_.name, director_.base_url])
Expand Down

0 comments on commit decec5d

Please sign in to comment.