#!/usr/bin/env python

import commands 
import xmlrpclib
import datetime
from ConfigParser import SafeConfigParser
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc import WordPressPost

#opens the file I want to read
f = open('filelist','r')
#reads the file and makes it callable
newlist = f.readlines()
#makes our searchterm callable, rewrites the in-ram list to be without it, and strips out the whitespace.
searchterm = newlist.pop(0).strip()
#closes the file, frees up memory. the newlist (sans searchterm) is now in memory.
f.close()

#Defines the function I want to use.
cmd = 'man %s' % (searchterm)
#This line makes it a callable.
s = commands.getoutput(cmd)

#Grabs the authorization info for publishing. For security, this is stored in a sepatate file and hidden from git.  These lines grab that information and parses it.
parser = SafeConfigParser()
parser.read('authorization')
url = parser.get('authorization_info','url' )
username = parser.get('authorization_info', 'username')
password = parser.get('authorization_info', 'password')
client = Client(url, username, password)

#post/post. -- imported definitions from wordpresspost, they tell the client where to plug info in.  Other terms (searchterm, s) plugins of functions already defined. 'publish' tells the client to push it to publish and not just draft, last line pulls client info (xmlrpc, username, password) and uses it author the post through wordpress.  
post = WordPressPost()
post.title = searchterm
post.content = s
post.terms_names = {'category' : ['Manual Page a Day']}
post.post_status = 'publish'
client.call(NewPost(post))


#opens the file with write capabilities.
#rewrites our permanent save file with new list(sans searchterm we just used)
#closes the file.
f = open('filelist', 'w')
f.writelines(newlist)
f.close() 

#Congrats, we're done.  
