def netflix_movie_from_title(cur_movie, only_id=False): movie = Movies() if cur_movie and cur_movie.Title and cur_movie.Year: try: # Query netflix search API with OAuth consumer = { "oauth_token": settings.API_KEYS["NETFLIX"], "oauth_token_secret": settings.API_KEYS["NETFLIX_SECRET"], } params = {"term": cur_movie.Title, "start_index": 0, "max_results": 5} request = OAuthRequest("http://api-public.netflix.com/catalog/titles", "GET", params) request.sign_request(OAuthSignatureMethod_HMAC_SHA1, consumer) url = request.to_url(include_oauth=True) req = urllib2.Request(url) res = urllib2.urlopen(req) if res.getcode() == 200: # Parse xml response dom = parseString(res.read()) for node in dom.getElementsByTagName("catalog_title"): # If year is the same if ( node.getElementsByTagName("release_year") and node.getElementsByTagName("release_year")[0] and abs(cur_movie.Year - int(node.getElementsByTagName("release_year")[0].childNodes[0].data)) < 1 ): if node.getElementsByTagName("id") and node.getElementsByTagName("id")[0]: if only_id: return { "id": netflix_id_from_input( str(node.getElementsByTagName("id")[0].childNodes[0].data) ) } else: return movie_from_netflix_input(node.getElementsByTagName("id")[0].childNodes[0].data) return {"error_msg": "Invalid"} else: return {"error_msg": "Invalid"} except Exception: return {"error_msg": "Netflix API failed, please try again"} else: return {"error_msg": "No title to search from."}
def get_netflix_dom(netflix_id, href=None): try: # Query netflix API using OAuth consumer = { "oauth_token": settings.API_KEYS["NETFLIX"], "oauth_token_secret": settings.API_KEYS["NETFLIX_SECRET"], } request = None if href: request = OAuthRequest(href) else: request = OAuthRequest("http://api-public.netflix.com/catalog/titles/movies/" + netflix_id) request.sign_request(OAuthSignatureMethod_HMAC_SHA1, consumer) url = request.to_url(include_oauth=True) req = urllib2.Request(url) res = urllib2.urlopen(req) if res.getcode() == 200: # Parse xml response return parseString(res.read()) else: return {"Response": False} except Exception: return {"Response": False}