neoaccount.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import logging
  2. import pickle
  3. from pathlib import Path
  4. import requests
  5. from bs4 import BeautifulSoup as bs
  6. class NeoAccount:
  7. domain = 'http://www.neopets.com'
  8. firefoxUA = 'Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0'
  9. chromeUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.17 Safari/537.36'
  10. def __init__(self, config):
  11. self.username = config['username']
  12. self.password = config['password']
  13. self.proxy = config['proxy']
  14. self.useragent = config['useragent']
  15. self.userhash = self.username.encode('utf-8')
  16. self.cachefile = Path('.cache' + '/' + self.username)
  17. # Apply headers based on browser in useragent
  18. if 'Firefox' in self.useragent:
  19. self.headers = {
  20. 'User-Agent': self.useragent,
  21. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  22. 'Accept-Language': 'en-US,en;q=0.5',
  23. 'Accept-Encoding': 'gzip, deflate',
  24. }
  25. elif 'Chrome' in self.useragent:
  26. self.headers = {
  27. 'User-Agent': self.useragent,
  28. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  29. 'Accept-Language': 'en-US,en;q=0.9',
  30. 'Accept-Encoding': 'gzip, deflate',
  31. }
  32. # Setup logger
  33. self.logger = logging.getLogger('neobots.NeoAccount')
  34. # Load cookies or create a session
  35. Path('.cache').mkdir(exist_ok=True)
  36. if Path.is_file(self.cachefile):
  37. with open(self.cachefile, 'rb') as f:
  38. self.session = pickle.load(f)
  39. else:
  40. self.session = requests.Session()
  41. # Set headers
  42. self.session.headers = self.headers
  43. # Set proxy if exists
  44. if (self.proxy != ''):
  45. self.session.proxies = {'http': 'http://{}/'.format(self.proxy)}
  46. # GET
  47. def get(self, url, params={}, referer='', headers={}):
  48. if url[0] == '/':
  49. url = self.domain + url
  50. if referer != '':
  51. if referer[0] == '/':
  52. referer = self.domain + referer
  53. headers['Referer'] = referer
  54. result = self.session.get(url, params=params, headers=headers)
  55. return result
  56. # POST
  57. def post(self, url, data={}, params={}, referer='', headers={}):
  58. headers['Origin'] = self.domain
  59. if url[0] == '/':
  60. url = self.domain + url
  61. if referer != '':
  62. if referer[0] == '/':
  63. referer = self.domain + referer
  64. headers['Referer'] = referer
  65. result = self.session.post(url, params=params, data=data, headers=headers)
  66. return result
  67. # XHR
  68. def xhr(self, url, data={}, params={}, referer='', headers={}):
  69. headers['Origin'] = self.domain
  70. headers['X-Requested-With'] = 'XMLHttpRequest'
  71. headers['Accept'] = '*/*'
  72. if url[0] == '/':
  73. url = self.domain + url
  74. if referer != '':
  75. if referer[0] == '/':
  76. referer = self.domain + referer
  77. headers['Referer'] = referer
  78. result = self.session.post(url, params=params, data=data, headers=headers)
  79. return result
  80. # Login
  81. def login(self):
  82. self.logger.debug(self.username + ': Logging in')
  83. result = self.get('http://www.neopets.com/index.phtml')
  84. soup = bs(result.content, 'lxml')
  85. match = soup.select_one('.user.medText a')
  86. if match and match.get_text() == self.username:
  87. self.logger.info(self.username + ': Already logged in!')
  88. return True
  89. result = self.get(
  90. 'http://www.neopets.com/login/',
  91. referer='/index.phtml'
  92. )
  93. result = self.post(
  94. 'http://www.neopets.com/login.phtml',
  95. data={
  96. 'username': self.username,
  97. 'password': self.password,
  98. 'destination': 'http://www.neopets.com/index.phtml'
  99. },
  100. referer='/login/'
  101. )
  102. if 'badpassword' in result.url:
  103. self.logger.error(self.username + ': Bad Password!')
  104. return False
  105. elif 'hello' in result.url:
  106. self.logger.error(self.username + ': Birthday Locked!')
  107. return False
  108. elif 'login' in result.url:
  109. self.logger.error(self.username + ': Account Frozen!')
  110. return False
  111. elif 'index' in result.url:
  112. with open(self.cachefile, 'wb') as f:
  113. pickle.dump(self.session, f, pickle.HIGHEST_PROTOCOL)
  114. self.logger.info(self.username + ': Logged in!')
  115. return True