# $Id$ # repost pyfaq comment as faq entry # usage: # # 1) locate a comment that's ready to be promoted to a FAQ entry # # 2) click on the # sign for that comment, to go to a separate page for # that comment # # 3) run # python faqpost.py user:pass category comment-url # where user is your username (you must have edit rights), pass is # your password, category is the initial category for this item (use # 'none' if not known), and comment-url is the url for the comment # page # # 4) follow the instructions; most importantly, make sure the generated # page name and the page contents make sense. if necessary, edit the # comment and try again. # # 5) when it looks good, press 'y' and return to generate/update the # page. # # 6) when done, the program prints a short markdown snippet that you # can use to replace the comment body. import sys, re, time import infogamibot import urlparse, urllib2 import difflib ET = infogamibot.ET # categories that don't need a prefix MASTER_CATEGORIES = [ 'general', 'programming', 'library', 'extending', 'windows', 'gui', ] try: site = sys.argv[1] if "@" not in site: site = site + "@pyfaq" category = sys.argv[2] comment_uri = urlparse.urlsplit(sys.argv[3])[2] except IndexError: print "usage: faqpost.py " sys.exit(1) bot = infogamibot.Bot(site, cache=1) comment = bot.get_source(comment_uri) title, body = comment.body.split("\n", 1) body = body.strip() if category not in "none": body = body + "\n\n" + "CATEGORY: " + category if title[:2] != "##": print "===", "title must start with ## (got %r)" % title sys.exit(1) title = title[2:].strip() # generate slug slug = re.sub("[^a-z0-9]+", "-", title.lower().replace("'", "")).strip("-") if category != "none" and category not in MASTER_CATEGORIES: slug = category + "-" + slug page = "/" + slug # check if the page exists result = bot.get_source("/" + slug) print "-"*68 print title print "-"*68 # diff against old version old_body = (result.body or "").strip().split("\n") new_body = body.strip().split("\n") if old_body == new_body: print "---", "already posted to", slug else: for line in difflib.unified_diff(old_body, new_body, "Current", "Comment"): print line print action = raw_input("store as %r ? y/[n] " % slug) if action == "k": # FIXME: hack to keep existing text but still update the comment body = result.body elif action != "y": sys.exit(1) result = bot.update(page, title, body) if result is not None: print "article ok!" comment.body = "## %s\n\nPosted to [%s](%s)" % (title, slug, page) result = bot.update(comment_uri, comment.title, comment.body) if result is not None: print "comment ok!"