api.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """PACC API functions"""
  2. import re
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from dateutil import parser
  6. from app import BASE_URL, HEADERS
  7. def get_rr_account(account_id):
  8. """Get Rival Region account"""
  9. response = requests.get(
  10. '{}slide/profile/{}'.format(BASE_URL, account_id),
  11. headers=HEADERS
  12. )
  13. soup = BeautifulSoup(response.text, 'html.parser')
  14. account = {
  15. 'name': None,
  16. 'region': None,
  17. 'residency': None,
  18. 'registation_date': None,
  19. }
  20. table = soup.find('table')
  21. name = soup.find('h1')
  22. if name:
  23. account['name'] = re.sub(r'.*:\s', '', name.text)
  24. for row in table.find_all('tr'):
  25. label = row.find('td').text.strip()
  26. if label == 'Region:':
  27. span = row.find('span', {'class': 'dot'})
  28. if span:
  29. account['region'] = span.text
  30. if label == 'Residency:':
  31. span = row.find('span', {'class': 'dot'})
  32. if span:
  33. account['residency'] = span.text
  34. if label == 'Registration date:':
  35. element = row.find('td', {'class': 'imp'})
  36. if element:
  37. account['registation_date'] = parser.parse(element.text)
  38. return account
  39. def get_accounts_by_name(account_name):
  40. """Get account list by name"""
  41. response = requests.get(
  42. '{}listed/region/0/{}/0'.format(BASE_URL, account_name),
  43. headers=HEADERS,
  44. )
  45. soup = BeautifulSoup(response.text, 'html.parser')
  46. accounts = []
  47. account_items = soup.find_all('tr', {'class': 'list_link'})
  48. for account_item in account_items:
  49. accounts.append({
  50. 'id': int(account_item.get('user')),
  51. 'name': account_item.find('td', {'class': 'list_name'}).text.strip(),
  52. 'level': int(account_item.find('td', {'class': 'list_level'}).text),
  53. })
  54. return accounts
  55. def send_personal_message(user_id, message):
  56. """Send personal message to player"""
  57. requests.post(
  58. '{}send_personal_message/{}'.format(BASE_URL, user_id),
  59. headers=HEADERS,
  60. data={
  61. 'message': message
  62. }
  63. )