def __handle(self, headers, params): resp_internal = self.__get_response(headers, params) if resp_internal is None: return ApiResponse(has_error=True, error_description='Cookie has expired.') items = resp_internal['list'] result = [] for item in items: split = item[13].split('/') track_id = str(item[1]) + '_' + str( item[0]) + '_' + split[2] + '_' + split[5] title = self.__parser.unescape(item[4] + " - " + item[3]) duration = item[5] result.append({ 'trackId': track_id, 'title': title, 'duration': duration }) response = { 'hasMore': True if resp_internal['hasMore'] == 1 else False, 'nextOffset': resp_internal['nextOffset'], 'totalCount': resp_internal['totalCount'], 'items': result } return ApiResponse(result=response)
def get_today_rate(self, currency): uri = self.url_today_rate.replace('{ccy_id}', currency) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) return ApiResponse(result=j_resp['Cur_OfficialRate'])
def get_rate(self, currency, date): uri = self.url_rate.replace('{ccy_id}', currency).replace( '{date}', utils.date_to_string(date, self.DATE_FORMAT)) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) return ApiResponse(result=j_resp['Cur_OfficialRate'])
def get_forecast(self, location_id): query = self.QUERY.replace('{location_id}', str(location_id)) query = urllib2.quote(query) uri = self.URI.replace('{query}', query) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) res = {} res_forecast = [] results = j_resp['query']['results'] if results is not None: res_item = results['channel']['item'] condition = res_item['condition'] forecast = res_item['forecast'] res_condition = { 'temp': condition['temp'], 'text': condition['text'], 'date': condition['date'] } for item in forecast: res_for = { 'day': item['day'], 'date': item['date'], 'high': item['high'], 'low': item['low'], 'text': item['text'] } res_forecast.append(res_for) else: return ApiResponse(has_error=True, error_description='Yahoo API error') res['condition'] = res_condition res['forecast'] = res_forecast return ApiResponse(result=res)
def get_audio_info(self, audio_ids): ids = ','.join(str(x) for x in audio_ids) headers = {'cookie': self.__cookie} params = {'act': 'reload_audio', 'al': 1, 'ids': ids} items = self.__get_response(headers, params) if items is None: return ApiResponse(has_error=True, error_description='Cookie has expired.') result = [] for item in items: track_id = str(item[1]) + '_' + str(item[0]) link = item[2] result.append({'trackId': track_id, 'link': link}) res = result[0] if len(result) == 1 else result return ApiResponse(result=res)
def get_rates_dynamics(self, currency, from_date, to_date): if self.__cur_mapping is None: self.__cur_mapping = self.get_currencies() if self.__cur_mapping is None or len(self.__cur_mapping) == 0: logging.debug(u'Initialization failed') return ApiResponse( has_error=True, error_description='Connection error on initialization') if currency not in self.__cur_mapping: return ApiResponse( has_error=True, error_description='Unsupported/unknown currency') currency_id = self.__cur_mapping[currency] uri = self.url_dynamics \ .replace("{ccy_id}", str(currency_id)) \ .replace("{from}", utils.date_to_string(from_date, self.DATE_FORMAT)) \ .replace("{to}", utils.date_to_string(to_date, self.DATE_FORMAT)) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) rates = {} for item in j_resp: date = utils.string_to_date(item['Date'], self.ISO_DATE_FORMAT) str_date = utils.date_to_string(date, self.DATE_FORMAT) rate = item['Cur_OfficialRate'] rates[str_date] = rate return ApiResponse( result=collections.OrderedDict(sorted(rates.items())))
def __get_place_internal(self, query, yql_query): query = yql_query.replace('{query}', query) query = urllib2.quote(query) uri = self.URI.replace('{query}', query) response = self.__client.get_response(uri) if response.has_error: return ApiResponse(has_error=True, error_description='Connection error') j_resp = json.loads(response.response_text) if j_resp['query']['results'] is None: return ApiResponse(result=[]) places = j_resp['query']['results']['place'] count = int(j_resp['query']['count']) if count == 1: places = [places] results = [] for place in places: woeid = place['woeid'] name = place['name'] place_type = place['placeTypeName']['content'] country = place['country']['content'] res = { 'name': name, 'woeid': woeid, 'placeType': place_type, 'country': country } results.append(res) return ApiResponse(result=results)