api.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """API module"""
  2. import requests
  3. from bs4 import BeautifulSoup
  4. from app import BASE_URL, HEADERS, ITEMS
  5. def get_player_market():
  6. """Get player market"""
  7. # return read_player_market()
  8. return download_player_market()
  9. def read_player_market():
  10. """Read player_market"""
  11. with open('item.html') as file:
  12. return [parse_item(file)]
  13. def download_player_market():
  14. """Download the market"""
  15. items = []
  16. for item_id in ITEMS.values():
  17. print(item_id)
  18. response = requests.get(
  19. '{}storage/market/{}'.format(BASE_URL, item_id),
  20. headers=HEADERS
  21. )
  22. items.append(parse_item(response.text))
  23. return items
  24. def parse_item(html):
  25. """Parse html return item"""
  26. soup = BeautifulSoup(html, 'html.parser')
  27. print(soup.find(class_='storage_buy_button'))
  28. return {
  29. 'player_id': int(soup.find(class_='storage_buy_button')['whose']),
  30. 'player_name': soup.select_one('.storage_see.dot').string,
  31. 'price': int(soup.find(class_='storage_buy_input')['price']),
  32. 'amount': int(soup.find(class_='storage_market_number')['max']),
  33. 'total_offers': int(soup.select_one('.storage_see').string),
  34. 'item_type': int(soup.find(class_='storage_market_number')['url']),
  35. }