-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save pages version + refacto templating for future use to diff page (…
…wip #18)
- Loading branch information
Showing
8 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import os, codecs | ||
from pystache import Renderer | ||
from contextlib import nested | ||
from gazouilleur.lib.log import loggerr | ||
try: | ||
from gazouilleur.config import URL_STATS | ||
except: | ||
URL_STATS = None | ||
|
||
class Templater(object): | ||
|
||
def __init__(self): | ||
self.url = '%s/' % URL_STATS.rstrip('/') if URL_STATS else None | ||
self.templates = os.path.join("web", "templates") | ||
|
||
def render_template(self, template, name, data): | ||
outfile = template.replace('.html', '_%s.html' % name) | ||
try: | ||
ofile = os.path.join("web", outfile) | ||
with nested(open(os.path.join(self.templates, template), "r"), codecs.open(ofile, "w", encoding="utf-8")) as (temp, generated): | ||
generated.write(Renderer(string_encoding='utf8').render(temp.read(), data)) | ||
os.chmod(ofile, 0o644) | ||
return True | ||
except IOError as e: | ||
loggerr("Could not write web/%s from %s/%s : %s" % (outfile, self.templates, template, e), action="stats") | ||
return False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import os, time | ||
from gazouilleur.lib.templater import Templater | ||
|
||
class WebMonitor(Templater): | ||
|
||
def __init__(self, name): | ||
Templater.__init__(self) | ||
self.name = name | ||
self.path = os.path.join('web', 'monitor', name) | ||
if not os.path.exists(self.path): | ||
os.makedirs(self.path) | ||
|
||
def check_diff(self, url, data): | ||
# TODO: | ||
# apply url_rewrite for '<[^>]*=['"]/([^/]|$)' et '<[^>]*=['"](!:http)' | ||
# - check if file -last exists | ||
# - if so diff md5 current/last | ||
# - check if exist and not diff | ||
if False: | ||
return None | ||
for name in ["last", time.strftime("%y%m%d-%H%M")]: | ||
fil = os.path.join(self.path, "%s.html" % name) | ||
with open(fil, "w") as f: | ||
f.write(data) | ||
os.chmod(fil, 0o644) | ||
msg = "Looks like the webpage %s at %s just changed!" % (self.name, url) | ||
if self.url: | ||
self.build_diff_page(url) | ||
msg += "\nYou can check the different versions and diffs at %smonitor_%s.html" % (self.url, self.name) | ||
return msg | ||
|
||
def build_diff_page(self, url): | ||
data = { | ||
"name": self.name, | ||
"url": url, | ||
} | ||
data["versions"] = sorted(os.listdir(os.path.join('web', 'monitor', self.name)), reverse=True) | ||
self.render_template("monitor.html", self.name, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<html> | ||
<head> | ||
<meta content="text/html; charset=utf-8" http-equiv="content-type"/> | ||
<title>WebMonitor by gazouilleur for {{name}}</title> | ||
<!-- TODO: stylify everything--> | ||
<style> | ||
</style> | ||
</head> | ||
<body> | ||
<h1>WebMonitor by gazouilleur for <a target="_blank" href="{{url}}">{{name}} at {{url}}</a></h1> | ||
<h2>Available versions</h2> | ||
<ul> | ||
<li><b><a target="_blank" href="{{url}}">Actual</a></b></li> | ||
{{#versions}} | ||
<li><b><a target="_blank" href="monitor/{{name}}/{{.}}">Saved {{.}}</a></b></li> | ||
{{/versions}} | ||
</ul> | ||
<h2 id="version">Actual version</h2> | ||
<iframe id="page" src="{{url}}"></iframe> | ||
<h2>Diff view</h2> | ||
<span id="old"></span> Vs. <span id="new"></span> | ||
<div id="diff"></div> | ||
</body> | ||
<script type="text/javascript"> | ||
// TODO: load page in iframe on click + set name in version | ||
// TODO: code differ | ||
// TODO: juggle between view and diff modes | ||
</script> | ||
</html> |