def rebuild(local=False): opened = False try: start = time.time() Price.clear_table() if local: f = open('manamarket.html') else: f = urllib.urlopen('http://manamarket.sagdas.net/manamarket.html') opened = True for L in f: L = L.strip() if L == '</table>': # We're done :) break # In the name of speed... (rather than .startswith()) elif L[:4] != '<tr>': continue # chop off the "<tr> <td>" (9 chars) at the beginning, split at the # cell borders ("</td> <td>"), and keep the first 3 rows, which # will be completely de-HTMLed :) keep = L[9:].split('</td> <td>')[:3] # Create and add the entry Price({'item': keep[0], 'quantity': int(keep[1].replace(',', '')), 'price': int(keep[2].replace(',', '')) }).add() except Exception: # Output the error sys.excepthook(*sys.exc_info()) w, success = 'failed', False get_connection().rollback() else: w, success = 'has been completed', False get_connection().commit() if opened: f.close() taken = time.time() - start print 'Rebuilding the TMW price database %s after %s seconds.' % (w, taken) close_connection() return success
def tmw_price(client, nick, crawler): """ .tmw-price ["for"] <some item> -- get the TMW server price for an item, based on the ~600,000 item online history of the ManaMarket bot. """ if crawler.normal(False).lower() == 'for': # Consume it crawler.normal() item = crawler.chain.title() if not item: return '`tmw-price` requires an item argument!' cheap = expensive = total = items = records = 0 for record in Price.filter(item=item): if not cheap or record.price < cheap: cheap = record.price if record.price > expensive: expensive = record.price total += record.quantity * record.price items += record.quantity records += 1 close_connection() if not records: return 'I see no %s in the records.' % item average = total // items reps = (items, records, cheap, expensive) insert = 'over %s items in %s records, min %s, max %s' % reps return 'price for %s: %sgp (%s)' % (item, average, insert)