def ParseMarketPricesCompressedHeader(self, marketPricesHeader, completeCompressed): ''' Decompress the header of the compressed market prices @param marketPricesHeader: compressed prices header string @type marketPricesHeader: str @param completeCompressed: whether the prices to decompress are the standard 3 level or the complete market prices @type completeCompressed: bool @returns: the prices header @rtype: MarketPrices header object ''' # Generate the object to store the information # marketPrices = self.GetObjectExchange('MarketPrices') if not completeCompressed: marketPrices = bfutil.Factory.MarketPrices() else: marketPrices = bfutil.Factory.CompleteMarketPrices() marketPrices.runnerPrices = list() # Split it into its constituent parts parts = marketPricesHeader.split('~') # and assign it to the fields of the generated MarketPrices object if completeCompressed == False: marketPrices.marketId = int(parts[0]) marketPrices.currencyCode = parts[1] marketPrices.marketStatus = parts[2] marketPrices.delay = int(parts[3]) marketPrices.numberOfWinners = int(parts[4]) marketPrices.marketInfo = parts[5] marketPrices.discountAllowed = bool(parts[6]) marketPrices.marketBaseRate = float(parts[7]) marketPrices.lastRefresh = bftimezone.fromTimestampString(parts[8]) marketPrices.removedRunners = parts[9] marketPrices.bspMarket = (parts[10] == 'Y') else: # For completecompressed, fill the available fields and (remove) the others marketPrices.marketId = int(parts[0]) # del marketPrices.currencyCode # del marketPrices.marketStatus marketPrices.delay = int(parts[1]) # del marketPrices.numberOfWinners # del marketPrices.marketInfo # del marketPrices.discountAllowed # del marketPrices.marketBaseRate # del marketPrices.lastRefresh marketPrices.removedRunners = parts[2] # del marketPrices.bspMarket # return the object return marketPrices
def __call__(self, response, **kwargs): # Return quickly if nothing in place if not response.marketData: response.marketData = list() return # Creat a placeholder for the marketItems to be parsed marketItems = list() # Parse the marketItems data string - separate major fields marketDataParts = response.marketData.split(':') # First element is always empty ... pop it out because Betfair places a delimiter at the beginning marketDataParts.pop(0) toSkip = 0 for i in xrange(len(marketDataParts)): if toSkip > 0: toSkip -= 1 continue # Break fields marketItemFields = marketDataParts[i].split('~') while len(marketItemFields) < 16: # 2010-04-04 ... 16 is the number of fields, but the documentation says # that there could be more in the future and to parse accordingly toSkip += 1 # The last character of the last field should be a backslash # pop out, strip char lastField = marketItemFields.pop(len(marketItemFields) - 1).rstrip('\\') # Get the extra fields needed extraFields = marketDataParts[i + toSkip].split('~') # join the popped out with the first one, since they were separated by the colon # and join them with a colon rebuiltField = lastField + ':' + extraFields.pop(0) # Re-insert the rebuilt at the end and extend with the extra fields marketItemFields.append(rebuiltField) marketItemFields.extend(extraFields) # Create a generic object and populate it with the market fields marketItem = bfutil.Factory.MarketItem() # marketItem = self.GetObjectExchange('Market') marketItem.marketId = int(marketItemFields[0]) marketItem.marketName = marketItemFields[1] marketItem.marketType = marketItemFields[2] marketItem.marketStatus = marketItemFields[3] marketItem.marketTime = bftimezone.fromTimestampString(marketItemFields[4]) menuPathParts = list() menuParts = marketItemFields[5].split('\\') for menuPart in menuParts: menuPathParts.append(menuPart.rstrip()) # Hack to remove unexistent 'Group X' menu path for the 4 events if menuPathParts[1] in ('Tennis', 'Golf', 'Cricket', 'Rugby Union'): if 'Group' in menuPathParts[2]: menuPathParts.pop(2) marketItem.menuPathParts = menuPathParts marketItem.menuPath = '\\'.join(menuPathParts) # Save the broken down menupath parts marketItem.menuPathParts = menuPathParts # Pop the initial empty element from the list marketItem.menuPathParts.pop(0) eventHierarchyStrings = marketItemFields[6].split('/') if len(eventHierarchyStrings): # if the event hierarchy is not empty # skip the leading '/' by popping the empty token eventHierarchyStrings.pop(0) marketItem.eventHierarchy = list() for ident in eventHierarchyStrings: marketItem.eventHierarchy.append(long(ident)) marketItem.delay = int(marketItemFields[7]) marketItem.exchangeId = int(marketItemFields[8]) marketItem.countryISO3 = marketItemFields[9] marketItem.lastRefresh = bftimezone.fromTimestampString(marketItemFields[10]) marketItem.numberOfRunners = int(marketItemFields[11]) marketItem.numberOfWinners = int(marketItemFields[12]) marketItem.totalAmountMatched = float(marketItemFields[13]) marketItem.bspMarket = (marketItemFields[14] == 'Y') marketItem.turningInPlay = (marketItemFields[15] == 'Y') marketItems.append(marketItem) response.marketData = marketItems