In Winter Bash 2014,
since there is no easy way to see the hats I'm missing per site,
I decided to use Gists for that.
A perhaps not so well-known feature of GitHub Flavered Markdown (GFM) format used everywhere on GitHub is that if you write bullet point lists with embedded [ ]
like this:
- [ ] Bill Lumbergh: answer 5 questions on Saturday (UTC)
- [ ] On The Road: ask using the app
- [ ] Bugdroid: use the Android app
- [ ] Not a cherry: use the iOS app
- [ ] I Voted Today: vote limit and 10+ questions
These will be rendered not as regular bullet point lists but a list of checkboxes: clicking on an item will toggle the checkbox, triggering a commit.
Just one little problem: there's no obviously easy way to copy-paste the hat names and descriptions from the Winter Bash website. So I cooked up this simple soup (pun totally intended) using Python's Beautiful Soup:
#!/usr/bin/env python
from urllib import request
from bs4 import BeautifulSoup
import os
URL = 'http://winterbash2014.stackexchange.com/'
def load_html_doc(url):
cached_file = 'page.html'
if os.path.isfile(cached_file) and os.path.getsize(cached_file):
with open(cached_file) as fh:
html_doc = fh.read()
else:
html_doc = request.urlopen(url).read()
with open(cached_file, 'wb') as fh:
fh.write(html_doc)
return html_doc
def get_soup(url):
html_doc = load_html_doc(url)
return BeautifulSoup(html_doc)
def print_hats_as_gfm_checkboxes(soup):
for box in soup.find_all('a', attrs={'class': 'box'}):
name = box.find(attrs={'class': 'hat-name'}).text
description = box.find(attrs={'class': 'hat-description'}).text
print('- [ ] {}: {}'.format(name, description))
def print_pretty(soup):
print(soup.prettify())
def main():
soup = get_soup(URL)
print_hats_as_gfm_checkboxes(soup)
# print_pretty(soup)
if __name__ == '__main__':
main()
This produces a list of all the hats in the right format to be suitable for copy-pasting into a Gist.
Anything to improve here?