Last active 1440821896

A python function to get netflix movie info and parse out all relevant data, returning it in a tuple as stated in the docstring

Revision 868c97cbba48c147a8a905dce739224b3e853b03

netflix.py Raw
1"""
2Example usage:
3
4 >>> netflix("60024942")
5 (u'Catch Me If You Can', u'2002', u'Thu Jan 09 08:00:00 UTC 2003', u'M', u'140 minutes', u'http://cdn2.nflximg.net/images/0432/12050432.jpg', u'An FBI agent makes it his mission to put cunning con man Frank Abagnale Jr. behind bars. But Frank not only eludes capture, he revels in the pursuit.', {u'director': u'Steven Spielberg', u'genre': u'Dramas', u'language': u'English', u'starring': u'Leonardo DiCaprio, Tom Hanks'})
6
7ID could be obtained through trivial URL parsing using any stdlib library.
8Here, i'll do an example:
9
10 from urlparse import urlparse, parse_qs # urllib.parse in python 3
11 parse_qs(urlparse("http://www.netflix.com/WiPlayer?movieid=60024942").query)
12 => {'movieid': ['60024942']}
13
14Obviously you'd have a much longer url, but you can then use r["movieid"][0] to get the id, pass it to netflix(), tadaaaaa
15"""
16
17def netflix(id):
18 """ Returns a tuple of strings: (title, year, date-published, MPAA-rating, duration, boxart-url, description, moreinfo)
19 moreinfo may contain genre, language, actor and director info, depending on what's available"""
20 soup = Soup(requests.get("http://www.netflix.com/JSON/BOB?movieid=" + id).json()["html"])
21 data = (
22 soup.find(attrs={'class': 'title'}).text.strip() if soup.find(attrs={'class': 'title'}) else None,
23 soup.find(attrs={'class': 'year'}).text.strip() if soup.find(attrs={'class': 'year'}) else None,
24 soup.find(attrs={'itemprop': 'datePublished'})["content"] if soup.find(attrs={'itemprop': 'datePublished'}) else None,
25 soup.find(attrs={'class': 'mpaaRating'}).text.strip() if soup.find(attrs={'class': 'mpaaRating'}) else None,
26 soup.find(attrs={'class': 'duration'}).text.strip() if soup.find(attrs={'class': 'duration'}) else None,
27 soup.find(attrs={'itemprop': 'thumbnailUrl'})["src"] if soup.find(attrs={'itemprop': 'thumbnailUrl'}) else None,
28 soup.find(attrs={'class', 'boxShot'}).nextSibling.strip(),
29 {k.text.strip()[:-1].lower(): " ".join(v.text.strip().split()) for k,v in zip(soup.findAll('dt'), soup.findAll('dd'))}
30 )
31 return data
32