-
Notifications
You must be signed in to change notification settings - Fork 8
/
camposter.py
executable file
·68 lines (51 loc) · 1.92 KB
/
camposter.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
#!/usr/bin/env python3
from configparser import ConfigParser
import io
from PIL.ImageFont import ImageFont
from fire import Fire
import requests
import re
from PIL import Image, ImageDraw, ImageFont
from lib.files import DATA_DIR, CFG_DIR
from lib.utils import eip4
CFG_PATH = CFG_DIR / 'camposter.ini'
FONT_PATH = DATA_DIR / 'fonts' / 'opensans.ttf'
IP_RE = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
def main(stream_url, image_path, location):
print(stream_url, image_path, location)
cfg = ConfigParser()
cfg.read(str(CFG_PATH))
token, chat_id = cfg['tg']['token'], cfg['tg']['chat_id']
password, water = cfg['enc']['password'], cfg['enc']['water']
ip = IP_RE.findall(stream_url)[0]
water = '%s %s' % (eip4(ip), water)
url = 'https://api.telegram.org/bot%s/sendphoto' % token
data = dict(
chat_id=chat_id,
caption='```%s```' % stream_url,
parse_mode='Markdown'
)
with open(image_path, 'rb') as f:
img: Image = Image.open(f)
w, h = img.size
text = ' '.join(location)
print(w, h)
font_size = int(0.04 * min(h, w))
px = int(0.005 * w)
py = int(0.002 * h)
font = ImageFont.truetype(str(FONT_PATH), font_size)
draw = ImageDraw.Draw(img, 'RGBA')
_, text_height = draw.textsize('Wg', font)
water_width, _ = draw.textsize(water, font)
text_y = h - py - text_height
draw.rectangle((0, text_y - py, w, h), fill=(0, 0, 0, 160))
draw.text((px, text_y), text, fill='yellow', font=font)
draw.text((w - px - water_width, text_y), water,
fill=(255, 255, 255, 128), font=font)
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
response = requests.post(url, data=data, files={'photo': img_byte_arr})
print(response.json())
if __name__ == "__main__":
Fire(main)