Browse Source

Fixing JSONReponse

Marco Quezada 11 năm trước cách đây
mục cha
commit
b30ad4a039

+ 8 - 8
hackathon_starter/hackathon/scripts/nytimes.py

@@ -8,15 +8,15 @@ def fetcharticle(apikey, url):
     '''returns the JSON data of the most
     '''returns the JSON data of the most
     popular articles by view from the past 24 hours.'''
     popular articles by view from the past 24 hours.'''
     parameters = {'api-key' : apikey}
     parameters = {'api-key' : apikey}
-    req = requests.get(url)
+    req = requests.get(url, params=parameters)
     data = json.loads(req.content)
     data = json.loads(req.content)
     parsedData = []
     parsedData = []
-    stockData = {}
+    newsData = {}
     for datum in data:
     for datum in data:
-    	stockData = ['title'] = data['title']
-    	stockData = ['abstract'] = data['abstract']
-    	stockData = ['section'] = data['section']
-    	stockData = ['byline'] = data['byline']
-    	stockData = ['views'] = data['views']
-    parsedData.append(stockData)
+    	newsData['title'] = data['results']['title']
+    	newsData['abstract'] = data['results']['abstract']
+    	newsData['section'] = data['results']['section']
+    	newsData['byline'] = data['results']['byline']
+    	newsData['views'] = data['results']['views']
+    parsedData.append(newsData)
     return parsedData
     return parsedData

+ 1 - 1
hackathon_starter/hackathon/views.py

@@ -177,7 +177,7 @@ def quandlstocks(request):
 def nytimespop(request):
 def nytimespop(request):
     POPAPIKEY = 'be4cd251d8a4f1a3362689088bdb0255:0:71947444'
     POPAPIKEY = 'be4cd251d8a4f1a3362689088bdb0255:0:71947444'
     popdata = fetcharticle(POPAPIKEY, 'http://api.nytimes.com/svc/mostpopular/v2/mostviewed/all-sections/1.json?')
     popdata = fetcharticle(POPAPIKEY, 'http://api.nytimes.com/svc/mostpopular/v2/mostviewed/all-sections/1.json?')
-    return JSONResponse({'data':popdata})
+    return JSONResponse({'data': popdata})
 
 
 #################
 #################
 #   GITHUB API  #
 #   GITHUB API  #