Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. import requests
  2. import time
  3. import hashlib
  4. from bs4 import BeautifulSoup
  5. from .globals import *
  6.  
  7.  
  8. def log(text: str):
  9. # Debug
  10. with open('log.html', 'w') as f:
  11. f.write(text.replace('\x99', ''))
  12.  
  13.  
  14. class Account:
  15. headers = {
  16. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
  17. cookies = {}
  18.  
  19. def __init__(self, username: str, password: str, accountid: int):
  20. self.username = username
  21. self.password = password
  22. self.hashedPw = hashlib.md5(password.encode('utf-8')).hexdigest()
  23. self.accountid = accountid
  24.  
  25. self.session = requests.Session()
  26.  
  27. def login(self):
  28. '''
  29. Logs in to the account.
  30. Returns true or false based on whether it's successful or not.
  31. '''
  32. self._setCookies()
  33. data = {
  34. 'do': 'login',
  35. '__cfduid': self.cookies['cfduid'],
  36. 'bb_sessionhash': self.cookies['session_hash'],
  37. 'vb_login_username': self.username,
  38. 'vb_login_password': '',
  39. 'vb_login_password_hint': 'Password',
  40. 'cookieuser': '1',
  41. 's': '',
  42. 'securitytoken': 'guest',
  43. 'vb_login_md5password': self.hashedPw,
  44. 'vb_login_md5password_utf': self.hashedPw
  45. }
  46.  
  47. return True if 'Thank you for logging in' in self.session.post(URL_LOGIN, data,
  48. headers=self.headers).text else False
  49.  
  50. def updateSignature(self, text: str):
  51. '''
  52. Sends a post request to update the account's forum signature.
  53. '''
  54.  
  55. html = self.session.get(URL_SIGNATURE_GET, headers=self.headers).text
  56.  
  57. data = {
  58. 'do': 'updatesignature',
  59. '__cfduid': self.cookies['cfduid'],
  60. 'bb_userid': self.accountid,
  61. 'bb_password': self.hashedPw,
  62. 'bb_sessionhash': self.cookies['session_hash'],
  63. 'message_backup': text,
  64. 'message': text,
  65. 'wysiwyg': 0,
  66. 'url': 'http://www.gta-sarp.com/forums/usercp.php',
  67. 'securitytoken': self._getSecurityToken(html),
  68. 'MAX_FILE_SIZE': 2097152,
  69. 'sigpicurl': 'http://www.',
  70. 'upload': '',
  71. 'MAX_FILE_SIZE': 2097152,
  72. }
  73. self.session.post(URL_SIGNATURE_POST, data, headers=self.headers)
  74.  
  75. def checkNotifications(self):
  76. '''
  77. Returns the amount of notifications the account has
  78. in integer format
  79. '''
  80.  
  81. html = self.session.get(URL_HOMEPAGE).text
  82. soup = BeautifulSoup(html, 'html.parser')
  83.  
  84. amnt = None
  85. try:
  86. amnt = soup.find('span', {'class': 'notifications-number'}).get_text()
  87. except AttributeError:
  88. amnt = 0
  89. return int(amnt)
  90.  
  91. def addReputation(self, **kw):
  92. '''
  93. Adds a positive or negative reputation. type should be "pos" or "neg"
  94. type_:str, url:str, postid:int, reason:str
  95.  
  96. Needs:
  97. Post URL
  98. Post ID
  99. Reason
  100. Type (pos, neg)
  101. '''
  102.  
  103. pId = None
  104. if '#post' in kw['url']:
  105. pId = kw['url'].split('#post')[1]
  106.  
  107. html = self.session.get(kw['url'], headers=self.headers).text
  108.  
  109. data = {
  110. 'do': 'addreputation',
  111. 'p': pId,
  112. '__cfduid': self.cookies['cfduid'],
  113. 'bb_userid': self.accountid,
  114. 'bb_password': self.hashedPw,
  115. 'bb_sessionhash': self.cookies['session_hash'],
  116. 'securitytoken': self._getSecurityToken(html),
  117. 'ajax': 1,
  118. 'reputation': kw['type'],
  119. 'reason': kw['reason'],
  120. 'p': pId,
  121. 'url': kw['url'],
  122. }
  123. self.session.post(URL_REPUTATION_POST, data, headers=self.headers)
  124. return True
  125.  
  126. def sendVm(self, text: str, touser: str):
  127. '''
  128. Sends a VM to a specific user ID.
  129. '''
  130.  
  131. html = self.session.get(URL_MEMBER + str(touser)).text
  132.  
  133. data = {
  134. 'do': 'message',
  135. '__cfduid': self.cookies['cfduid'],
  136. 'bb_userid': self.accountid,
  137. 'bb_password': self.hashedPw,
  138. 'bb_sessionhash': self.cookies['session_hash'],
  139. 'ajax': 1,
  140. 'wysiwyg': 0,
  141. 'fromquickcomment': 1,
  142. 's': '',
  143. 'securitytoken': self._getSecurityToken(html),
  144. 'u': touser,
  145. 'u2': '',
  146. 'loggedinuser': self.accountid,
  147. 'parseurl': 1,
  148. 'allow_ajax_qc': 1,
  149. 'fromconverse': '',
  150. 'message_backup': text,
  151. 'message': text,
  152. '': text,
  153. }
  154.  
  155. self.session.post(URL_VM, data, headers=self.headers)
  156.  
  157. def react(self, url: str, type_: str):
  158. '''
  159. Reacts to a post. Varnames:
  160. likes
  161. hates
  162. WTFs
  163. thanks
  164. laugh
  165. '''
  166. if '#post' in url:
  167. pId = url.split('#post')[1]
  168. else:
  169. return
  170.  
  171. html = self.session.get(url, headers=self.headers).text
  172. data = {
  173. '__cfduid': self.cookies['cfduid'],
  174. 'bb_userid': self.accountid,
  175. 'bb_password': self.hashedPw,
  176. 'bb_sessionhash': self.cookies['session_hash'],
  177. 'securitytoken': self._getSecurityToken(html),
  178. 'do': 'ajax',
  179. 'action': 'entry',
  180. 'varname': type_,
  181. 'p': pId,
  182. }
  183.  
  184. self.session.post(URL_REACT_POST, data, headers=self.headers)
  185.  
  186. def sendPm(self, **kw):
  187. # not working
  188.  
  189. html = self.session.get(URL_SENDPM, headers=self.headers).text
  190. data = {
  191. 'do': 'insertpm',
  192. 'pmid': '',
  193. '__cfduid': self.cookies['cfduid'],
  194. 'bb_userid': self.accountid,
  195. 'bb_password': self.hashedPw,
  196. 'bb_sessionhash': self.cookies['session_hash'],
  197. 'recipients': '; '.join(kw['to']),
  198. 'title': kw['subject'],
  199. 'message_backup': kw['message'],
  200. 'message': kw['message'],
  201. 'securitytoken': self._getSecurityToken(html),
  202. 'pmid': '',
  203. 'forward': '',
  204. 'sbutton': 'Submit+Message',
  205. 'savecopy': 1,
  206. 'parseurl': 1,
  207. }
  208.  
  209. print(data)
  210.  
  211. self.session.post(URL_SENDPM, data=data, headers=self.headers)
  212.  
  213. def postFc(self, **kw):
  214. '''
  215. - Posts a forum complaint. Needed:
  216. - rule_breakers
  217. - rules_broken
  218. - evidence
  219. - level
  220. '''
  221. self._setCookies(URL_FC)
  222. html = self.session.get(URL_FC, headers=self.headers).text
  223.  
  224. data = {
  225. 'do': 'form',
  226. 'fid': 107,
  227. '__cfduid': self.cookies['cfduid'],
  228. 'bb_userid': self.accountid,
  229. 'bb_password': self.hashedPw,
  230. 'bb_cpsession': self.cookies['cpsession'],
  231. 'bb_sessionhash': self.cookies['session_hash'],
  232. '1176': 'FC_BOT',
  233. '1177[Bday]': '01',
  234. '1177[Bmonth]': '1',
  235. '1177[Byear]': '2017',
  236. '1178': kw['rule_breakers'],
  237. '1179': kw['rules_broken'],
  238. '1180': kw['evidence'],
  239. '1181': kw['level'],
  240. 'sbutton': 'Submit',
  241. 'do': 'postform',
  242. 'fid': 107,
  243. 's': '',
  244. 'posthash': self._getPostHash(html),
  245. 'poststarttime': self._getPostStartTime(html),
  246. 'embed': 0,
  247. 'hiddenfield1': '',
  248. 'hiddenfield2': '',
  249. 'hiddenfield3': '',
  250. 'userid': self.accountid,
  251. 'username': self.username,
  252. 'securitytoken': self._getSecurityToken(html),
  253. }
  254.  
  255. self.session.post(URL_FORMS, data=data, headers=self.headers)
  256.  
  257. def _getSecurityToken(self, html: str):
  258. _soup = BeautifulSoup(html, 'html.parser')
  259. return _soup.find('input', {'name': 'securitytoken'})['value']
  260.  
  261. def _getPostHash(self, html: str):
  262. _soup = BeautifulSoup(html, 'html.parser')
  263. return _soup.find('input', {'name': 'posthash'})['value']
  264.  
  265. def _getPostStartTime(self, html: str):
  266. _soup = BeautifulSoup(html, 'html.parser')
  267. return _soup.find('input', {'name': 'poststarttime'})['value']
  268.  
  269. def _setCookies(self, url: str = URL_HOMEPAGE):
  270. self.session.get(url)
  271. self.cookies['cfduid'] = self.session.cookies.get('__cfduid')
  272. self.cookies['session_hash'] = self.session.cookies.get('bb_sessionhash')
  273. self.cookies['cpsession'] = self.session.cookies.get('bb_cpsession')
  274.  
  275.  
  276. ###########################################
  277. #
  278. # Login - Account.login()
  279. # Change Signature - Account.updateSignature()
  280. # Check for notification - Account.checkNotification()
  281. # Add reputation - Account.addReputation()
  282. # Send VMs - Account.sendVm()
  283. #
  284. # TODO:
  285. # - Send PMs
  286. # - Like / Hate / Laugh etc. at posts
  287. # - Reply to threads
  288. ###########################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement