def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. rs_api_items = Item.all() rune_wikia_url = 'http://runescape.wikia.com/wiki/%s' ItemAddition = orm.ItemAddition for item in rs_api_items: url = rune_wikia_url % item.name.replace(' ', '_') html = urlopen(url).read() index1 = html.find('High Alch') index2 = html[index1:].find('<td>') index3 = html[index1:][index2:].find('</td>') price = html[index1+index2:index1+index2+index3].replace('<td> ', '').replace(' coins\n', '') price = price.replace(' coin\n', '') try: split = price.replace(',', '').replace('gp', '').split(' ') price = '0' for p in split[::-1]: try: price = int(p) break except ValueError: continue price = int(price) ItemAddition.objects.create(id=item.id, high_alch_price=price) except ValueError: import pdb; pdb.set_trace() print 'item not found: %s %s coins' % (item.name, price) else: print 'item done: %s' % item.name
def highalch(request): item_types = request.POST.getlist('f2p_p2p_items', [HighAlchemyForm.F2P, HighAlchemyForm.P2P]) nature_rune = Item.get(name='Nature rune') fire_rune = Item.get(name='Fire rune') highalch_cost = nature_rune.price_info.exact + 4 * fire_rune.price_info.exact item_additions = ItemAddition.objects.all() processed_items = [] for item in Item.all(): if (HighAlchemyForm.F2P in item_types and not item.members_item) or (HighAlchemyForm.P2P in item_types and item.members_item): addition = [x for x in item_additions if x.id == item.id] if len(addition) > 0: addition = addition[0] processed_items.append({ 'item': item, 'highalch_profit': addition.high_alch_price - item.price_info.exact - highalch_cost }) processed_items = [q for q in sorted(processed_items, key=lambda x: -x['highalch_profit']) if q['highalch_profit'] >= 0] form = HighAlchemyForm(initial={ 'f2p_p2p_items': item_types }) return render_to_response('ge/superheat.html', { 'items': processed_items, 'form': form }, RequestContext(request))