-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha_render.py
More file actions
75 lines (66 loc) · 2.32 KB
/
captcha_render.py
File metadata and controls
75 lines (66 loc) · 2.32 KB
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
import os
import random
import time
import hashlib
import math
import base64
import render_katex
# boilerplate_header = r'''\documentclass[preview]{standalone}
# \usepackage{amsmath} \begin{document} \['''
# boilerplate_footer = r'''\] \end{document}'''
def generate_user_id():
# return sha384-hashed unix timestamp
timestamp = str(time.time()).replace('.', '')
return hashlib.sha384(bytes(timestamp, 'utf-8')).hexdigest()
def render_src(eqn):
# eqn -> user id
# with open(f'tmp.tex', 'w') as f:
# f.write(boilerplate_header + eqn + boilerplate_footer)
user_id = generate_user_id()
# os.system('pdflatex --interaction=batchmode tmp.tex 2>&1 > /dev/null')
# os.system('pdfcrop tmp.pdf > /dev/null')
# os.system('pdftoppm tmp-crop.pdf > tmp.ppm')
# os.system('pnmtopng -quiet tmp.ppm > {}.png'.format(user_id))
# os.system('rm tmp.tex tmp.aux tmp.log tmp.ppm tmp.pdf tmp-crop.pdf')
# convert image to base64 string
# with open(user_id + '.png', 'rb') as f:
# image_content = f.read()
# encoded = base64.b64encode(image_content)
# src = str(encoded, 'utf-8')
# os.remove(user_id + '.png')
# return HTML for src instead of an image or base64/png
src = render_katex.render_math_html(eqn)
return user_id, src
def process_line(line):
eqn, ans = line.split('=>')
eqn = eqn.strip()
ans = ans.strip()
return eqn, ans
def random_line(filename):
with open(filename, 'r') as f:
# remove whitespace and comments
lines = [process_line(l) for l in f.readlines()
if l[0] != '#' and len(l.strip()) > 0]
return random.choice(lines)
def render_random_captcha(filename):
eqn, ans = random_line(filename)
r = random.randint(2, 98)
eqn = eqn % str(r)
ans = eval(ans)(r)
user_id, src = render_src(eqn)
return user_id, src, ans
def render_each():
with open('eqns.txt', 'r') as f:
lines = [process_line(l) for l in f.readlines()
if l[0] != '#' and len(l.strip()) > 0]
for line in lines[20:]:
eqn, ans = line
r = random.randint(2, 98)
eqn = eqn % str(r)
ans = eval(ans)(r)
user_id = render_src(eqn)
print(user_id, ans)
input() # wait for enter key
os.remove(user_id + '.png')
if __name__ == '__main__':
render_each()