Skip to content

Instantly share code, notes, and snippets.

@bennydictor
Created July 3, 2019 10:58
Show Gist options
  • Save bennydictor/c3f6fb149774da429897e9ee38dce414 to your computer and use it in GitHub Desktop.
Save bennydictor/c3f6fb149774da429897e9ee38dce414 to your computer and use it in GitHub Desktop.
Make executable Python code screenshots
from bs4 import BeautifulSoup
from requests import Session
import sys
import zipfile
if len(sys.argv) >= 2:
code = open(sys.argv[1]).read()
else:
code = sys.stdin.read()
if not code:
raise ValueError('Code must not be empty')
session = Session()
r = session.get('http://codephoto.ru/')
soup = BeautifulSoup(r.content, 'lxml')
csrf_token = soup.select_one('input[name="csrf_token"]')['value']
r = session.post('http://codephoto.ru/code', allow_redirects=False, data={
'csrf_token': csrf_token,
'language': 'Python',
'code': code,
})
img_loc = r.headers['Location'].split('/')[-1]
r = session.get(f'http://codephoto.ru/upload/{img_loc}.jpg')
img = r.content
with zipfile.ZipFile('output.jpg', 'w') as out:
out.writestr('__main__.py', code)
pyzip = open('output.jpg', 'rb').read()
with open('output.jpg', 'wb') as out:
out.write(img)
out.write(pyzip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment