diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0634e13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.pyc +*.egg +*.egg-info +.DS_Store +__pycache__ + +.earwigbot +*.min.js +*.min.css +logs/* +!logs/.gitinclude diff --git a/app.py b/app.py index db8c411..0eee91c 100644 --- a/app.py +++ b/app.py @@ -1,28 +1,22 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- -from flask import Flask, g +from flask import Flask from flask.ext.mako import MakoTemplates, render_template +from tif.calc import calculate_tif from tif.util import catch_errors, set_up_hash_caching app = Flask(__name__) MakoTemplates(app) set_up_hash_caching(app) -@app.before_request -def prepare_request(): - g._db = None - -@app.teardown_appcontext -def close_databases(error): - if g._db: - g._db.close() - @app.route("/") @catch_errors(app) def index(): - return render_template("index.mako") + title = request.args.get("title") + result = calculate_tif(title) if title else None + return render_template("index.mako", result=result) if __name__ == '__main__': app.run() diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..f745d12 --- /dev/null +++ b/static/style.css @@ -0,0 +1,13 @@ +header {} + +footer {} + +#container {} + +#error {} + +#result {} + +#result-page {} + +#result-cache {} diff --git a/templates/error.mako b/templates/error.mako index e69de29..a2df007 100644 --- a/templates/error.mako +++ b/templates/error.mako @@ -0,0 +1,7 @@ +<%include file="/support/header.mako" args="title='Error - TIF Calculator'"/> +

Error!

+

An error occurred. If it hasn't been reported (try to check), please file an issue or email me. Include the following information:

+
+
${traceback | trim,h}
+
+<%include file="/support/footer.mako"/> diff --git a/templates/index.mako b/templates/index.mako index e69de29..b8bb765 100644 --- a/templates/index.mako +++ b/templates/index.mako @@ -0,0 +1,43 @@ +<%include file="/support/header.mako" args="title='TIF Calculator'"/> +
+ % if result["title"]: + + % else: + + % endif + +
+ +% if "error" in result: +
+ % if result["error"] == "no page": +

Can't find the given page: ${result["page"].title | h}.

+ % else + An unknown error occurred. + % endif +
+% endif + +% if "tif" in result: +
+
${result["page"].title | h}
+ + + + + + + + + + + + + +
TIF${result["tif"]}
Transclusions${result["transclusions"]}
Protection${result["protection"]}
+ % if "cache" in result and result["cache"]: +
Pageview data is cached from up to ${result["cache_age"]} ago.
+ % endif +
+% endif +<%include file="/support/footer.mako"/> diff --git a/templates/support/footer.mako b/templates/support/footer.mako new file mode 100644 index 0000000..76e80ea --- /dev/null +++ b/templates/support/footer.mako @@ -0,0 +1,10 @@ +<%! from datetime import datetime %>\ +<% this_year = datetime.now().year %>\ + + + + diff --git a/templates/support/header.mako b/templates/support/header.mako new file mode 100644 index 0000000..b123556 --- /dev/null +++ b/templates/support/header.mako @@ -0,0 +1,15 @@ +<%page args="title"/>\ +<%! from flask import request, url_for %>\ + + + + + ${title} + + + +
+

TIF Calculator

+

Calculate the template influence factor of any page on the English Wikipedia

+
+
diff --git a/tif/calc.py b/tif/calc.py new file mode 100644 index 0000000..f23d691 --- /dev/null +++ b/tif/calc.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- + +from datetime import datetime +from os.path import expanduser + +from earwigbot.bot import Bot +from oursql import connect + +__all__ = ["calculate_tif"] + +def _get_db(bot): + args = bot.config.wiki["_tifSQL"] + args["read_default_file"] = expanduser("~/.my.cnf") + args["autoping"] = True + args["autoreconnect"] = True + return connect(**args) + +def _get_transclusions(page): + # TODO + yield page + +def _get_view_average(page, db, cache_info): + # TODO + return 0.0 + +def _format_time(cache_time): + formatter = lambda n, w: "{0} {1}{2}".format(n, w, "" if n == 1 else "s") + diff = datetime.utcnow() - cache_time + if diff.seconds > 3600: + return formatter(diff.seconds / 3600, "hour") + if diff.seconds > 60: + return formatter(diff.seconds / 60, "minute") + return formatter(diff.seconds, "second") + +def calculate_tif(title): + bot = Bot(".earwigbot") + db = _get_db(bot) + site = bot.wiki.get_site() + template = site.get_page(title) + result = {"title": title, "page": template} + + if template.exists != template.PAGE_EXISTS: + result["error"] = "no page" + return result + + tif = 0.0 + transclusions = 0 + cache_info = {"cache": False, "cache_time_raw": None} + for page in _get_transclusions(template): + tif += _get_view_average(page, db, cache_info) + transclusions += 1 + + if cache_info["cache"]: + ctime = cache_info["cache_time"] + cache_info["cache_time"] = ctime.strftime("%b %d, %Y %H:%M:%S UTC") + cache_info["cache_ago"] = _format_time(ctime) + + result["tif"] = tif + result["transclusions"] = transclusions + result["protection"] = template.protection + result.update(cache_info) + return result