From cea72a7d9ae41e53e00126a0528056e077eb4240 Mon Sep 17 00:00:00 2001 From: ben Date: Fri, 30 Oct 2009 15:06:59 +0000 Subject: StoreProxy ready for review --- (limited to 'tutorius/store.py') diff --git a/tutorius/store.py b/tutorius/store.py index 225cdd4..c197c9e 100644 --- a/tutorius/store.py +++ b/tutorius/store.py @@ -18,6 +18,7 @@ import urllib import urllib2 from xml.dom import minidom from apilib.restful_lib import Connection +from array import array class StoreProxy(object): """ @@ -27,14 +28,14 @@ class StoreProxy(object): """ def __init__(self): - + # Base Urls for the api - self.base_url = "http://tutorius.dev/en-US/tutorius" + self.base_url = "http://bobthebuilder.mine.nu/tutorius/en-US/tutorius" self.remora_api = "api/1.4" self.tutorius_api = "TutoriusApi" self.bandwagon_api = "api/1.4/sharing" - self.api_auth_key = "" + self.api_auth_key = None # Prepares the connection with the api self.conn = Connection(self.base_url) @@ -67,22 +68,48 @@ class StoreProxy(object): # Loop through the categories and create the list to be returned for xml_category in xml_categories: category = {} - + category['id'] = xml_category.getElementsByTagName('id')[0].firstChild.nodeValue category['name'] = xml_category.getElementsByTagName('name')[0].firstChild.nodeValue categories.append(category) return categories + + def search(self, keywords, category='all', page=1, numResults=10, sortBy='name'): + """ + Returns a list of tutorials that correspond to the given search criteria. + + @param keywords The keywords to search for + @param page The page in the result set from which to return results. This is + used to allow applications to fetch results one set at a time. + @param numResults The max number of results that can be returned in a page + @param sortBy The field on which to sort the results + @return A list of tutorial meta-data that corresponds to the query + """ + request_url = "/%s/search/%s/%s/%d/%d/%s" % (self.tutorius_api, keywords, category, page, numResults, sortBy) + + response = self.conn.request_get(request_url) + + if (self.helper.iserror(response)): + return False + + xml_response = minidom.parseString(response['body']) + + xml_tutorials = xml_response.getElementsByTagName('tutorial') + + tutorials = list() + + for xml_tutorial in xml_tutorials: + tutorial = self.helper.parse_tutorial(xml_tutorial) + tutorials.append(tutorial) + return tutorials def get_tutorials(self, category='all', page=1, numResults=10, sortBy='name'): """ Returns the list of tutorials that correspond to the given search criteria. - @param keywords The list of keywords that should be matched inside the tutorial title - or description. If None, the search will not filter the results - according to the keywords. @param category The category in which to restrict the search. @param page The page in the result set from which to return results. This is used to allow applications to fetch results one set at a time. @@ -138,17 +165,6 @@ class StoreProxy(object): return tutorials - def get_tutorial_collection(self, collection_name): - """ - Returns a list of tutorials corresponding to the given collection name. - Collections can be groups like '5 most downloaded' or 'Top 10 ratings'. - - @param collection_name The name of the collection from which we want the - meta-data - @return A list of tutorial meta-data corresponding to the given group - """ - raise NotImplementedError("get_tutorial_collection() not implemented... yet!") - def get_latest_version(self, tutorial_id_list): """ Returns the latest version number on the server, for each tutorial ID @@ -325,7 +341,7 @@ class StoreProxy(object): return True - + def unpublish(self, tutorial_store_id): """ Removes a tutorial from the server. The user in the current session @@ -433,12 +449,22 @@ class StoreProxyHelper(object): @return A dictionnary containing the metadata """ tutorial = {} - - tutorial['name'] = xml_tutorial.getElementsByTagName('name')[0].firstChild.nodeValue - tutorial['summary'] = xml_tutorial.getElementsByTagName('summary')[0].firstChild.nodeValue - tutorial['version'] = xml_tutorial.getElementsByTagName('version')[0].firstChild.nodeValue - tutorial['description'] ="" - tutorial['author'] = "" - tutorial['rating'] = "" + + params = [ + 'name', + 'summary', + 'version', + 'description', + 'author', + 'rating' + ] + + for param in params: + xml_node = xml_tutorial.getElementsByTagName(param)[0].firstChild + + if xml_node != None: + tutorial[param] = xml_node.nodeValue + else: + tutorial[param] = '' return tutorial -- cgit v0.9.1