WordPress Book Posting Scripts

Module 6: full_process.py (Putting it all together)

Whew! That was a lot of work, but now it can all get put together. This is the script that pulls all those modules together in order to convert a document to html, split it up by chapter, and schedule all those chapters as posts (with appropriate tags!).

# In House Modules
import doc_to_html
import chop_sections
import wp_poster
import dategen
import clean_html

# Python Standard Library
import os

def full_process(source_document,
                 export_path,
                 # Parameters for poster
                 wp_url,
                 wp_username,
                 wp_password,
                 wp_blogid,
                 potential_tags,
                 tag_aliases,
                 # Parameters for dates.
                 year,
                 month,
                 day,
                 accepted_days,
                 # Category to post to
                 categories,
                 title_prefix=None):
    # Convert document to html.
    html_path = doc_to_html.convert_to_html(source_document)
    # Seperate document into individual posts.
    content_paths = chop_sections.parse_file(html_path, export_path)
    content_paths = [x for x in content_paths
                     if "chapter" in
                     os.path.basename(x)]
    # Create wp_poster
    poster = wp_poster.WP_Poster(
                                 wp_url,
                                 wp_username,
                                 wp_password,
                                 wp_blogid
                                 )
    if potential_tags:
        poster.set_potential_tags(potential_tags, tag_aliases)

    # Create date generator
    post_dates = dategen.sequenced_dates(
                        year, month, day,
                        accepted_days=accepted_days,)
    
    # Send posts to web.
    for content_path in content_paths:
        # title
        title = os.path.basename(content_path)
        title = os.path.splitext(title)[0]
        title = title.replace("_", " ")
        title = title.title()
        if title_prefix:
            title = "%s, %s" % (title_prefix, title)
        # content
        open_file = open(content_path, 'r')
        content = open_file.read()
        open_file.close()
        content = clean_html.scrub_html_string(content)
        # post_date
        post_date = post_dates.next()
        
                
        poster.post_item(title, content, post_date, categories,
                         status_published=1,
                         tags=[])
    pass

if __name__ == '__main__':
    def main():
        # source_document
        doc_folder = r"C:\PathToYourDocumentFolder"
        doc = "NameOfYourDocument.docx"
        source_document = os.path.join(doc_folder, doc)
        export_path = os.path.join(os.getcwd(), r'htmls')

        wp_url = "http://YourWordPressWebsite/xmlrpc.php"
        wp_username = "YourUsername"
        wp_password = "YourPassword"
        # These are the names I want the script to scan for and use as tags.
        potential_tags = ["Abby", "Megan", "Fumiko", "Hans",
                          "Katherine", "Emma", "Sarah", "Jimmy", "Carl",
                          "Zane", "Butterson", "Salvatore",
                          "Tophat", "Hitomi", "Miaka", "Hilda", "Tanaka",
                          "George", "Bob", "Mark","Eyelids"
                          ]
        # These are some alternative names that the script should convert to
        # specific tags if they are found.
        tag_aliases = {"ABIGAIL": "Abby",
                       "I": "Abby"}
        year = 2014
        month = 3
        day = 17
        accepted_days = dategen.TWO_UPDATES
        categories = ["Book 1-New Year's Night"]
        full_process(source_document=source_document,
                     export_path=export_path,
                     # Parameters for poster
                     wp_url=wp_url,
                     wp_username=wp_username,
                     wp_password=wp_password,
                     wp_blogid="",
                     potential_tags=potential_tags,
                     tag_aliases=tag_aliases,
                     # Parameters for dates.
                     year=year,
                     month=month,
                     day=day,
                     accepted_days=accepted_days,
                     # Category to post to
                     categories=categories,
                     title_prefix = "Book 1",
                     )
    main()
    print "DONE"

There you go: all of the scripts necessary to turn a word document into a series of scheduled WordPress posts. Not too shabby for a couple of hours on the weekend, huh?

My next goal is going to be turn these scripts into a nice little app with a GUI. I think it would be pretty cool if you could store multiple blogs/usernames/passwords, and had a file dialog for selecting your document, rather than having to adjust the parameters that are hard coded in the main() function.

But that’s a project for another day.

Utility Scripts , ,

Leave a Reply

Your email address will not be published. Required fields are marked *