Prechádzať zdrojové kódy

Sending JSON Response of scraped steam sale data

David Leonard 10 rokov pred
rodič
commit
a5eecc4deb

+ 54 - 55
hackathon_starter/hackathon/scripts/scraper.py

@@ -2,68 +2,67 @@ import requests
 from bs4 import BeautifulSoup
 import itertools 
 
-req = requests.get('http://store.steampowered.com/search/?specials=1#sort_by=_ASC&sort_order=ASC&specials=1&page=1')
-content = req.text
-soup = BeautifulSoup(content)
-allData = {id: {} for id in range(0, 25)}
-
-
-# Get all divs of a specific class
-releaseDate = soup.findAll('div', {'class': 'col search_released'})
-
-# Get all release dates
-releaseDates = []
-id = 0
-for date in releaseDate:
-	allData[id]['releaseDates'] = date.text
-	releaseDates.append(date.text)
-	id += 1
-
-#print releaseDates
-
-id = 0
-gameName = soup.findAll('div', {'class': 'col search_name ellipsis'})
-
-# Get all game names
-gameNames = []
-for name in gameName:
-	span = name.findAll('span', {'class': 'title'})
-	for tag in span:
-		allData[id]['name'] = tag.text
-		gameNames.append(tag.text)
+def steamDiscounts():
+	req = requests.get('http://store.steampowered.com/search/?specials=1#sort_by=_ASC&sort_order=ASC&specials=1&page=1')
+	content = req.text
+	soup = BeautifulSoup(content)
+	allData = {id: {} for id in range(0, 25)}
+
+
+	# Get all divs of a specific class
+	releaseDate = soup.findAll('div', {'class': 'col search_released'})
+
+	# Get all release dates
+	releaseDates = []
+	id = 0
+	for date in releaseDate:
+		allData[id]['releaseDates'] = date.text
+		releaseDates.append(date.text)
 		id += 1
 
-# print gameNames
+	#print releaseDates
 
-discount = soup.findAll('div', {'class': 'col search_discount'})
+	id = 0
+	gameName = soup.findAll('div', {'class': 'col search_name ellipsis'})
 
-id = 0
-# Get all game discounts 
-gameDiscounts = []
-for discountedGame in discount:
-	span = discountedGame.findAll('span')
-	for tag in span:
-		allData[id]['discount'] = tag.text
-		gameDiscounts.append(tag.text)
-		id += 1
+	# Get all game names
+	gameNames = []
+	for name in gameName:
+		span = name.findAll('span', {'class': 'title'})
+		for tag in span:
+			allData[id]['name'] = tag.text
+			gameNames.append(tag.text)
+			id += 1
 
-# print gameDiscounts
+	# print gameNames
 
-price = soup.findAll('div', {'class': 'col search_price discounted'})
+	discount = soup.findAll('div', {'class': 'col search_discount'})
 
-id = 0
-prices = []
-# Get all discounted prices
-for value in price:
-	br = value.findAll('br')
-	for tag in br:
-		allData[id]['price'] = tag.text.strip('\t')
-		prices.append(tag.text.strip('\t'))
-		id += 1
+	id = 0
+	# Get all game discounts 
+	gameDiscounts = []
+	for discountedGame in discount:
+		span = discountedGame.findAll('span')
+		for tag in span:
+			allData[id]['discount'] = tag.text
+			gameDiscounts.append(tag.text)
+			id += 1
+
+	# print gameDiscounts
+
+	price = soup.findAll('div', {'class': 'col search_price discounted'})
+
+	id = 0
+	prices = []
+	# Get all discounted prices
+	for value in price:
+		br = value.findAll('br')
+		for tag in br:
+			allData[id]['price'] = tag.text.strip('\t')
+			prices.append(tag.text.strip('\t'))
+			id += 1
 
-# print prices
+	# print prices
+	return allData
 
-# Print all the data
-for i in allData:
-	print allData[i]
 

+ 1 - 0
hackathon_starter/hackathon/urls.py

@@ -9,6 +9,7 @@ urlpatterns = patterns('',
     url(r'^logout/$', views.user_logout, name='logout'),
     url(r'^api/$', views.api_examples, name='api'),
     url(r'^steam/$', views.steam, name='steam'),
+    url(r'^steamDiscountedGames/$', views.steamDiscountedGames, name='steamDiscountedGames'),
     url(r'^githubResume/$', views.githubResume, name='githubResume'),
     url(r'^githubUser/$', views.githubUser, name='githubUser'),
     url(r'^githubTopRepositories/$', views.githubTopRepositories, name='githubTopRepositories'),

+ 5 - 0
hackathon_starter/hackathon/views.py

@@ -17,6 +17,7 @@ from scripts.github import *
 from scripts.tumblr import TumblrOauthClient
 from scripts.twilioapi import *
 from scripts.instagram import InstagramOauthClient
+from scripts.scraper import steamDiscounts
 
 # Python
 import oauth2 as oauth
@@ -71,6 +72,10 @@ def steam(request):
     return render(request,'hackathon/steam.html', {"game": game })
 
 
+def steamDiscountedGames(request):
+    data = steamDiscounts()
+    return JsonResponse({ 'data': data })
+    
 #################
 #   GITHUB API  #
 #################