Skip to content

Instantly share code, notes, and snippets.

@dakerfp
Created August 6, 2021 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakerfp/5dda6f02fe6edcda0921558452fabe7b to your computer and use it in GitHub Desktop.
Save dakerfp/5dda6f02fe6edcda0921558452fabe7b to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
import re
from bs4.element import ContentMetaAttributeValue
def trim(s):
return s.strip().replace('\n', '')
class Color(object):
def __init__(self, name, brand, code):
self.name = name
self.brand = brand
self.code = code
def __repr__(self):
return self.name
def __eq__(self, o):
if isinstance(o, str):
return self.name.upper() == o.upper()
if isinstance(o, Color):
return self.name == o.name and \
self.brand == o.brand and \
self.code == o.code
def __hash__(self):
return hash(self.name) ^ hash(self.brand) ^ hash(self.code)
def parse_redgrimm_conversion_page(source_html):
soup = BeautifulSoup(source_html, 'html.parser')
line_range = soup.find('h1').text
conversion = {}
for tr in soup.find_all('tr'):
equivalencies = []
for td in tr.find_all('td'):
strong = td.find('strong')
if not strong:
continue
color_name = strong.text
txt = trim(td.text)
if not txt:
continue
contents = [c for c in td.contents if c != '\n']
if len(td.contents) < 3:
continue
match = re.search(r'(.*)(\ \-\ )(\#[A-Fa-f0-9]+)', contents[2])
assert match is not None
color_brand = match.group(1)
color_code = match.group(3).upper()
equivalencies.append(Color(color_name, color_brand, color_code))
if len(equivalencies) > 1:
conversion[equivalencies[0]] = equivalencies[1:]
return conversion
if __name__ == '__main__':
citadel_html = requests.get('https://redgrimm.github.io/paint-conversion/index.html').text
vallejo_game_html = requests.get('https://redgrimm.github.io/paint-conversion/vallejo-game.html').text
vallejo_model_html = requests.get('https://redgrimm.github.io/paint-conversion/vallejo-game.html').text
p3_html = requests.get('https://redgrimm.github.io/paint-conversion/p3.html').text
army_painter_html = requests.get('https://redgrimm.github.io/paint-conversion/army-painter.html').text
citadel_conversion = parse_redgrimm_conversion_page(citadel_html)
p3_conversion = parse_redgrimm_conversion_page(p3_html)
vallejo_game_conversion = parse_redgrimm_conversion_page(vallejo_game_html)
vallejo_model_conversion = parse_redgrimm_conversion_page(vallejo_model_html)
army_painter_conversion = parse_redgrimm_conversion_page(army_painter_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment