api.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """API module"""
  2. import re
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from app import BASE_URL, HEADERS, RESOURCES, STATE_ITEMS
  6. def download_item(item_type):
  7. """Download item id"""
  8. tries = 1
  9. html = ''
  10. while not html and tries <= 2:
  11. response = requests.post(
  12. '{}storage/market/{}'.format(BASE_URL, item_type),
  13. headers=HEADERS
  14. )
  15. html = response.text
  16. tries += 1
  17. return html
  18. def download_offers(item_type):
  19. """Download item id"""
  20. tries = 1
  21. html = ''
  22. while not html and tries <= 2:
  23. response = requests.get(
  24. '{}storage/listed/{}'.format(BASE_URL, item_type),
  25. headers=HEADERS
  26. )
  27. html = response.text
  28. tries += 1
  29. return html
  30. def get_player_market():
  31. """Get player market"""
  32. # return read_player_market()
  33. return download_player_market()
  34. def read_player_market():
  35. """Read player market"""
  36. with open('items.html') as file:
  37. resources = {}
  38. for resource_type in RESOURCES.values():
  39. resources[resource_type] = parse_player_offers(file)
  40. break
  41. return resources
  42. def download_player_market():
  43. """Download the market"""
  44. resources = {}
  45. for resource_type in RESOURCES.values():
  46. html = download_offers(resource_type)
  47. resources[resource_type] = parse_player_offers(html)
  48. return resources
  49. def parse_player_item(html, item_type):
  50. """Parse html and return player"""
  51. soup = BeautifulSoup(html, 'html.parser')
  52. return {
  53. 'player_id': int(re.sub(r'^.*\/', '', soup.select_one('.storage_see.dot')['action'])),
  54. 'player_name': soup.select_one('.storage_see.dot').string,
  55. 'price': int(float(soup.find(class_='storage_buy_input')['price'])*100),
  56. 'amount': int(soup.find(class_='storage_market_number')['max']),
  57. 'total_offers': int(re.sub(r'\..*$', '', soup.select_one('.storage_see').string)),
  58. 'item_type': item_type,
  59. }
  60. def parse_player_offers(html):
  61. """Parse html and return offers"""
  62. soup = BeautifulSoup(html, 'html.parser')
  63. offers_tree = soup.find_all(class_='list_link')
  64. offers = []
  65. for offer_tree in offers_tree:
  66. offers.append({
  67. 'player_id': int(
  68. re.sub(r'^.*\/', '', offer_tree.select_one('.results_date')['action'])
  69. ),
  70. 'player_name': offer_tree.select_one('.results_date').string,
  71. 'price': int(float(offer_tree.select('.list_level')[1]['rat'])*100),
  72. 'amount': int(offer_tree.select_one('.list_level.imp.small')['rat']),
  73. })
  74. return offers
  75. def get_state_market():
  76. """Get state market"""
  77. # return read_state_market()
  78. return download_state_market()
  79. def read_state_market():
  80. """Read state market"""
  81. with open('state_item.html') as file:
  82. return [parse_state_item(file, 1001)]
  83. def download_state_market():
  84. """Download the state market"""
  85. items = []
  86. for item_type in STATE_ITEMS.values():
  87. item = download_item(item_type)
  88. if item:
  89. items.append(parse_state_item(item, item_type))
  90. return items
  91. def parse_state_item(html, item_type):
  92. """Parse htm return state item"""
  93. soup = BeautifulSoup(html, 'html.parser')
  94. return {
  95. 'region_id': int(re.sub(r'^.*\/', '', soup.select_one('.storage_see.dot')['action'])),
  96. 'region_name': soup.select_one('.storage_see.dot').string,
  97. 'price': int(float(soup.find(class_='storage_buy_input')['price'])*100),
  98. 'amount': int(soup.find(class_='storage_market_number')['max']),
  99. 'item_type': item_type,
  100. }