-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (51 loc) · 2.87 KB
/
main.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
import json
import requests
from datetime import datetime
# the json file I'm reading from
url = 'https://miau.sadgrl.online/yesterweb-ring/webring.json'
# getting the status code of the website
data = requests.get(url)
# loading the json data
yesterwebRing = json.loads(data.text);
# this opens the HTML file which is generated at the end
#f = open('yesterCheck.html', 'w')
f = open('/var/www/ring-status.yesterweb.org/html/index.html', 'w')
# this opens the log file which is generated at the end
#log = open('log.txt', "a")
log = open('/var/www/ring-status.yesterweb.org/html/log.txt', "a")
# get current time
now = datetime.now()
# this writes the html stuff that comes before the loop
f.write('<!DOCTYPE html><head><link rel="stylesheet" href="style.css"></head><body><div class="container"><h1>Yesterweb Webring Status</h1><table><colgroup><col span="1" style="50%"><col span="1" style="50%"></colgroup>')
# this function validates the URL and returns an error message associated with the status returned
def _validate_url(url):
try:
r = requests.head(url, timeout=10.0)
# for 404s
if r.status_code == 404:
f.write ('<tr><td><a href="' + url + '" target="blank">' + url + '</a></td>' + '<td class="fourohfour">' + ' 404 NOT FOUND' + '</td></tr>')
# writes current date/time and error to log
log.write(now.strftime("%d/%m/%Y %H:%M:%S") + ' : ERROR: 404 NOT FOUND ' + url + "\n")
# for 200 (success)
if r.status_code == 200:
f.write ('<tr class="success"><td><a href="' + url + ' target="blank">' + url + '</a></td>' + '<td>' + ' 200 SUCCESS' + '</td></tr>')
# if the request times out
except requests.Timeout as err:
f.write ('<tr><td><a href="' + url + '" target="blank">' + url + '</a></td>' + '<td class="timedout">' + 'TIMED OUT' + '</td></tr>')
log.write(now.strftime("%d/%m/%Y %H:%M:%S") + ' : ERROR: URL TIMED OUT ' + url + "\n")
# this seems to cover a lot of different errors
except requests.RequestException as err:
f.write ('<tr><td><a href="' + url + '" target="blank">' + url + '</a></td>' + '<td class="error">' + 'ERROR' + '</td></tr>')
log.write(now.strftime("%d/%m/%Y %H:%M:%S") + ' : ERROR: UNKNOWN ' + url + "\n")
# needs more info
# loops through each line in the JSON file
for s in range(len(yesterwebRing)):
# grab the url values
eachURL = format(yesterwebRing[s]["url"])
# runs the validate function
_validate_url(eachURL)
# end of page html
f.write('</table></div></body></html>')
# close log files
f.close()
log.close()