Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0c800e

Browse files
author
Mattias
committed
we can now get the result as json file
1 parent 5115a16 commit d0c800e

4 files changed

Lines changed: 33 additions & 27 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ venv.bak/
108108
.mypy_cache/
109109
.DS_Store
110110
dbase/dbase.sqlite
111+
result.json

checks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
request_timeout = config.http_request_timeout
1616
googlePageSpeedApiKey = config.googlePageSpeedApiKey
1717

18+
ALL_TESTS = -1
19+
1820
(GOOGLE_PAGESPEED, UNKNOWN_01, PAGE_NOT_FOUND, UNKNOWN_03, UNKNOWN_04, UNKNOWN_05, HTML, CSS, UNKNOWN_08, UNKNOWN_09, UNKNOWN_10, UNKNOWN_11, UNKNOWN_12, UNKNOWN_13, UNKNOWN_14, UNKNOWN_15, UNKNOWN_16, UNKNOWN_17, UNKNOWN_18, UNKNOWN_19, WEBBKOLL) = range(21)
1921

2022
def check_four_o_four(url):

default.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ def testsites(sites, test_type=None, only_test_untested_last_hours=24, order_by=
1313
* test_type=num|None to execute all available tests
1414
"""
1515

16+
result = list()
17+
1618
# TODO: implementera test_type=None
1719

1820
print("###############################################")
1921

2022
i = 1
2123

22-
#sites = list()
23-
#for row in result:
24-
# site_id = row[0]
25-
# website = row[1]
26-
# sites.append([site_id, website])
27-
2824
print('Webbadresser som testas:', len(sites))
2925

3026
for site in sites:
@@ -59,33 +55,39 @@ def testsites(sites, test_type=None, only_test_untested_last_hours=24, order_by=
5955
checkreport = str(the_test_result[1]).encode('utf-8') # för att lösa encoding-probs
6056
jsondata = str(json_data).encode('utf-8') # --//--
6157

62-
site_test = SiteTests(site_id=site_id, type_of_test=test_type, check_report=checkreport, rating=the_test_result[0], test_date=datetime.datetime.now(), json_check_data=jsondata)
58+
site_test = SiteTests(site_id=site_id, type_of_test=test_type, check_report=checkreport, rating=the_test_result[0], test_date=datetime.datetime.now(), json_check_data=jsondata).todata()
59+
60+
result.append(site_test)
6361

6462
the_test_result = None # 190506 för att inte skriva testresultat till sajter när testet kraschat. Måste det sättas till ''?
6563
except Exception as e:
6664
print('FAIL!', website, '\n', e)
6765
pass
6866

6967
i += 1
68+
69+
return result
7070

71-
def testing(sites = list([[0, "https://webperf.se"]]), test_type= -1):
71+
def testing(sites, test_type= ALL_TESTS):
7272
print('### {0} ###'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
73+
tests = {}
7374
##############
74-
if (test_type == -1 or test_type == GOOGLE_PAGESPEED):
75+
if (test_type == ALL_TESTS or test_type == GOOGLE_PAGESPEED):
7576
print('###############################\nKör test: 0 - Google Pagespeed')
76-
testsites(sites, test_type=GOOGLE_PAGESPEED)
77-
if (test_type == -1 or test_type == PAGE_NOT_FOUND):
77+
tests['google_pagespeed'] = testsites(sites, test_type=GOOGLE_PAGESPEED)
78+
if (test_type == ALL_TESTS or test_type == PAGE_NOT_FOUND):
7879
print('###############################\nKör test: 2 - 404-test')
79-
testsites(sites, test_type=PAGE_NOT_FOUND)
80-
if (test_type == -1 or test_type == HTML):
80+
tests['404'] = testsites(sites, test_type=PAGE_NOT_FOUND)
81+
if (test_type == ALL_TESTS or test_type == HTML):
8182
print('###############################\nKör test: 6 - HTML')
82-
testsites(sites, test_type=HTML)
83-
if (test_type == -1 or test_type == CSS):
83+
tests['html'] = testsites(sites, test_type=HTML)
84+
if (test_type == ALL_TESTS or test_type == CSS):
8485
print('###############################\nKör test: 7 - CSS')
85-
testsites(sites, test_type=CSS)
86-
if (test_type == -1 or test_type == WEBBKOLL):
86+
tests['css'] = testsites(sites, test_type=CSS)
87+
if (test_type == ALL_TESTS or test_type == WEBBKOLL):
8788
print('###############################\nKör test: 20 - Webbkoll')
88-
testsites(sites, test_type=WEBBKOLL)
89+
tests['webbkoll'] = testsites(sites, test_type=WEBBKOLL)
90+
return tests
8991

9092
def validate_test_type(test_type):
9193
if test_type != HTML and test_type != PAGE_NOT_FOUND and test_type != CSS and test_type != WEBBKOLL and test_type != GOOGLE_PAGESPEED:
@@ -114,8 +116,9 @@ def main(argv):
114116
-o/--output <file path>\t: output file path (JSON)
115117
"""
116118

117-
test_type = -1
119+
test_type = ALL_TESTS
118120
sites = list()
121+
output_filename = ''
119122

120123
try:
121124
opts, args = getopt.getopt(argv,"hu:t:i:o:",["help","url","test", "input", "output"])
@@ -149,15 +152,15 @@ def main(argv):
149152
sites.append([site["id"], site["url"]])
150153
pass
151154
elif opt in ("-o", "--output"): # output file path
152-
# TODO: make it possible to store result(s) in json file
155+
output_filename = arg
153156
pass
154157

155158

156159
if (len(sites)):
157-
if (test_type != -1):
158-
testing(sites, test_type=test_type)
159-
else:
160-
testing(sites)
160+
siteTests = testing(sites, test_type=test_type)
161+
if (len(output_filename) > 0):
162+
with open(output_filename, 'w') as outfile:
163+
json.dump(siteTests, outfile)
161164
else:
162165
print(main.__doc__)
163166

models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ def __init__(self, site_id, type_of_test, check_report, rating, test_date, json_
3333
self.test_date = test_date
3434
self.json_check_data = json_check_data
3535

36-
def tojson(self):
36+
def todata(self):
3737
result = {
3838
'id': self.site_id,
3939
'type_of_test': self.type_of_test,
4040
'rating': self.rating,
41-
'date': self.test_date.strftime('%Y-%m-%d %H:%M:%S'),
41+
'date': self.test_date.isoformat(),
4242
'report': self.check_report.decode('utf-8'),
4343
'data': self.json_check_data.decode('utf-8')
4444
}
45-
return json.dumps(result)
45+
return result
4646

4747
def __repr__(self):
4848
return '<SiteTest %r>' % self.test_date

0 commit comments

Comments
 (0)