diff --git a/microblog.py b/microblog.py index 25e1aa0..a48e56e 100644 --- a/microblog.py +++ b/microblog.py @@ -1,6 +1,7 @@ import sys, os, traceback import dateutil.parser +from time import strftime, localtime # returns html-formatted string def make_buttons(btn_dict, msg_id): @@ -52,6 +53,8 @@ def make_gallery(indices, w, conf=None): tag.append("") return tag +# apply basic HTML formatting - only div class here is gallery +from html import escape def markup(message, config): def is_image(s, image_formats): l = s.rsplit('.', maxsplit=1) @@ -116,8 +119,6 @@ def markup(message, config): gallery = [] return sep.join(output), tags -# apply basic HTML formatting - only div class here is gallery -from html import escape class Post: def __init__(self, ts, msg): self.timestamp = ts.strip() # string @@ -321,6 +322,11 @@ if __name__ == "__main__": demote_css(html, config["relative_css"], lvl), tcloud, timeline ) + elif subdir == "placeholder": + lvl = 1 + tcloud = make_tagcloud(tagcloud, "./tags/%s/latest.html") + with open ("follows.html", 'w') as f: + print(pagectrl.singlepage(html, tcloud, timeline),file=f) else: # if timelines per tag is_tagline = True lvl = 2 @@ -351,14 +357,15 @@ if __name__ == "__main__": def export_profile(post_count, last_update, config): if "profile" not in config: return - if "alias" not in config["profile"] \ + if "username" not in config["profile"] \ or "url" not in config["profile"]: print("Warning: no profile exported", file=sys.stderr) return profile = { - "username" : config["profile"]["alias"], + "username" : config["profile"]["username"], "url": config["profile"]["url"], "last-updated": last_update, + "short-bio" : config["profile"]["short_bio"], "post-count" : post_count } if "avatar" in config["profile"]: @@ -367,13 +374,49 @@ if __name__ == "__main__": print(json.dumps(profile), file=f) import urllib.request as http - def fetch(follow_list): - objects = [] - for eceleb in follow_list: - with http.urlopen(eceleb) as response: - data = response.read() - objects.append(json.loads(data)) - return objects + def fn1(config): # come up with better name later + def fetch(follow_list): + oither_people = [] + for someone in follow_list: + try: + with http.urlopen(someone) as response: + data = response.read() + objects.append(json.loads(data)) + except urllib.error.HTTPError: + print("http error - skipping", file=sys.stderr) + pass + return other_people + list_of_json_objs = [] + with open("meta.json",'r') as f: + list_of_json_objs.append(json.loads(f.read())) + # list_of_json_obj = fetch(config["profile"]["following"]) + rendered = [] + template = config["format"] + SHORT_BIO_LIMIT = 150 + for profile in list_of_json_objs: + epoch_timestamp = profile["last-updated"] + if not isinstance(epoch_timestamp, int): + epoch_timestamp = 0 + + post_count = profile["post-count"] + if not isinstance(post_count, int): + post_count = 0 + + self_desc = profile["short-bio"] + if len(profile["short-bio"]) >= SHORT_BIO_LIMIT: + self_desc = profile["short-bio"][:SHORT_BIO_LIMIT] + "..." + + rendered.append(template.format( + __avatar__=escape(profile["avatar"]), + __handle__=escape(profile["username"]), + __url__=escape(profile["url"]), + __post_count__ = post_count, + __shortbio__= escape(self_desc), + __lastupdated__= strftime( + "%Y %b %d", localtime(epoch_timestamp)) + )) + # print(html, file=sys.stderr) + return rendered def main(): tpl, content = get_args() @@ -388,11 +431,6 @@ if __name__ == "__main__": print("exit: table 'page' absent in settings.toml", file=sys.stderr) return p = parse_txt(content) - if "syndication" in cfg: - if cfg["syndication"]["enabled"] == True: - export_profile( - len(p), p[0].get_epoch_time(), cfg["syndication"] - ) tl, tc, tg = get_posts(p, cfg["post"]) if tl == []: return @@ -412,6 +450,14 @@ if __name__ == "__main__": tpl, tagline, tc, cfg["page"], \ subdir="tags/%s" % key[1:] \ ) + if "syndication" in cfg: + if cfg["syndication"]["enabled"] == True: + export_profile( + len(p), p[0].get_epoch_time(), cfg["syndication"] ) + if "following" in cfg["syndication"]: + fellows = fn1(cfg["syndication"]["following"] ) + updated += writepage( + tpl, fellows, tc, cfg["page"], subdir="placeholder" ) with open("updatedfiles.txt", 'w') as f: for filename in updated: print(filename, file=f) # sys.stderr) @@ -422,8 +468,11 @@ if __name__ == "__main__": except KeyError as e: traceback.print_exc() print("\n\tA key may be missing from your settings file.", file=sys.stderr) - except dateutil.parser._parser.ParserError as e: + except dateutil.parser._parser.ParserError: traceback.print_exc() print("\n\tFailed to interpret a date from string..", "\n\tYour file of posts may be malformed.", "\n\tCheck if your file starts with a line break.", file=sys.stderr) + except toml.decoder.TomlDecodeError: + traceback.print_exc() + print("\n\tYour configuration file is malformed.")