|
import json |
|
import os |
|
from urllib import request |
|
import requests |
|
from bs4 import BeautifulSoup |
|
|
|
|
|
# Used to generate teamColors.json |
|
def scraper(): |
|
URL = "https://teamcolorcodes.com/premier-league-color-codes/" |
|
|
|
page =requests.get(URL) |
|
soup = BeautifulSoup(page.text, 'html.parser') |
|
data = [] |
|
for team in soup.select(".team-button"): |
|
subpage = requests.get(team.attrs['href']) |
|
subsoup = BeautifulSoup(subpage.text,'html.parser') |
|
teamdata = {} |
|
teamdata['name'] = team.text |
|
teamdata['colors'] = []; |
|
for block in subsoup.select('.colorblock'): |
|
a = block.text |
|
color = a[a.find("#"):a.find("#") + 7] |
|
teamdata['colors'].append(color) |
|
data.append(teamdata) |
|
|
|
with open('teamColors.json', 'w') as fp: |
|
json.dump(data, fp) |
|
|
|
# Used to generate SVGs |
|
def generator(): |
|
json_data = open('teamColors.json') |
|
data = json.load(json_data) |
|
configs = [] |
|
prefix = "epl_" |
|
if not os.path.exists('svg'): |
|
os.makedirs('svg') |
|
|
|
for team in data: |
|
colors = team['colors'] |
|
if get_white(colors) is not None: |
|
colors.remove(get_white(colors)) |
|
count = colors.__len__() |
|
if count >= 3: |
|
# INNER OUTER LOGO |
|
configs.extend([ |
|
{'name': team['name'] + "-1", 'colors': [colors[0], colors[1], colors[2]]}, |
|
{'name': team['name'] + "-2", 'colors': [colors[1], colors[2], colors[0]]}, |
|
{'name': team['name'] + "-3", 'colors': [colors[2], colors[0], colors[1]]}, |
|
{'name': team['name'] + "-4", 'colors': [colors[0], colors[1], "#FFFFFF"]}, |
|
{'name': team['name'] + "-5", 'colors': [colors[1], colors[2], "#FFFFFF"]}, |
|
{'name': team['name'] + "-6", 'colors': [colors[2], colors[0], "#FFFFFF"]} |
|
]) |
|
if count == 2: |
|
configs.extend([ |
|
{'name': team['name'] + "-1", 'colors': [colors[0], colors[1], colors[1]]}, |
|
{'name': team['name'] + "-2", 'colors': [colors[1], colors[0], colors[0]]}, |
|
{'name': team['name'] + "-3", 'colors': [colors[0], colors[1], "#FFFFFF"]}, |
|
{'name': team['name'] + "-4", 'colors': [colors[1], colors[0], "#FFFFFF"]} |
|
]) |
|
if count == 1: |
|
configs.append( |
|
{'name': team['name'] + "-1", 'colors': ["#FFFFFF",colors[0], colors[0]]} |
|
) |
|
else: |
|
count = colors.__len__() |
|
if count >= 3: |
|
# INNER OUTER LOGO |
|
configs.extend([ |
|
{'name': team['name'] + "-1", 'colors': [colors[0], colors[1], colors[2]]}, |
|
{'name': team['name'] + "-2", 'colors': [colors[1], colors[2], colors[0]]}, |
|
{'name': team['name'] + "-3", 'colors': [colors[2], colors[0], colors[1]]} |
|
]) |
|
if count == 2: |
|
configs.extend([ |
|
{'name': team['name'] + "-1", 'colors': [colors[0], colors[1], colors[1]]}, |
|
{'name': team['name'] + "-2", 'colors': [colors[1], colors[0], colors[0]]} |
|
]) |
|
if count == 1: |
|
configs.append( |
|
{'name': team['name'] + "-1", 'colors': ["#FFFFFF", colors[0], colors[0]]} |
|
) |
|
|
|
|
|
for config in configs: |
|
src = open('icon.svg').read() |
|
src = src.replace('color_1', config['colors'][0]) |
|
src = src.replace('color_2', config['colors'][1]) |
|
src = src.replace('color_3', config['colors'][2]) |
|
|
|
f = open("svg/" + prefix + config['name'].replace(" ", '-') + '.svg', 'w') |
|
f.write(src) |
|
f.close() |
|
|
|
def get_white(colors): |
|
for color in colors: |
|
if color == "#FFFFFF" or color == "#ffffff": |
|
return color |
|
|
|
return None |
|
# scraper() |
|
generator() |