import sys import os import re import urllib import json """ Sample code to demonstrate the BlipADeal API Written By By: Sido.B - 13th JULY 2012 For further information please visit : www.blipadeal.com/dev-api For Online working demo please visit : webapi.blipadeal.com/webapi For support or help please email : support@blipadeal.com """ class blipadeal_api_sample_program: def __init__(self): pass def _start(self): print print '----------------------------------------------------------' print 'TESTING the "/api_get_deal_location_near_by_text" API call' print '----------------------------------------------------------' response_CLOSE_CITY = self._get_deals_near_by_text('AUSTRALIA','SYDNEY', 200) print print '----------------------------------------------------------' print 'TESTING the "/api_get_deal_location_near_by_geo" API call' print '----------------------------------------------------------' response_CLOSE_CITY_geo = self._get_deals_near_by_coord( response_CLOSE_CITY ) print print '----------------------------------------------------------' print 'TESTING the "/api_get_category_type" & api_get_category_deals API call' print '----------------------------------------------------------' response_FREE_TEXT = self._get_deals_of_category_type_in_given_location('AUSTRALIA','SYDNEY') print print '----------------------------------------------------------' print 'TESTING the "/api_get_country" API call' print '----------------------------------------------------------' response_COUNTRY = self._get_all_countries() print print '----------------------------------------------------------' print 'TESTING the "/api_get_location" API call' print '----------------------------------------------------------' response_CITY = self._get_all_cities_from_a_country('AUSTRALIA') print print '----------------------------------------------------------' print 'TESTING the "/api_get_category" API call' print '----------------------------------------------------------' response_CATEGORY = self._get_all_category_from('AUSTRALIA','SYDNEY') print print '----------------------------------------------------------' print 'TESTING the "/api_search_keyword" API call' print '----------------------------------------------------------' response_KEYWORDS = self._get_deals_given_keywords('AUSTRALIA','SYDNEY') """ "Generic function to send the request to the API" """ def _call_function(self, function_name, json_object): json_object["message_status" ] = "SUCCESS" json_object["api_key_token" ] = "" json_object["api_secret_token"] = "" response_handle = urllib.urlopen( 'https://webapi.blipadeal.com/' + function_name + '?request=' + json.dumps( json_object ) ) JSON_object = json.loads(response_handle.read()) return ( JSON_object ) # end def """ This function will get the location of all the places where there are deals within a 200km region """ def _get_deals_near_by_text(self, country, city, distance): json_request = { "param_city" : city, "param_country" : country, "param_distance": distance } response_JSON = self._call_function( 'api_get_deal_location_near_by_text', json_request ) response_status = response_JSON['message_action'] if response_status == 'GET_DEAL_LOCATION_NEAR_BY_SUCCESSFUL': point_of_interest_array = response_JSON['poi_list'] print for point in point_of_interest_array: print 'NEAR TO - ' + point['blp_country_city'] print ' Distance - ' + point['blp_distance'] print ' Latitude - ' + point['blp_publish_lat_pos'] print ' Longtitude - ' + point['blp_publish_long_pos'] print return response_JSON # end def """ This function will take the longtitude and lattitude from the third city returned from the '_get_deals_near_by_text' function call and use it to find what is close by that within a 500km radius """ def _get_deals_near_by_coord(self, json_coords ): point = json_coords['poi_list'][2] lat = point['blp_publish_lat_pos'] lng = point['blp_publish_long_pos'] json_request = { "param_lng" : lng, "param_lat" : lat, "param_distance": 500 } response_JSON = self._call_function( 'api_get_deal_location_near_by_geo', json_request ) response_status = response_JSON['message_action'] if response_status == 'GET_DEAL_LOCATION_NEAR_BY_SUCCESSFUL': point_of_interest_array = response_JSON['poi_list'] print for point in point_of_interest_array: print 'NEAR TO - ' + point['blp_country_city'] print ' Distance - ' + point['blp_distance'] print ' Latitude - ' + point['blp_publish_lat_pos'] print ' Longtitude - ' + point['blp_publish_long_pos'] print return response_JSON # end def """ This function will get all the deals based on a phrase in the city of sydney. Then it will look for deals near by sydney given the same phrase """ def _get_deals_of_category_type_in_given_location(self, country, city): json_request = { 'free_text' : urllib.quote('I want to buy a Cheap ipod or iPhone & maybe a computer') } response_JSON = self._call_function( 'api_get_category_type', json_request ) response_status = response_JSON['message_action'] preferred_category = None if response_status == 'GET_CATEGORY_TYPE_SUCCESS': category_array = response_JSON['api_category_type' ] category_num = response_JSON['api_category_count'] if category_num > 1: print 'Preferred Category 1 = ' + category_array[0][0] print 'Preferred Category 2 = ' + category_array[1][0] else: print 'Only One Preferred Category = ' + category_array[0][0] # end if preferred_category = category_array[0][0] # end if """ Now lets look for all the items that match this category and find them all in Sydney """ json_request_2 = { "city_select" : city, "country_select" : country, "category_list" : [preferred_category], "sort_method" : "bought" } response_JSON_2 = self._call_function( 'api_get_category_deals', json_request_2 ) response_status_2 = response_JSON_2['message_action'] if response_status_2 == 'DEAL_RETRIEVE_SUCCESS': deals_from_sydney = response_JSON_2['deal_list'] print print "------------------- DEALS FROM "+city+" -------------------" # just print out the first 50 deal summaries count = 0 for deal in deals_from_sydney[0:50]: # truncate the string count = count + 1 print str(count) + ' ' + deal['blp_main_deal_summary'][0:100] + '...' # end for # end if return response_JSON_2 # end def """ This function will retrieve all of the countries currently active within the blipADeal """ def _get_all_countries(self): json_request = {} response_JSON = self._call_function( 'api_get_country', json_request ) response_status = response_JSON['message_action'] if response_status == 'COUNTRY_RETRIEVE_SUCCESS': country_list = response_JSON['country_list'] print print "------------------- COUNTRIES AVAILABLE -------------------" for country in country_list: country_name = country['blp_main_deal_detail_country_location'] city_count = country['blp_city_count'] print ' ' + country_name + ' with ' + str( city_count ) + ' cities' # end for # end if return response_JSON # end def """ This function will retrieve all the cities in a given country. You may want to use this to get deals from multiple cities """ def _get_all_cities_from_a_country(self, country): json_request = { "param_country" : country } response_JSON = self._call_function( 'api_get_location', json_request ) response_status = response_JSON['message_action'] if response_status == 'CITY_RETRIEVE_SUCCESS': city_list = response_JSON['location_list'] print "------------------- CITIES IN " + country + " -------------------" for city in city_list: city_name = city['deal_location'] country_name = city['deal_country' ] print ' ' + country_name + ' ' + city_name # end for # end if return response_JSON # end def """ This function will retrieive all the categories given a location defined by a city and country """ def _get_all_category_from(self, country, city): json_request = { "param_country" : country, "param_city" : city } response_JSON = self._call_function( 'api_get_category', json_request ) response_status = response_JSON['message_action'] if response_status == 'DEAL_CATEGORY_RETRIEVE_SUCCESS': category_deal_list = response_JSON['category_deal_list'] print "------------------- CATEGORIES IN " + country + " -------------------" for category_deal in category_deal_list: category_name = category_deal['blp_category_display'] category_count = category_deal['blp_category_count'] print ' ' + category_name + ' category count ' + str(category_count) # end for # end if return response_JSON # end def """ This function will retrieve all the deals given a location and specific keywords """ def _get_deals_given_keywords(self, country, city): json_request = { 'param_city' : city, 'param_country' : country , 'param_keyword_list' : [ urllib.quote("holiday"), urllib.quote("bali" ), urllib.quote("relax" ) ], 'param_sort_method' : "bought" } response_JSON = self._call_function( 'api_keyword_search', json_request ) response_status = response_JSON['message_action'] if response_status == 'DEAL_RETRIEVE_SUCCESS': deals_from_sydney = response_JSON['deal_list'] print print "------------------- DEALS FROM "+city+" -------------------" # just print out the first 50 deal summaries count = 0 for deal in deals_from_sydney[0:50]: # truncate the string count = count + 1 print str(count) + ' ' + deal['blp_main_deal_summary'][0:100] + '...' # end for # end if return response_JSON # end def # end class """ START TEST PROGRAM... """ if __name__ == '__main__': sample = blipadeal_api_sample_program() sample._start() # end if