Skip to content

Instantly share code, notes, and snippets.

@Kawzeg
Last active February 13, 2021 08:44
Show Gist options
  • Save Kawzeg/d6b6d9c5f0d1113ea744e724a24741f4 to your computer and use it in GitHub Desktop.
Save Kawzeg/d6b6d9c5f0d1113ea744e724a24741f4 to your computer and use it in GitHub Desktop.
Gitland map collector
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', '![](icons/ux)').replace('ug', '![](icons/ug)').replace('ur', '![](icons/ur)').replace('ub', '![](icons/ub)').replace('cg', '![](icons/cg)').replace('cr', '![](icons/cr)').replace('cb', '![](icons/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
#!/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
@Kawzeg
Copy link
Author

Kawzeg commented Apr 27, 2020

Requires (pip install ):

  • gitpython
  • markdown
  • weasyprint
  • pdf2image

ffmpeg is used to assemble the images into mp4, python could be used for that as well.

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