Created
March 23, 2013 19:29
-
-
Save NahimNasser/5229060 to your computer and use it in GitHub Desktop.
Quick solution for assignment 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from django.core.management import setup_environ | |
from ymcproject import settings | |
setup_environ(settings) | |
import simplejson as json | |
from experiments import models | |
def _get_dict_for_one_page_of_products(page): | |
response = requests.get("http://lcboapi.com/products?per_page=100&page=%d" % page) | |
lcbo_data = json.loads(response.content) | |
return lcbo_data | |
def _store_products_from_a_page(lcbo_data): | |
for product in lcbo_data: | |
if "name" in product: | |
lcbo_product = models.LcboProduct(name=product['name']) | |
lcbo_product.save() | |
def store_all_lcbo_products(): | |
has_next = True | |
page = 0 | |
while has_next: | |
page += 1 | |
try: # Thanks alot lcbo api for giving us malformed json. | |
lcbo_data = _get_dict_for_one_page_of_products(page) | |
_store_products_from_a_page(lcbo_data['result']) | |
except json.scanner.JSONDecodeError: | |
continue | |
if lcbo_data['pager']['is_final_page']: | |
has_next = False | |
print "Begin stealing booze..." | |
store_all_lcbo_products() | |
print "%d liquor products have been stolen" % models.LcboProduct.objects.count() | |
# print "Type the following: models.LcboProduct.objects.all()" | |
# import pdb; pdb.set_trace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment