def run(self, args): p = args.proxy if p is None: p = os.environ.get('http_proxy') if p is not None: printDebug("DEBUG", "Using http_proxy evvironment variable") if 'http://' in p: p = p.replace('http://', '') elif 'https://' in p: p = p.replace('https://', '') else: printDebug("DEBUG", "Not using proxy") self.wp = Client(self.blog, args.user, args.password) else: p = p.replace('https://', '') try: self.wp = Client(self.blog, args.user, args.password, proxy=p) except Exception as e: printDebug("ERROR", "Failed to connect to %s" % self.blog) raise e # Send a file to wordpress. if args.update : fileName = args.update if not os.path.exists(fileName): raise IOError, "File %s does not exists" % fileName fmt, txt = formatter.readInputFile(fileName) self.format = fmt post = WordPressPost() assert post is not None self.updatePost(post, txt) elif args.new : self.newPostToWordpress(args.new) # Fetch blogs from wordpress. elif args.fetch : # Get all posts self.fetchWpPosts(args.fetch) else : # get recent posts printDebug("STEP", "Getting recent posts") posts = self.wp.call(GetPosts( {'post_status': 'publish'})) self.writePosts(posts)
def newPostToWordpress(self, postName): printDebug("STEP", "You are going to create a new post!") post = WordPressPost() post.id = self.wp.call(NewPost(post)) ## get the text of new post fileName = postName fmt, txt = formatter.readInputFile(fileName) self.format = fmt try: self.updatePost(post, txt) except Exception as e: printDebug("WARN", "Failed to send post to wordpress") printDebug("DEBUG", "Error was {0}".format(e)) return printDebug("INFO", "Post sent successfully") # Now download the sent post and save it. postNew = self.wp.call(GetPost(post.id)) self.writePosts([postNew]) printDebug("ADVICE", "You should now delete : {0}.".format(postName)) return 0