Created
February 27, 2024 13:11
-
-
Save BongoKnight/5308476054f1bad762535e8292b0fc00 to your computer and use it in GitHub Desktop.
Polarstep to Markdown
This file contains hidden or 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 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"\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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :