# python2
# coding=utf-8

"""Retrieve and print words form a URL.

Usage:
    python2 urlopen.py <URL>
"""

import sys
from urllib import urlopen


def fetch_words(url):
    """Fetch a list of words from a URL.

    Args:
        url: The URL of a UTF-8 text doc.

    Returns:
        A list of strings containing the words from the doc.
    """
    story = urlopen(url)
    story_words = []
    for line in story:
        line_words = line.decode('utf-8').split()
        for word in line_words:
            story_words.append(word)
    return story_words


def print_items(items):
    """Print items one per line

    Args:
        An iterable series of printable items.
    """
    for item in items:
        print(item)


def main(url):
    """
    Print each word from a text doc from URL.

    Args:
        url: The url of a UTF-8 text doc.
    """
    words = fetch_words(url)
    print_items(words)
    help(urlopen)


if __name__ == "__main__":
    main(sys.argv[1])
