inkblock/main.py
def promote():
    """Schedule all the social media posts."""

    if "BUFFER_ACCESS_TOKEN" not in os.environ:
        warn("Missing BUFFER_ACCESS_TOKEN.")
        echo("To publish to social medial, you'll need an access token for buffer.")
        echo("The simplest way to get one is to create a new app here: https://buffer.com/developers/apps")
        echo("The token you want is the 'Access Token'")
        echo("Once you have it, make it available to ink by putting it in the environment.")

    # GET https://api.bufferapp.com/1/profiles.json
    echo("Verifying available profiles on buffer")
    profiles = buffer_get("/1/profiles.json")
    for p in profiles:
        supported_profile = False
        if p["formatted_service"].lower() == "facebook" or p["formatted_service"].lower() == "facebook page":
            facebook_profiles.append(p)
            supported_profile = True
        elif p["formatted_service"].lower() == "twitter":
            twitter_profiles.append(p)
            supported_profile = True

        if supported_profile:
            click.secho(u"✓  %s: %s" % (p["formatted_service"], p["formatted_username"]), fg="green")

    echo("Checking publication status...")
    site_json_filename = os.path.join(ROOT_DIR, BUILD_DIR, "static", "private.json")
    with open(site_json_filename, "r") as site_json:
        site = load(site_json)

    echo('Reviewing social posts...')

    posts = {}
    unpublished_posts = []

    for dirpath, dirnames, filenames in os.walk(os.path.join(ROOT_DIR, "posts"), topdown=False):
        for filename in filenames:
            if "piece.md" in filename:
                if exists(dirpath, "social.yml") and exists(dirpath, "meta.yml"):
                    with open(os.path.join(dirpath, "social.yml")) as f:
                        social = load(f)

                    with open(os.path.join(dirpath, "meta.yml")) as f:
                        meta = load(f)

                    if "url" in meta:
                        site_json_entry = None
                        for sp in site["posts"]:
                            if meta["url"] == sp["url"]:
                                site_json_entry = sp
                                break

                        posts[meta["url"]] = {
                            "meta": meta,
                            "social": social,
                            "dirpath": dirpath,
                            "site": site_json_entry,
                        }
                        if "published" not in social or social["published"] is not True:
                            unpublished_posts.append(meta["url"])
                    else:
                        warn("No url found for %s" % dirpath.replace(ROOT_DIR))

    automark_set = False
    automark = None
    for u in unpublished_posts:
        post = posts[u]

        if "posts" in post["social"] and post["social"]["posts"] and len(post["social"]["posts"]) > 0:
            facebook_posts = []
            twitter_posts = []
            mark_as_published = False

            has_valid_post = False
            for p in post["social"]["posts"]:
                try:
                    if len(p.keys()) != 1:
                        error("Something's formatted wrong in %s's social.yml" % u)
                        break
                    if p.keys()[0] == "facebook":
                        facebook_posts.append(p["facebook"])
                        if post_in_future(p["facebook"], post):
                            has_valid_post = True
                    elif p.keys()[0] == "twitter":
                        if post_in_future(p["twitter"], post):
                            has_valid_post = True
                        twitter_posts.append(p["twitter"])
                    else:
                        warn("Unknown post type: %s.  Skipping." % p.keys()[0])
                except:
                    error("Error parsing social.yml for \"%s\"" % post["meta"]["title"])
                    import traceback
                    traceback.print_exc()

            if not has_valid_post:
                if automark:
                    mark_as_published = True
                else:
                    warn('"%s" hasn\'t been published, but all posts are in the past.' % post["meta"]["title"])
                    if click.confirm("Mark as published?"):
                        mark_as_published = True
                        if not automark_set:
                            if click.confirm("Mark all other similar posts as published?"):
                                automark = True
                            automark_set = True
            else:
                echo('\n"%s" hasn\'t been published to social media.' % post["meta"]["title"])
                if len(facebook_posts) > 0:
                    echo("  Facebook:")
                    for p in facebook_posts:
                        if (len(p["content"]) > 40):
                            truncated_content = "%s..." % p["content"][:40]
                        else:
                            truncated_content = p["content"]
                        if post_in_future(p, post):
                            echo("   - %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                        else:
                            warn("   - %s:  \"%s\" skipping (past)" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                echo("  Twitter:")
                if len(twitter_posts) > 0:
                    for p in twitter_posts:
                        if (len(p["content"]) > 40):
                            truncated_content = "%s..." % p["content"][:40]
                        else:
                            truncated_content = p["content"]
                        if post_in_future(p, post):
                            echo("   - %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                        else:
                            warn("   - %s:  \"%s\" skipping (past)" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))

                if click.confirm(click.style("  Publish now?", fg="green")):
                    mark_as_published = True
                    echo("  Publishing...")
                    for p in facebook_posts:
                        if post_in_future(p, post):
                            publish_facebook(p, post)
                            if (len(p["content"]) > 40):
                                truncated_content = "%s..." % p["content"][:40]
                            else:
                                truncated_content = p["content"]
                            click.secho(u"   ✓ Facebook %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ), fg="green")
                    for p in twitter_posts:
                        if post_in_future(p, post):
                            publish_twitter(p, post)
                            if (len(p["content"]) > 40):
                                truncated_content = "%s..." % p["content"][:40]
                            else:
                                truncated_content = p["content"]
                            click.secho(u"   ✓ Twitter %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ), fg="green")

                    echo("  Published.")

            # Save as published.
            if mark_as_published or automark:
                post["social"]["published"] = True
                with open(os.path.join(post["dirpath"], "social.yml"), "w") as f:
                    dump(post["social"], f, default_flow_style=False, width=1000)

    if click.confirm("Publish your entire backlog to buffer?"):
        print ("dope")
def promote():
    """Schedule all the social media posts."""

    if "BUFFER_ACCESS_TOKEN" not in os.environ:
        warn("Missing BUFFER_ACCESS_TOKEN.")
        echo("To publish to social medial, you'll need an access token for buffer.")
        echo("The simplest way to get one is to create a new app here: https://buffer.com/developers/apps")
        echo("The token you want is the 'Access Token'")
        echo("Once you have it, make it available to ink by putting it in the environment.")

    # GET https://api.bufferapp.com/1/profiles.json
    echo("Verifying available profiles on buffer")
    profiles = buffer_get("/1/profiles.json")
    for p in profiles:
        supported_profile = False
        if p["formatted_service"].lower() == "facebook" or p["formatted_service"].lower() == "facebook page":
            facebook_profiles.append(p)
            supported_profile = True
        elif p["formatted_service"].lower() == "twitter":
            twitter_profiles.append(p)
            supported_profile = True

        if supported_profile:
            click.secho(u"✓  %s: %s" % (p["formatted_service"], p["formatted_username"]), fg="green")

    echo("Checking publication status...")
    site_json_filename = os.path.join(ROOT_DIR, BUILD_DIR, "static", "private.json")
    with open(site_json_filename, "r") as site_json:
        site = load(site_json)

    echo('Reviewing social posts...')

    posts = {}
    unpublished_posts = []

    for dirpath, dirnames, filenames in os.walk(os.path.join(ROOT_DIR, "posts"), topdown=False):
        for filename in filenames:
            if "piece.md" in filename:
                if exists(dirpath, "social.yml") and exists(dirpath, "meta.yml"):
                    with open(os.path.join(dirpath, "social.yml")) as f:
                        social = load(f)

                    with open(os.path.join(dirpath, "meta.yml")) as f:
                        meta = load(f)

                    if "url" in meta:
                        site_json_entry = None
                        for sp in site["posts"]:
                            if meta["url"] == sp["url"]:
                                site_json_entry = sp
                                break

                        posts[meta["url"]] = {
                            "meta": meta,
                            "social": social,
                            "dirpath": dirpath,
                            "site": site_json_entry,
                        }
                        if "published" not in social or social["published"] is not True:
                            unpublished_posts.append(meta["url"])
                    else:
                        warn("No url found for %s" % dirpath.replace(ROOT_DIR))

    automark_set = False
    automark = None
    for u in unpublished_posts:
        post = posts[u]

        if "posts" in post["social"] and post["social"]["posts"] and len(post["social"]["posts"]) > 0:
            facebook_posts = []
            twitter_posts = []
            mark_as_published = False

            has_valid_post = False
            for p in post["social"]["posts"]:
                try:
                    if len(p.keys()) != 1:
                        error("Something's formatted wrong in %s's social.yml" % u)
                        break
                    if p.keys()[0] == "facebook":
                        facebook_posts.append(p["facebook"])
                        if post_in_future(p["facebook"], post):
                            has_valid_post = True
                    elif p.keys()[0] == "twitter":
                        if post_in_future(p["twitter"], post):
                            has_valid_post = True
                        twitter_posts.append(p["twitter"])
                    else:
                        warn("Unknown post type: %s.  Skipping." % p.keys()[0])
                except:
                    error("Error parsing social.yml for \"%s\"" % post["meta"]["title"])
                    import traceback
                    traceback.print_exc()

            if not has_valid_post:
                if automark:
                    mark_as_published = True
                else:
                    warn('"%s" hasn\'t been published, but all posts are in the past.' % post["meta"]["title"])
                    if click.confirm("Mark as published?"):
                        mark_as_published = True
                        if not automark_set:
                            if click.confirm("Mark all other similar posts as published?"):
                                automark = True
                            automark_set = True
            else:
                echo('\n"%s" hasn\'t been published to social media.' % post["meta"]["title"])
                if len(facebook_posts) > 0:
                    echo("  Facebook:")
                    for p in facebook_posts:
                        if (len(p["content"]) > 40):
                            truncated_content = "%s..." % p["content"][:40]
                        else:
                            truncated_content = p["content"]
                        if post_in_future(p, post):
                            echo("   - %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                        else:
                            warn("   - %s:  \"%s\" skipping (past)" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                echo("  Twitter:")
                if len(twitter_posts) > 0:
                    for p in twitter_posts:
                        if (len(p["content"]) > 40):
                            truncated_content = "%s..." % p["content"][:40]
                        else:
                            truncated_content = p["content"]
                        if post_in_future(p, post):
                            echo("   - %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))
                        else:
                            warn("   - %s:  \"%s\" skipping (past)" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ))

                if click.confirm(click.style("  Publish now?", fg="green")):
                    mark_as_published = True
                    echo("  Publishing...")
                    for p in facebook_posts:
                        if post_in_future(p, post):
                            publish_facebook(p, post)
                            if (len(p["content"]) > 40):
                                truncated_content = "%s..." % p["content"][:40]
                            else:
                                truncated_content = p["content"]
                            click.secho(u"   ✓ Facebook %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ), fg="green")
                    for p in twitter_posts:
                        if post_in_future(p, post):
                            publish_twitter(p, post)
                            if (len(p["content"]) > 40):
                                truncated_content = "%s..." % p["content"][:40]
                            else:
                                truncated_content = p["content"]
                            click.secho(u"   ✓ Twitter %s:  \"%s\"" % (
                                publish_datetime(p, post).strftime("%c"),
                                truncated_content,
                            ), fg="green")

                    echo("  Published.")

            # Save as published.
            if mark_as_published or automark:
                post["social"]["published"] = True
                with open(os.path.join(post["dirpath"], "social.yml"), "w") as f:
                    dump(post["social"], f, default_flow_style=False, width=1000)

    if click.confirm("Publish your entire backlog to buffer?"):
        print ("dope")
def promote ( ) : if "BUFFER_ACCESS_TOKEN" not in os . environ : warn ( "Missing BUFFER_ACCESS_TOKEN." ) echo ( "To publish to social medial, you'll need an access token for buffer." ) echo ( "The simplest way to get one is to create a new app here: https://buffer.com/developers/apps" ) echo ( "The token you want is the 'Access Token'" ) echo ( "Once you have it, make it available to ink by putting it in the environment." ) # GET https://api.bufferapp.com/1/profiles.json echo ( "Verifying available profiles on buffer" ) profiles = buffer_get ( "/1/profiles.json" ) for p in profiles : supported_profile = False if p [ "formatted_service" ] . lower ( ) == "facebook" or p [ "formatted_service" ] . lower ( ) == "facebook page" : facebook_profiles . append ( p ) supported_profile = True elif p [ "formatted_service" ] . lower ( ) == "twitter" : twitter_profiles . append ( p ) supported_profile = True if supported_profile : click . secho ( u"✓  %s: %s" % ( [ " f ormatted_service"], p " f ormatted_username"]) , f =" g reen") echo ( "Checking publication status..." ) site_json_filename = os . path . join ( ROOT_DIR , BUILD_DIR , "static" , "private.json" ) with open ( site_json_filename , "r" ) as site_json : site = load ( site_json ) echo ( 'Reviewing social posts...' ) posts = { } unpublished_posts = [ ] for dirpath , dirnames , filenames in os . walk ( os . path . join ( ROOT_DIR , "posts" ) , topdown = False ) : for filename in filenames : if "piece.md" in filename : if exists ( dirpath , "social.yml" ) and exists ( dirpath , "meta.yml" ) : with open ( os . path . join ( dirpath , "social.yml" ) ) as f : social = load ( f ) with open ( os . path . join ( dirpath , "meta.yml" ) ) as f : meta = load ( f ) if "url" in meta : site_json_entry = None for sp in site [ "posts" ] : if meta [ "url" ] == sp [ "url" ] : site_json_entry = sp break posts [ meta [ "url" ] ] = { "meta" : meta , "social" : social , "dirpath" : dirpath , "site" : site_json_entry , } if "published" not in social or social [ "published" ] is not True : unpublished_posts . append ( meta [ "url" ] ) else : warn ( "No url found for %s" % dirpath . replace ( ROOT_DIR ) ) automark_set = False automark = None for u in unpublished_posts : post = posts [ u ] if "posts" in post [ "social" ] and post [ "social" ] [ "posts" ] and len ( post [ "social" ] [ "posts" ] ) > 0 : facebook_posts = [ ] twitter_posts = [ ] mark_as_published = False has_valid_post = False for p in post [ "social" ] [ "posts" ] : try : if len ( p . keys ( ) ) != 1 : error ( "Something's formatted wrong in %s's social.yml" % u ) break if p . keys ( ) [ 0 ] == "facebook" : facebook_posts . append ( p [ "facebook" ] ) if post_in_future ( p [ "facebook" ] , post ) : has_valid_post = True elif p . keys ( ) [ 0 ] == "twitter" : if post_in_future ( p [ "twitter" ] , post ) : has_valid_post = True twitter_posts . append ( p [ "twitter" ] ) else : warn ( "Unknown post type: %s.  Skipping." % p . keys ( ) [ 0 ] ) except : error ( "Error parsing social.yml for \"%s\"" % post [ "meta" ] [ "title" ] ) import traceback traceback . print_exc ( ) if not has_valid_post : if automark : mark_as_published = True else : warn ( '"%s" hasn\'t been published, but all posts are in the past.' % post [ "meta" ] [ "title" ] ) if click . confirm ( "Mark as published?" ) : mark_as_published = True if not automark_set : if click . confirm ( "Mark all other similar posts as published?" ) : automark = True automark_set = True else : echo ( '\n"%s" hasn\'t been published to social media.' % post [ "meta" ] [ "title" ] ) if len ( facebook_posts ) > 0 : echo ( "  Facebook:" ) for p in facebook_posts : if ( len ( p [ "content" ] ) > 40 ) : truncated_content = "%s..." % p [ "content" ] [ : 40 ] else : truncated_content = p [ "content" ] if post_in_future ( p , post ) : echo ( "   - %s:  \"%s\"" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) ) else : warn ( "   - %s:  \"%s\" skipping (past)" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) ) echo ( "  Twitter:" ) if len ( twitter_posts ) > 0 : for p in twitter_posts : if ( len ( p [ "content" ] ) > 40 ) : truncated_content = "%s..." % p [ "content" ] [ : 40 ] else : truncated_content = p [ "content" ] if post_in_future ( p , post ) : echo ( "   - %s:  \"%s\"" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) ) else : warn ( "   - %s:  \"%s\" skipping (past)" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) ) if click . confirm ( click . style ( "  Publish now?" , fg = "green" ) ) : mark_as_published = True echo ( "  Publishing..." ) for p in facebook_posts : if post_in_future ( p , post ) : publish_facebook ( p , post ) if ( len ( p [ "content" ] ) > 40 ) : truncated_content = "%s..." % p [ "content" ] [ : 40 ] else : truncated_content = p [ "content" ] click . secho ( u"   ✓ Facebook %s:  \"%s\"" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) , fg = "green" ) for p in twitter_posts : if post_in_future ( p , post ) : publish_twitter ( p , post ) if ( len ( p [ "content" ] ) > 40 ) : truncated_content = "%s..." % p [ "content" ] [ : 40 ] else : truncated_content = p [ "content" ] click . secho ( u"   ✓ Twitter %s:  \"%s\"" % ( publish_datetime ( p , post ) . strftime ( "%c" ) , truncated_content , ) , fg = "green" ) echo ( "  Published." ) # Save as published. if mark_as_published or automark : post [ "social" ] [ "published" ] = True with open ( os . path . join ( post [ "dirpath" ] , "social.yml" ) , "w" ) as f : dump ( post [ "social" ] , f , default_flow_style = False , width = 1000 ) if click . confirm ( "Publish your entire backlog to buffer?" ) : print ( "dope" )
Schedule all the social media posts.
Schedule all the social media posts .