Skip to content

Instantly share code, notes, and snippets.

@samgrover
Last active April 6, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samgrover/d0d354edbcba1154b48f05f3721770bb to your computer and use it in GitHub Desktop.
Save samgrover/d0d354edbcba1154b48f05f3721770bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# A helper script to convert a CSV files of my books read into Markdown for website.
# Usage:
# books.py <books.csv exported from Books.numbers>
import sys
import csv
from datetime import datetime
MAX_RATING = 5
AFFILIATE_ID = 'INDIEBOUND-AFFILIATE-ID'
def rating_to_stars(rating):
empty_stars = '☆' * (MAX_RATING - rating)
filled_stars = '★' * rating
return filled_stars + empty_stars
# Suffix snippet: https://stackoverflow.com/a/739266
def day_with_suffix(date):
day_suffix = ''
if 4 <= date.day <= 20 or 24 <= date.day <= 30:
day_suffix = "th"
else:
day_suffix = ["st", "nd", "rd"][date.day % 10 - 1]
return day_suffix
if len(sys.argv) != 2:
print("This script requires the following arguments:")
print("books.py <books.csv exported from Books.numbers>")
exit(0)
with open(sys.argv[1]) as csv_file:
reader = csv.reader(csv_file)
for idx, row in enumerate(reader):
if idx == 0:
continue
title = row[1]
author = row[2]
isbn = row[6].replace('"', '').replace('=', '')
my_rating = int(row[7])
stars = rating_to_stars(my_rating)
date = datetime.strptime(row[15], '%Y/%m/%d')
day_suffix = day_with_suffix(date)
date = date.strftime('%-d') + day_suffix + ' ' + date.strftime('%b, %Y')
title_and_author = ""
if not isbn:
title_and_author = f"{title} by {author}"
query = f"{title} {author}"
title_and_author = f"[{title_and_author}](https://www.indiebound.org/search/book?aff={AFFILIATE_ID}&keys={query})"
else:
title_and_author = f"[{title} by {author}](https://www.indiebound.org/book/{isbn}?aff={AFFILIATE_ID})"
if my_rating != 0:
print(f"{date} ")
print(f"{title_and_author} ")
print(f"{stars} ")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment