import feedparser
import pandas as pd

RSS_URL = "http://dvd.netflix.com/Top100RSS"

feed = feedparser.parse(RSS_URL)

type(feed)

"""
Explanation: RSS
You can find RSS feeds on many different sites. Library of Congress has a lot. Most blogs and news web sites have them, for example Tech Crunch, New York Times, and NPR. The DC Public Library even gives you an RSS feed of your catalog searches.
Today we are going to take a look at the Netflix Top 100 DVDs. We will use the Python package FeedParser to work with the RSS feed. FeedParser will allow us to deconstruct the data in the feed.
End of explanation
"""


feed.keys()

"""
Explanation: "parse" is the primary function in FeedParser. The returned object is dictionary like and can be handled similarly. For example, we can look at the keys it contains.
End of explanation
"""


feed.version

"""
Explanation: We will look at some, but not all, of the data stored in the feed. For more information about the keys, see the documentation.
We can use the version to check which type of feed we have.
End of explanation
"""


if feed.bozo == 0:
    print("Well done, you have a well-formed feed!")
else:
    print("Potential trouble ahead.")

"""
Explanation: Bozo is an interesing key to know about if you are going to operationalize RSS feed ingestion. FeedParser sets the bozo bit when it detects a feed is not well-formed. (FeedParser will still parse the feed if it is not well-formed.)
End of explanation
"""


print(feed.feed.title)
print(feed.feed.link)
print(feed.feed.description)

"""
Explanation: We can look at some of the feed elements through the feed attribute.
End of explanation
"""


feed.feed.published

"""
Explanation: Published date is another thing we can look at, but not all feeds use this.
End of explanation
"""


feed.get('published', 'N/A')

"""
Explanation: Like with dictionaries, we can use the "get" method to see if a key exists. This is useful if we are writing code.
End of explanation
"""


len(feed.entries)

"""
Explanation: The items we are looking for are contained in the entries. Given the feed we are working with, how many entries do you think we have?
End of explanation
"""


feed.entries[0].title

i = 0
for entry in feed.entries:
    print(i, feed.entries[i].title)
    i += 1

"""
Explanation: The items in entries are stored as a list.
End of explanation
"""


df = pd.DataFrame(feed.entries)

df

df.summary[0]

"""
Explanation: Given that information, what is something we can do with this data? Why not make it a dataframe?
End of explanation
"""


