-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosses.py
162 lines (131 loc) · 5.45 KB
/
losses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import requests, re, json, sys
from lxml import html
from tabulate import tabulate
import PIL.ImageEnhance as ImageEnhance
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
def tint_image(img, factor):
enhancer = ImageEnhance.Brightness(img)
return enhancer.enhance(factor)
def draw_text_on_image(data, date, background_path, font_path, font_size, header_font_size, header_text, text_color='white', header_color='yellow', tint_amount=0.35):
HEADER_TEXT = header_text % date
HEADER_COLOR = header_color # TODO make it configurable
HIGHLIGHT_COLOR = (255, 40, 0)
TINT_AMOUNT = tint_amount
TEXT_COLOR = text_color
img = tint_image(Image.open(background_path), TINT_AMOUNT)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(font_path, font_size)
header_font = ImageFont.truetype(font_path, header_font_size)
center_x, center_y = img.size[0] / 2, img.size[1] / 2
start_y = center_y - ((len(data) + 1) * font_size) / 2
draw.text(
(center_x - draw.textsize(HEADER_TEXT, font=header_font)[0] / 2, start_y - 3 * header_font_size),
HEADER_TEXT,
font=header_font,
align='center',
fill=HEADER_COLOR
)
# iterate over data and draw each line of text
for i, line in enumerate(data):
text_width = draw.textsize(line[0], font=font)[0]
draw.text(
(center_x - text_width - 10, start_y + i * font_size),
line[0] + ':',
font=font,
align='right',
fill=TEXT_COLOR
)
if ', ' in line[1]:
for j, sub_line in enumerate(line[1].split(', ')):
draw.text(
(center_x + 10, start_y + i * font_size + j * font_size),
sub_line.split('(+')[0],
font=font,
align='left',
fill=TEXT_COLOR
)
if '(+' in sub_line:
draw.text(
(center_x + 10 + draw.textsize(sub_line.split('(+')[0], font=font)[0], start_y + i * font_size + j * font_size),
'(+' + sub_line.split('(+')[1],
font=font,
align='left',
fill=HIGHLIGHT_COLOR
)
else:
draw.text(
(center_x + 10, start_y + i * font_size),
line[1].split('(+')[0],
font=font,
align='left',
fill=TEXT_COLOR
)
if '(+' in line[1]:
draw.text(
(center_x + 10 + draw.textsize(line[1].split('(+')[0], font=font)[0], start_y + i * font_size),
'(+' + line[1].split('(+')[-1],
font=font,
align='left',
fill=HIGHLIGHT_COLOR
)
img.save('./result.jpg')
def print_table(headers, data):
print(tabulate(data, headers, tablefmt='grid'))
def data_to_table(data):
return [row.split(' — ') for row in data]
def strip_data(data):
return [element.strip() for element in data if element.strip()]
def parse_data(data):
result = re.findall('\D+\d*.*?[\s\(.?\d+\)]?', data)
for i in range(len(result)):
if '(+' in result[i]:
result[i - 1] += result[i]
result[i] = ''
parsed = strip_data(result)
try:
army_index = [idx for idx, s in enumerate(parsed) if 'Особовий склад' in s][0]
army_joined = ' '.join(parsed[army_index:]).replace(' ,', ',')
return parsed[:army_index] + [army_joined]
except IndexError:
return parsed
def load_data(url):
page = requests.get(url)
tree = html.fromstring(page.content)
data = tree.xpath('//ul[@class="see-also"]/li[@class="gold"][1]/div[@class="casualties"]/div/ul/li//text()')
date = tree.xpath('//ul[@class="see-also"]/li[@class="gold"][1]/span[@class="black"]/text()')[0]
return ''.join(data).replace('\xa0', ' '), date
def main():
URL = 'https://index.minfin.com.ua/ua/russian-invading/casualties/'
data, date = load_data(URL)
data = data_to_table(parse_data(data))
# check if there is and argument and it is true
if len(sys.argv) > 1 and sys.argv[1] == '-i':
try:
with open('./config.json', encoding='utf8') as config_file:
config = json.load(config_file)
draw_text_on_image(data, date, **config)
except FileNotFoundError:
print('Config file not found, creating new.')
# create config.json file
with open('./config.json', 'w', encoding='utf8') as config_file:
json.dump(
{
'background_path': './resources/image.jpeg',
'font_path': './resources/font.ttf',
'font_size': 30,
'header_font_size': 46,
'header_text': 'Протягом 24.02-%s\n орієнтовані втрати противника склали:',
'text_color': 'white',
'header_color': 'yellow',
'tint_amount': 0.35
},
config_file,
indent=4,
ensure_ascii=False,
)
else:
print_table(['Category', f'Amount of losses ({date})'], data)
if __name__ == '__main__':
main()