from rival_regions_wrapper.middleware import LocalAuthentication from rival_regions_wrapper import util from rival_regions_wrapper.wrapper.abstract_wrapper import AbstractWrapper import re from bs4 import BeautifulSoup authentication = LocalAuthentication(True) authentication.set_credentials("mail@gmail.com","password","google") class CurrentPrice(AbstractWrapper): def info(self, resource): """Get profile""" if isinstance(resource, str) and resource in util.ITEM_KEYS: resource = util.ITEM_KEYS[resource] path = 'storage/listed/{}'.format(resource) response = self.middleware.get(path) soup = BeautifulSoup(response, 'html.parser') offers_tree = soup.find_all(class_='list_link') test = 0 prijs = 0 for offer_tree in offers_tree: if test == 0: test = 1 prijs = int( float(offer_tree.select('.list_level')[1]['rat']) * 100 ) return prijs class Amount(AbstractWrapper): def info(self, resource): """Get profile""" if isinstance(resource, str) and resource in util.ITEM_KEYS: resource = util.ITEM_KEYS[resource] path = 'storage/listed/{}'.format(resource) response = self.middleware.get(path) soup = BeautifulSoup(response, 'html.parser') offers_tree = soup.find_all(class_='list_link') test = 0 for offer_tree in offers_tree: if test ==0: test =1 amount = int(offer_tree.select_one('.list_level.imp.small')['rat']) return amount class Profile(AbstractWrapper): def info(self, resource): """Get profile""" if isinstance(resource, str) and resource in util.ITEM_KEYS: resource = util.ITEM_KEYS[resource] path = 'storage/listed/{}'.format(resource) response = self.middleware.get(path) soup = BeautifulSoup(response, 'html.parser') offers_tree = soup.find_all(class_='list_link') test = 0 profiel = 0 for offer_tree in offers_tree: if test ==0: test =1 profiel = int(re.sub( r'^.*\/', '', offer_tree.select_one('.results_date')['action'])) return profiel class BuyMarket(AbstractWrapper): def info(self, resource,price): current_price = CurrentPrice(authentication).info(resource)/100 if current_price < price: profiel = Profile(authentication).info(resource) amount = Amount(authentication).info(resource) item = util.ITEM_KEYS[resource] self.middleware.post( 'storage/buy/{}/{}/{}/{}'.format(item,profiel,amount,current_price)) return True