-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
96 lines (85 loc) · 3.6 KB
/
api.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from browsermobproxy import Client
from selenium import webdriver
import urlparse
import logging
import datetime
import os
from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
class GET_PAGE_SIZE(Resource):
def get(self, URL):
try:
proxy = Client('im-expservices1.gksm.local:8999')
chromedriver = './chromedriver'
os.environ['webdriver.chrome.driver'] = chromedriver
url = urlparse.urlparse(proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--proxy-server={0}'.format(url))
driver = webdriver.Chrome(chromedriver,
chrome_options=chrome_options)
driver.set_window_size(1920, 1080)
try:
proxy.new_har(str(URL),
options={'captureHeaders': True,
'captureContent': True,
'captureBinaryContent': True})
driver.get(URL)
proxy.wait_for_traffic_to_stop(100, 20000)
except Exception, err:
return 'GET ERROR: ' + str(err)
S = lambda X: \
driver.execute_script('return document.body.parentNode.scroll'
+ X)
driver.set_window_size(S('Width'), S('Height'))
# har = proxy.har['log']['entries']
# mimeType = []
bodySize = []
download_time = []
counter = 0
for entry in proxy.har['log']['entries']:
counter += 1
# mimeType.append(entry['response']['content']['mimeType'])
bodySize.append(int(entry['response']['bodySize']))
download_time.append(int(entry['time']))
# example = defaultdict(dict)
# keys = defaultdict(dict)
# for i in range(1, len(mimeType)):
# for entries in range(1, len(har)):
# if (mimeType[i] == har[entries]['response']['content']['mimeType']):
# keys[mimeType[i]][entries] = {'bodySize': int(har[entries]['response']['bodySize']),'URL': str(har[entries]['request']['url'])}
# example['result'] = keys
driver.quit()
startDownloadTime = \
datetime.datetime.strptime(str(proxy.har['log'
]['entries'][0]['startedDateTime']),
'%Y-%m-%dT%H:%M:%S.%fZ')
LastStartDownloadTime = \
datetime.datetime.strptime(str(proxy.har['log'
]['entries'][counter - 1]['startedDateTime']),
'%Y-%m-%dT%H:%M:%S.%fZ')
proxy.close()
return {
'bodySize': str(sum(bodySize)),
'browser_download_time': str(sum(download_time)),
'LastStartDownloadTime': str(LastStartDownloadTime),
'startDownloadTime': str(startDownloadTime),
'total_download_time': str((LastStartDownloadTime
- startDownloadTime).total_seconds()),
}
except Exception, e:
return 'MAIN GET ERROR: ' + str(e)
try:
app.logger.disabled = True
log = logging.getLogger('werkzeug')
log.disabled = True
api.add_resource(GET_PAGE_SIZE, '/webpage_size/<path:URL>')
app.run(host='0.0.0.0', port=5001, debug=False)
except Exception, err:
print ('MAIN ERROR: ', str(err))