Skip to content

Commit

Permalink
Moving towrads pictures in news
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej-Marsic committed Sep 15, 2023
1 parent 5182f47 commit bd12d8a
Show file tree
Hide file tree
Showing 19 changed files with 2,148 additions and 21 deletions.
Binary file added assets/battle0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 22 additions & 8 deletions diplomacy_news/get_backstabbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@


def get_backstabbr():
res = requests.get("https://www.backstabbr.com/game/KGB/5178831816753152")
base_url = "https://www.backstabbr.com"
url = base_url + "/game/KGB/5178831816753152"
res = requests.get(url)
bs = BeautifulSoup(res.text, "lxml")
stage = json.loads(re.search("var stage = (.*)", res.text)[1][:-1])
stage = get_property("stage", res)
if stage in ["SATISFIED", "NEEDS_BUILDS"]:
prev_season = bs.find("a", {"id": "history_previous_season"})["href"]
res = requests.get("https://www.backstabbr.com" + prev_season)
url = base_url + prev_season
res = requests.get(url)
bs = BeautifulSoup(res.text, "lxml")
# stage = json.loads(re.search("var stage = (.*)", res.text)[1][:-1])
season = bs.find("a", {"id": "history_current_season"})
if season:
season = season.text.strip().title()
orders = json.loads(re.search("var orders = (.*)", res.text)[1][:-1])
units_by_player = json.loads(
re.search("var unitsByPlayer = (.*)", res.text)[1][:-1]
)
territories = json.loads(re.search("var territories = (.*)", res.text)[1][:-1])
orders = get_property("orders", res)
units_by_player = get_property("units_by_player", res)
territories = get_property("territories", res)
units_by_player = get_property("unitsByPlayer", res)
orders = get_property("orders", res)
return orders, units_by_player, territories, season


def get_property(property_name, res):
pattern = f"var {property_name} = (.*)"
match = re.search(pattern, res.text)
if match:
property_raw = match[1][:-1]
property = json.loads(property_raw)
else:
property = ""
return property
133 changes: 133 additions & 0 deletions diplomacy_news/get_war_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import io
from pathlib import Path

import cairosvg
from bs4 import BeautifulSoup
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager


def get_war_map(url):
svg_element = get_svg_element(url)

# Path("/tmp/lol.svg").write_text(str(svg_element))
background_image = Image.open("map_background.png")
png_data = cairosvg.svg2png(
bytestringstr(svg_element),
parent_width=background_image.size[0],
parent_height=background_image.size[1],
)
png_image = Image.open(io.BytesIO(png_data))
background_image = background_image.resize(png_image.size)
composite_image = Image.alpha_composite(
background_image.convert("RGBA"), png_image.convert("RGBA")
)
output_image = "assets/map.png"
composite_image.save(output_image, "PNG")


def get_svg_element(url):
options = Options()
options.add_argument("headless")
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s, options=options)
driver.maximize_window()
driver.get(url)
element = driver.find_element(By.XPATH, '//*[@id="map"]').get_attribute("innerHTML")
driver.quit()

if element:
bs = BeautifulSoup(element, "lxml")
svg_element = bs.find("svg")
else:
svg_element = ""

return svg_element


def get_battle_map(battle_coords, i):
image = Image.open("assets/map.png")
cropped_image = image.crop(battle_coords)
filename = f"assets/battle{i}.png"
cropped_image.save(filename)
return filename


def get_battles_coords(battles, metadata):
battles_coords = [get_battle_coords(battle, metadata) for battle in battles]
return battles_coords


def get_battle_coords(battle, metadata):
battle_coords = [
get_territoritory_coords(territory, metadata) for territory in battle
]
battle_coords = get_4_extremes(battle_coords)
battle_coords = make_square(battle_coords)
return battle_coords


def get_territoritory_coords(territory, metadata):
img_width = 604
width = 609
img_height = 560
height = 559
ter_path = metadata[territory]["path"]
full_ter_path = [t[1:].split(",") for t in ter_path.split()]
full_ter_path = [[int(i.replace("Z", "")) for i in t] for t in full_ter_path]
full_ter_path = [t for t in full_ter_path if len(t) == 2]
full_ter_path = [
[t[0] / width * img_width, t[1] / height * img_height] for t in full_ter_path
]
coords = get_2_extremes(full_ter_path)
return coords


def get_2_extremes(full_ter_path):
x1 = min([t[0] for t in full_ter_path])
x2 = max([t[0] for t in full_ter_path])
y1 = min([t[1] for t in full_ter_path])
y2 = max([t[1] for t in full_ter_path])
coords = (x1, y1, x2, y2)
return coords


def get_4_extremes(battle_coords):
x1 = min([t[0] for t in battle_coords])
y1 = min([t[1] for t in battle_coords])
x2 = max([t[2] for t in battle_coords])
y2 = max([t[3] for t in battle_coords])
coords = (x1, y1, x2, y2)
return coords


def make_square(battle_coords):
img_width = 604
img_height = 560
new_battle_coords = list(battle_coords)
coord_width = battle_coords[2] - battle_coords[0]
coord_height = battle_coords[3] - battle_coords[1]
diff = coord_height - coord_width
if diff > 0:
new_battle_coords[0] -= diff / 2
new_battle_coords[2] += diff / 2
if new_battle_coords[0] < 0:
new_battle_coords[2] -= new_battle_coords[0]
new_battle_coords[0] = 0
if new_battle_coords[2] > img_width:
new_battle_coords[0] -= new_battle_coords[2] - img_width
new_battle_coords[2] = img_width
if diff < 0:
new_battle_coords[1] -= abs(diff) / 2
new_battle_coords[3] += abs(diff) / 2
if new_battle_coords[1] < 0:
new_battle_coords[3] -= new_battle_coords[1]
new_battle_coords[1] = 0
if new_battle_coords[3] > img_height:
new_battle_coords[1] -= new_battle_coords[3] - img_height
new_battle_coords[3] = img_height
return tuple(new_battle_coords)
Loading

0 comments on commit bd12d8a

Please sign in to comment.