-
Notifications
You must be signed in to change notification settings - Fork 0
/
makescreenshot.py
98 lines (75 loc) · 2.52 KB
/
makescreenshot.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
import os
from flask import Flask, request
from flask.ext import restful
from flask.ext.restful import reqparse
import random , string
import selenium.webdriver
import PIL
from PIL import Image
import base64
from io import BytesIO
import cStringIO
import boto
from boto.s3.key import Key
app = Flask(__name__)
api = restful.Api(app)
parser = reqparse.RequestParser()
parser.add_argument('url', type=str)
#AWS configurations
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
S3_BUCKET = ''
#Import secrets from the file the world should not see :P
try:
from config import *
except ImportError:
pass
conn = boto.connect_s3(config["AWS_ACCESS_KEY_ID"],config["AWS_SECRET_ACCESS_KEY"])
class TakeScreenshot(restful.Resource):
def post(self):
args = parser.parse_args()
finalink=take_screenshot(args['url'])
return finalink
def take_screenshot(url):
try:
webdriver = selenium.webdriver.PhantomJS('vendor/phantomjs/bin/phantomjs')
webdriver.get(url)
webdriver.set_window_size(1280,800)
imagedata = webdriver.get_screenshot_as_base64()
webdriver.close()
webdriver.quit()
except Exception, e:
raise
return process_screenshot(imagedata)
def process_screenshot(base64_img):
basewidth = 220
img = Image.open(BytesIO(base64.b64decode(base64_img)))
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
img = img.crop((0,0,basewidth,basewidth))
#saving the image into a cStringIO object to avoid writing to disk
out_img=cStringIO.StringIO()
img.save(out_img,'PNG')
return upload_to_s3(out_img)
def upload_to_s3(upload_img):
url = ''
try:
bucket = conn.get_bucket(config["S3_BUCKET"])
#generate an unique ID
random_char = [random.choice(string.ascii_letters + string.digits) for n in xrange(12)]
uniquid = "".join(random_char)
k = bucket.new_key('screenshots/%s.png' % uniquid)
k.set_contents_from_string(upload_img.getvalue(),headers={"Content-Type": "image/png"})
k.make_public()
url=k.generate_url(expires_in=0 , query_auth=False)
except boto.exception.s3responseerror, e:
raise
finally:
return url
api.add_resource(TakeScreenshot, '/takescreenshot')
#App Configuration
app.config.from_pyfile('settings.py')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug= True)