api.py 3.4 KB

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