-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed.py
More file actions
executable file
·143 lines (109 loc) · 3.82 KB
/
speed.py
File metadata and controls
executable file
·143 lines (109 loc) · 3.82 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
import sys
import json
import ConfigParser
import os.path
import urllib
from termcolor import colored
import easygui as eg
import mechanize
import cookielib
import time
import sys
config = os.path.expanduser("~/.pagespeed-config")
parser = ConfigParser.SafeConfigParser()
#def gui():
# t = "Check Web Page Speed v 1.0"
# url = eg.enterbox(msg='Enter web site URL.', title='Web Page Speed v 1.0', default =' ', strip = True, image=None, root=None)
# return url
#gui()
def gui():
t = "Check Web Page Speed v 1.0"
url2 = eg.enterbox(msg='Enter web site URL.', title='Web Page Speed v 1.0', default =' ', strip = True, image=None, root=None)
# eg.exceptionbox(msg="You entered %s" % url2,title="Web Page Speed v 1.0" )
# return url2
msg = "Do you want to continue check %s ?" % url2
title = "Please Confirm to continue"
if eg.ccbox(msg, title):
pass
return url2
else:
sys.exit(0)
def out(text):
eg.codebox(msg='Speed Results', title='Web Page Speed Results', text='%s' % (text))
eg.msgbox("%s" %text)
#out()
def main():
try:
parser.read(config)
api_key = parser.get('pagespeed', 'API_KEY')
except:
print "Unable to read the config file %s. It should look like this:\n" % config
print "[pagespeed]"
print "API_KEY = AIzaSyCHpTlqb_2PKmMFkOqXTl0ldcuuoGvqR2I\n"
print "See https://developers.google.com/speed/docs/insights/v1/getting_started for details on obtaining a key."
sys.exit()
try:
#url = sys.argv[1]
url = gui()
except:
print "URL to check not specified."
sys.exit()
if not "http" in url:
url = "http://%s" % url
qs = {}
qs["prettyprint"] = "false"
qs["key"] = api_key
qs["strategy"] = "desktop"
qs["url"] = url
url2="%s" % url
br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Ubuntu/3.0.1-1.fc9 Firefox/3.0.1')]
start_timer = time.time()
r = br.open(url2)
r.read()
html = r.read()
latency = time.time() - start_timer
endpoint="https://www.googleapis.com/pagespeedonline/v1/runPagespeed?%s" % urllib.urlencode(qs)
response = urllib.urlopen(endpoint)
jso = json.load(response)
total_score = jso["score"]
print "\nTotal score: %d" % total_score
print "\nTotal time without 3-rd party: %s" % colored(latency, "red")
return jso
def main2(jso):
for short_name, details in jso["formattedResults"]["ruleResults"].items():
full_name = details["localizedRuleName"]
score = details["ruleScore"]
impact = details["ruleImpact"]
# if score == 100:
# continue
print "\n%s (%d)" % (full_name, score)
if "urlBlocks" in details:
for index, block in enumerate(details["urlBlocks"]):
header = block["header"]
summary = header["format"]
if not "args" in header:
print summary
else:
for index, arg in enumerate(header["args"]):
summary = summary.replace("$%d" % (index + 1), arg["value"])
print summary
if not "urls" in block:
continue
for index, url in enumerate(block["urls"]):
message = url["result"]["format"]
for index, arg in enumerate(url["result"]["args"]):
message = message.replace("$%d" % (index + 1), arg["value"])
print " - %s " % message
eg.textbox(msg='', title=' ', text='Wep site title : %s\n Web site score : %s' %(full_name, score), codebox=0)
return
t = main()
main2(t)
t2 = main2(t)
eg.msgbox("Web page Speed test %s" %t2)