diff --git a/tildetown-py/__init__.py b/tildetown-py/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cgi/app.py b/tildetown-py/cgi/app.py similarity index 100% rename from cgi/app.py rename to tildetown-py/cgi/app.py diff --git a/cgi/templates/guestbook.html b/tildetown-py/cgi/templates/guestbook.html similarity index 100% rename from cgi/templates/guestbook.html rename to tildetown-py/cgi/templates/guestbook.html diff --git a/tildetown-py/stats.py b/tildetown-py/stats.py new file mode 100644 index 0000000..55ee304 --- /dev/null +++ b/tildetown-py/stats.py @@ -0,0 +1,75 @@ +import json +from functools import partial +from os import listdir +from os.path import getmtime, join +from datetime import datetime +from sh import find, facter +from .util import slurp, thread + +# this script emits json on standard out that has information about tilde.town +# users. It denotes who has not updated their page from the default. It also +# reports the time this script was run. The user list is sorted by public_html update time. + + +DEFAULT_HTML = slurp("/etc/skel/public_html/index.html") + +username_to_html_path = lambda u: "/home/{}/public_html".format(u) + +def default_p(username): + return DEFAULT_HTML == slurp(join(username_to_html_path(username), "index.html")) + +def bounded_find(path): + # find might return 1 but still have worked fine. + return find(path, "-maxdepth" "3", _ok_code=[0,1]) + +def guarded_mtime(path): + try: + return getmtime(path.rstrip()) + except Exception, e: + return 0 + +def modify_time(username): + files_to_mtimes = partial(map, guarded_mtime) + return thread(username, + username_to_html_path, + bounded_find, + files_to_mtimes, + list, + max) + +def sort_user_list(usernames): + return sorted(usernames, key=modify_time) + +def user_generator(): + ignore_system_users = lambda f: f != "ubuntu" and f != "poetry" + return filter(ignore_system_users, listdir("/home")) + +def get_user_data(): + username_to_data = lambda u: {'username': u, + 'default': default_p(u), + 'favicon':'TODO'} + live_p = lambda user: not user['default'] + + all_users = thread(user_generator(), + sort_user_list, + reversed, + partial(map, username_to_data), + list) + + live_users = list(filter(live_p, all_users)) + + active_user_count = int(facter("active_user_count").stdout.strip()) + + return {'all_users': all_users, + 'num_users': len(all_users), + 'num_live_users': len(live_users), + 'active_user_count': active_user_count, + 'live_users': live_users,} + +def get_data(): + user_data = get_user_data() + data = {'generated_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')} + return data.update(user_data) + +if __name__ == '__main__': + print(json.dumps(get_data())) diff --git a/tildetown-py/util.py b/tildetown-py/util.py new file mode 100644 index 0000000..4657510 --- /dev/null +++ b/tildetown-py/util.py @@ -0,0 +1,11 @@ +def slurp(file_path): + contents = None + with open(file_path, 'r') as f: + contents = f.read() + return contents + +def thread(initial, *fns): + value = initial + for fn in fns: + value = fn(value) + return value