Last active
February 13, 2021 08:44
-
-
Save Kawzeg/d6b6d9c5f0d1113ea744e724a24741f4 to your computer and use it in GitHub Desktop.
Gitland map collector
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 os | |
import git | |
import markdown | |
from weasyprint import HTML, CSS | |
from pdf2image import convert_from_path | |
# Copied from gitland server.py | |
def mapToMd(map): | |
return map.replace('ux', '').replace('ug', '').replace('ur', '').replace('ub', '').replace('cg', '').replace('cr', '').replace('cb', '').replace(',', ' ').strip().replace('\n', ' \n') | |
# Collect all versions of 'map' on master | |
repo = git.Repo('./') | |
commits = list(repo.iter_commits('master')) | |
maps = [] | |
# Iterates from latest commit backwards | |
for commit in commits: | |
try: | |
map = commit.tree['map'] | |
maps.append(map.data_stream.read()) | |
except KeyError: # Map isn't part of the commit | |
pass | |
size = len(maps) | |
maps = reversed(maps) # Reverse to get oldest first | |
# Create timelapse directory | |
directory = 'timelapse' | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
# For each map: | |
# map -> markdown -> html -> pdf -> png | |
i = 0 | |
for bmap in maps: | |
print(f'{i:06} of {size:06}') | |
mapFile = os.path.join(directory, f'map{i:06}') | |
pngFile = mapFile + '.png' | |
if os.path.exists(pngFile): | |
print(f'{pngFile} already exists, skipping') | |
i += 1 | |
continue | |
# Save map to file | |
map = bmap.decode('utf-8') | |
map = map.strip() # Some map files contain many newlines | |
with open(mapFile, 'w') as f: | |
f.write(map) | |
# Generate markdown | |
mdString = mapToMd(map) | |
# Generate HTML | |
htmlString = markdown.markdown(mdString) | |
htmlFile = mapFile + '.html' | |
with open(htmlFile, 'w') as f: | |
f.write(htmlString) | |
# Generate PDF using weasyprint | |
pdfFile = mapFile + '.pdf' | |
HTML(htmlFile).write_pdf(pdfFile, | |
stylesheets=[CSS(string='@page {margin: 0;size: 10in;}')]) | |
# 10in size fits the whole map on 1 page | |
# Read PDF and convert to PNG using pdf2image | |
images = convert_from_path(pdfFile) # Each pdf page becomes one image | |
# We should only have a single image | |
assert len(images) == 1 | |
images[0].save(pngFile) | |
i += 1 |
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
#!/usr/bin/env bash | |
# Create PNG files from commits | |
python collect_map.py && ffmpeg -r 40 -f image2 -s 1280x720 -i timelapse/map%06d.png -vcodec libx264 -crf 20 -pix_fmt yuv420p timelapse.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires (pip install ):
ffmpeg is used to assemble the images into mp4, python could be used for that as well.