Created
January 27, 2022 23:35
-
-
Save blalop/5296d3807d3938d641af80b4819ea21b to your computer and use it in GitHub Desktop.
Fill letterboxd exported data using OMDb API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
import requests | |
DIR = '/path/to/files' | |
API_KEY = 'apikey' | |
URL = 'http://www.omdbapi.com' | |
_, TITLE, YEAR, _ = range(4) | |
def read_files(dir): | |
with open(DIR + 'watched.csv') as f: | |
reader = csv.reader(f) | |
watched = list(reader) | |
with open(DIR + 'watchlist.csv') as f: | |
reader = csv.reader(f) | |
watchlist = list(reader) | |
for film in watched: | |
film.pop() | |
film.append('Watched') | |
for film in watchlist: | |
film.pop() | |
film.append('Watchlist') | |
return watched + watchlist | |
def get_director(film): | |
query_params = {'t': film[TITLE], 'y': film[YEAR], 'apikey': API_KEY} | |
r = requests.get(URL, params=query_params) | |
try: | |
return json.loads(r.text)['Director'] | |
except: | |
return '' | |
def write_csv(films): | |
with open('output.csv', 'w') as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerow(['Date', 'Title', 'Year', 'Status', 'Director']) | |
writer.writerows(films) | |
films = read_files(DIR) | |
for film in films: | |
film.append(get_director(film)) | |
print(films) | |
write_csv(films) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment