Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Last active July 12, 2023 15:25
Show Gist options
  • Save Xnuvers007/da86575b76ecada46f0766f7d884a632 to your computer and use it in GitHub Desktop.
Save Xnuvers007/da86575b76ecada46f0766f7d884a632 to your computer and use it in GitHub Desktop.
This is script for html2image or get screenshot with python and replit.com -> my project = https://replit.com/@Xnuvers007/s website = https://s.xnuvers007.repl.co/ss?url=https://google.com
from flask import Flask, request, send_from_directory, jsonify, redirect, url_for
from html2image import Html2Image
import os
from threading import Timer
from urllib.parse import urlparse
app = Flask(__name__)
# Configure Html2Image with custom flags
hti = Html2Image(output_path='output/',
custom_flags=[
'--no-sandbox',
'--virtual-time-budget=5000',
'--window-size=1920,1080',
'--hide-scrollbars',
'--device-scale-factor=2.0',
'--full-page',
'--quality=100',
'--scroll-down=100'
]
)
def delete_file(file_path):
if os.path.exists(file_path):
os.remove(file_path)
@app.route('/ss')
def take_screenshot():
url = request.args.get('url')
if url:
# Parse the URL
parsed_url = urlparse(url)
# Check if the URL has a scheme (http:// or https://)
if not parsed_url.scheme:
# Add the https:// prefix if only the domain is provided
if not parsed_url.netloc:
url = 'https://' + url
else:
# Add the http:// prefix if no scheme is provided
url = 'http://' + url
# Generate the unique filename
domain = parsed_url.netloc
count = 1
filename = f'screenshot{count}.png'
while os.path.exists(os.path.join(hti.output_path, filename)):
count += 1
filename = f'screenshot{count}.png'
# Take the screenshot
screenshot_path = os.path.join(hti.output_path, filename)
hti.screenshot(url=url, save_as=filename)
# Schedule file deletion after 30 seconds
t = Timer(30, delete_file, (screenshot_path,))
t.start()
return f'<h1 style="text-align: center; font-weight: bold;">Created By Xnuvers007</h1><br>Screenshot generated for URL: {url}<br>Visit your image <a href="/output/{filename}" target="_blank">here</a><br><br><a href="http://ndraeee25.000webhostapp.com/dana/DanaXnuvers007.jpeg"> Donate me </a>'
else:
# show the host/url server
return f'Please provide a valid URL.<br>parameter url: {request.args.get("url")}<br>server = http://s.xnuvers007.repl.co/ss?url='
@app.route('/ssjson')
def take_screenshot2():
url = request.args.get('url')
if url:
# Parse the URL
parsed_url = urlparse(url)
# Check if the URL has a scheme (http:// or https://)
if not parsed_url.scheme:
# Add the https:// prefix if only the domain is provided
if not parsed_url.netloc:
url = 'https://' + url
else:
# Add the http:// prefix if no scheme is provided
url = 'http://' + url
# Generate the unique filename
domain = parsed_url.netloc
count = 1
filename = f'screenshot{count}.png'
while os.path.exists(os.path.join(hti.output_path, filename)):
count += 1
filename = f'screenshot{count}.png'
# Take the screenshot
screenshot_path = os.path.join(hti.output_path, filename)
hti.screenshot(url=url, save_as=filename)
# Schedule file deletion after 30 seconds
t = Timer(30, delete_file, (screenshot_path,))
t.start()
response = {
'message': 'Screenshot generated successfully',
'author': 'Xnuvers007 (https://github.com/Xnuvers007)',
'donate': 'http://ndraeee25.000webhostapp.com/dana/DanaXnuvers007.jpeg',
'url': url,
'image_url': f'https://s.xnuvers007.repl.co/output/{filename}',
}
return jsonify(response)
else:
# show parameter url/server
return jsonify({'error': 'Please provide a valid URL.',
'server': 'http://s.xnuvers007.repl.co/ssjson?url='}
)
@app.route('/output/<path:filename>')
def get_screenshot(filename):
return send_from_directory(hti.output_path, filename)
@app.errorhandler(404)
def page_not_found(error):
redirect_url = url_for('take_screenshot2', url=request.args.get('url'))
return redirect(redirect_url)
@app.errorhandler(500)
def internal_server_error(error):
redirect_url = url_for('take_screenshot2', url=request.args.get('url'))
return redirect(redirect_url)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=False)
@Xnuvers007
Copy link
Author

Xnuvers007 commented Jul 12, 2023

requirements.txt

pip install flask
pip install html2image

type this in shell replit
chromium-browser --version (choose ungoogled chromium)

then run

@Xnuvers007
Copy link
Author

117.54.114.99:80
203.146.127.159:80
57.128.12.85:80
202.5.16.44:80

here's proxy free

@Xnuvers007
Copy link
Author

i dont know why in replit cannot screenshot website use path, i have tested in windows and linux that is can screenshot website

windows localhost:
gambar
gambar

replit:
gambar
gambar
gambar

but if i input domain , it can be screenshot
gambar
gambar
gambar

@Xnuvers007
Copy link
Author

Xnuvers007 commented Jul 12, 2023

solved:
it turns out that it comes from the replit server that is slow or unable to

indo:
ternyata emang dari server replitnya, saya coba lagi bisa
gambar
gambar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment