3
0

buy_bot.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from rival_regions_wrapper.middleware import LocalAuthentication
  2. from rival_regions_wrapper import util
  3. from rival_regions_wrapper.wrapper.abstract_wrapper import AbstractWrapper
  4. import re
  5. from bs4 import BeautifulSoup
  6. authentication = LocalAuthentication(True)
  7. authentication.set_credentials("mail@gmail.com", "password", "google")
  8. class CurrentPrice(AbstractWrapper):
  9. def info(self, resource):
  10. """Get profile"""
  11. if isinstance(resource, str) and resource in util.ITEM_KEYS:
  12. resource = util.ITEM_KEYS[resource]
  13. path = "storage/listed/{}".format(resource)
  14. response = self.middleware.get(path)
  15. soup = BeautifulSoup(response, "html.parser")
  16. offers_tree = soup.find_all(class_="list_link")
  17. test = 0
  18. prijs = 0
  19. for offer_tree in offers_tree:
  20. if test == 0:
  21. test = 1
  22. prijs = int(
  23. float(offer_tree.select(".list_level")[1]["rat"]) * 100
  24. )
  25. return prijs
  26. class Amount(AbstractWrapper):
  27. def info(self, resource):
  28. """Get profile"""
  29. if isinstance(resource, str) and resource in util.ITEM_KEYS:
  30. resource = util.ITEM_KEYS[resource]
  31. path = "storage/listed/{}".format(resource)
  32. response = self.middleware.get(path)
  33. soup = BeautifulSoup(response, "html.parser")
  34. offers_tree = soup.find_all(class_="list_link")
  35. test = 0
  36. for offer_tree in offers_tree:
  37. if test == 0:
  38. test = 1
  39. amount = int(
  40. offer_tree.select_one(".list_level.imp.small")["rat"]
  41. )
  42. return amount
  43. class Profile(AbstractWrapper):
  44. def info(self, resource):
  45. """Get profile"""
  46. if isinstance(resource, str) and resource in util.ITEM_KEYS:
  47. resource = util.ITEM_KEYS[resource]
  48. path = "storage/listed/{}".format(resource)
  49. response = self.middleware.get(path)
  50. soup = BeautifulSoup(response, "html.parser")
  51. offers_tree = soup.find_all(class_="list_link")
  52. test = 0
  53. profiel = 0
  54. for offer_tree in offers_tree:
  55. if test == 0:
  56. test = 1
  57. profiel = int(
  58. re.sub(
  59. r"^.*\/",
  60. "",
  61. offer_tree.select_one(".results_date")["action"],
  62. )
  63. )
  64. return profiel
  65. class BuyMarket(AbstractWrapper):
  66. def info(self, resource, price):
  67. current_price = CurrentPrice(authentication).info(resource) / 100
  68. if current_price < price:
  69. profiel = Profile(authentication).info(resource)
  70. amount = Amount(authentication).info(resource)
  71. item = util.ITEM_KEYS[resource]
  72. self.middleware.post(
  73. "storage/buy/{}/{}/{}/{}".format(
  74. item, profiel, amount, current_price
  75. )
  76. )
  77. return True