예제 #1
0
def process_file(file_name, out_dir):
    """

    :param file_name: fully qualified name of the file to be processed
    :param out_dir: Output directory to put the post in.
    :return: None
    """
    code_parser = CodeParser()
    link = code_parser.get_page_link(file_name)
    if not link:
        print "I can't find any link in the given file"
        return
    # link = 'https://www.leetcode.com/problems/add-digits/'

    # initialize the post
    post = Post(link=link, file_name=file_name)

    # Object of Uniqueness checker
    verify_unique = VerifyUnique(config)
    # Check for post uniqueness for current file
    status = verify_unique.check_post_exists(post)
    if status is verify_unique.NON_UNIQUE:
        print 'post file already exists'
    elif status is verify_unique.NEW_CODE:
        # Old post file already exists.
        # Append the current code to existing post file.
        append_to_post(post.post_file, file_name)
        print "updated already existing post file for: " + file_name
    else:
        # Object to fetch the web page
        fetcher = Fetcher(logging.DEBUG)
        # Get the host and web page from the link
        try:
            host, response = fetcher.fetch(link.strip())
        except Exception as e:
            print "Exception while fetching for link: ", link
            print "Exception: ", e
            return

        # print host
        # print response

        # update the post with host_name and
        # raw_page response values
        post.host_name = host
        post.raw_page = response

        # Get the appropriate problem description
        try:
            problem_desc_tag = WebParser.parse_page(post)
        except Exception, e:
            print "Exception in parsing page: ", e
            problem_desc_tag = None

        # print 'title: ' + post.title
        # print problem_desc_tag
        if not problem_desc_tag:
            print "could not parse the file "
            return
        # update the post object with problem description
        post.problem_text = problem_desc_tag
        # Save the post file
        print "making the post"
        try:
            make_post(file_name, post, out_dir)
            verify_unique.serialize_post(post)
        except Exception, e:
            print "could not make the post file", e
            return