import json
import datetime
import os
# Read the JSON file
with open('posts_1.json') as file:
data = json.load(file)
# Iterate over the data and grab everything we will need
for i, item in enumerate(data, start=1):
media = item['media'][0]
uri = media['uri']
title = media['title']
timestamp = media['creation_timestamp']
# Name the file that we will be creating (Note, these files are created in
# the same directory as the python script)
filename = f"instagram_{i}.md"
# The timestamp in the JSON isn't what I need for Hugo, so reformat!
dt = datetime.datetime.fromtimestamp(timestamp)
formatted_dt = dt.strftime('%Y-%m-%dT%H:%M:%S%z')
# Images have the directory structure defined in the URL when getting this
# data. I'm removing it so that it is just the actual file name. Hugo's
# templates take care of where that image is (I copied all the images to
# that folder).
image_name = os.path.basename(uri)
image_name = os.path.splitext(image_name)[0]
# Create the markdown file in the format / template that Hugo would expect.
# with open(filename, 'w') as md_file:
md_file.write(f"---\n")
md_file.write(f"title: 'Instagram-{i}'\n")
md_file.write(f"date: {formatted_dt}-04:00\n")
md_file.write(f"draft: false\n")
md_file.write(f"statusimage: '{image_name}.jpg'\n")
md_file.write(f"categories: ["photos"]\n")
# You should get the point here...
md_file.write(f"---\n")
# When it's done, let's not only inform ourselves that it worked, but let's also
# add in some words of encouragement.
print("Wow, I did it. I made all the files for you. You're so smart, now go do more amazing things.")