Skip to content

Instantly share code, notes, and snippets.

@BongoKnight
Created February 27, 2024 13:11
Show Gist options
  • Select an option

  • Save BongoKnight/5308476054f1bad762535e8292b0fc00 to your computer and use it in GitHub Desktop.

Select an option

Save BongoKnight/5308476054f1bad762535e8292b0fc00 to your computer and use it in GitHub Desktop.
Polarstep to Markdown
import json
from dataclasses import dataclass
import inspect
from datetime import date, datetime
@dataclass
class Step:
"Class for representing a PolarStep Step"
description: str
media: list
name: str
start_time: str
@property
def filename(self):
return f"{datetime.fromisoformat(self.start_time).date().isoformat()}.md"
@property
def media_urls(self):
urls = [media.get('path') for media in self.media]
return urls
@property
def image_mark(self):
lines = ""
for img in self.media:
url = img.get("path", "").split("/")[-1]
lines+= f"![]({url})\n"
return lines
@classmethod
def from_dict(cls, env):
return cls(**{
k: v for k, v in env.items()
if k in inspect.signature(cls).parameters
})
def __str__(self):
return f"# {self.name}\n\n{self.description}\n\n{self.image_mark}"
# Dump of the biggest JSON seen while visiting the polarstep page
data = {}
with open("polar.json", "r", encoding="utf-8") as f:
data = json.loads(f.read())
steps = data.get("steps",{})
def generate_markdown(steps):
for step_data in steps:
step = Step.from_dict(step_data)
with open(step.filename, "w", encoding="utf-8") as f:
f.write(str(step))
def download_media(steps):
import wget
for step_data in steps:
step = Step.from_dict(step_data)
if step.media_urls:
for url in step.media_urls:
wget.download(url)
generate_markdown(steps)
download_media(steps)
@BongoKnight
Copy link
Author

BongoKnight commented Feb 27, 2024

This script download all the images in full resolution and put each step description in a <step_date>.md file.

Each file have this structure :

# <Name of the step>

<Description of the step>

![](img 1)
...
![](img N)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment