amalakar / pyflix2

Python module for accessing Netflix APIs
http://pyflix2.readthedocs.org/en/latest/
BSD 2-Clause "Simplified" License
11 stars 8 forks source link

Does "?expand=" construct work with pyflix2? #10

Open paritosh91 opened 11 years ago

paritosh91 commented 11 years ago

Hi,

Netflix API says that this URL:

http://api-public.netflix.com/catalog/titles/movies/60036637?expand=synopsis%20cast%20directors%20awards%20similars

should work while getting title details. However, the get_title() function has this check for the categories:

if category and EXPANDS.index("@" + category) >= 0: url = "%s/%s" % (url, category)

This restricts the category to be a singe word and defeats the purpose of having expands. I tried a quick crude fix :

if category : expansions = category.split() countOfExp = len(expansions) countOfLegalExp = 0

            for expansion in expansions:                    
                if EXPANDS.index("@" + expansion.strip()) >= 0:
                    countOfLegalExp += 1

            if countOfExp == countOfLegalExp:
                url="%s?expand=%s" %(url,urllib.quote(category))
            else:
                raise NetflixError("Illegal Expansions present.")

However, this doesn't work. All I get in return is unexpanded data.

Please help.

Regds, Paritosh

amalakar commented 11 years ago

Paritosh, are you sure expand can take more than one alias at a time?

I am referring to:

http://developer.netflix.com/page/Netflix_API_20_Release_Notes#expansions

Can you point me to documentation that says so? I will have a look at it when I am home.

amalakar commented 11 years ago

Looking at the example mentioned in http://kentbrewster.com/netflix-api-explorer/

http://api.netflix.com/catalog/titles/movies/493387?expand=cast,synopsis

I guess it should comma separated. You are trying space separated, can you try comma instead?

amalakar commented 11 years ago

The following patch seems to work for me. I will clean up the patch and commit it.

diff --git a/pyflix2/pyflix2.py b/pyflix2/pyflix2.py
index f74b240..90d58a1 100644
--- a/pyflix2/pyflix2.py
+++ b/pyflix2/pyflix2.py
@@ -221,9 +221,18 @@ class _NetflixAPI(object):

         if id.startswith('http'):
             url=id
-            if category and EXPANDS.index("@" + category) >= 0:
-                url = "%s/%s" % (url, category)
-            return self._request('get', url).json()
+            data = {}
+            if category:
+                if isinstance(category, basestring):
+                    category = [category]
+                for expand in category:
+                    try:
+                        EXPANDS.index(expand)
+                    except:
+                        raise NetflixError(expand, " is not part of allowed list of EXPANDS")
+
+                data["expand"] = ",".join(category)
+            return self._request('get', url, data).json()
         else:
             raise NetflixError("The id should be like: http://api.netflix.com/catalog/movies/60000870")

diff --git a/pyflix2/test.py b/pyflix2/test.py
index 4c6d63a..1e7b8e8 100644
--- a/pyflix2/test.py
+++ b/pyflix2/test.py
@@ -128,9 +128,9 @@ class TestNetflixAPIV2(unittest.TestCase):
             self.assertIsNotNone(title[u'id'])
         self.assertEqual(int(titles_instant['results_per_page']), 25)

-        m = self.netflix.get_title(titles_instant['catalog'][0]['id'])
+        m = self.netflix.get_title(titles_instant['catalog'][0]['id'], ["@box_art", "@cast", "@languages_and_audio"])
         self.assertIsNotNone(m)
-        #dump_object(m)
+        dump_object(m)

         m = self.netflix.get_title(titles_instant['catalog'][0]['id'], "languages_and_audio")
         self.assertIsNotNone(m)