api.py 3.5 KB

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