Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import time
- import hashlib
- from bs4 import BeautifulSoup
- from .globals import *
- def log(text: str):
- # Debug
- with open('log.html', 'w') as f:
- f.write(text.replace('\x99', ''))
- class Account:
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
- cookies = {}
- def __init__(self, username: str, password: str, accountid: int):
- self.username = username
- self.password = password
- self.hashedPw = hashlib.md5(password.encode('utf-8')).hexdigest()
- self.accountid = accountid
- self.session = requests.Session()
- def login(self):
- '''
- Logs in to the account.
- Returns true or false based on whether it's successful or not.
- '''
- self._setCookies()
- data = {
- 'do': 'login',
- '__cfduid': self.cookies['cfduid'],
- 'bb_sessionhash': self.cookies['session_hash'],
- 'vb_login_username': self.username,
- 'vb_login_password': '',
- 'vb_login_password_hint': 'Password',
- 'cookieuser': '1',
- 's': '',
- 'securitytoken': 'guest',
- 'vb_login_md5password': self.hashedPw,
- 'vb_login_md5password_utf': self.hashedPw
- }
- return True if 'Thank you for logging in' in self.session.post(URL_LOGIN, data,
- headers=self.headers).text else False
- def updateSignature(self, text: str):
- '''
- Sends a post request to update the account's forum signature.
- '''
- html = self.session.get(URL_SIGNATURE_GET, headers=self.headers).text
- data = {
- 'do': 'updatesignature',
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_sessionhash': self.cookies['session_hash'],
- 'message_backup': text,
- 'message': text,
- 'wysiwyg': 0,
- 'url': 'http://www.gta-sarp.com/forums/usercp.php',
- 'securitytoken': self._getSecurityToken(html),
- 'MAX_FILE_SIZE': 2097152,
- 'sigpicurl': 'http://www.',
- 'upload': '',
- 'MAX_FILE_SIZE': 2097152,
- }
- self.session.post(URL_SIGNATURE_POST, data, headers=self.headers)
- def checkNotifications(self):
- '''
- Returns the amount of notifications the account has
- in integer format
- '''
- html = self.session.get(URL_HOMEPAGE).text
- soup = BeautifulSoup(html, 'html.parser')
- amnt = None
- try:
- amnt = soup.find('span', {'class': 'notifications-number'}).get_text()
- except AttributeError:
- amnt = 0
- return int(amnt)
- def addReputation(self, **kw):
- '''
- Adds a positive or negative reputation. type should be "pos" or "neg"
- type_:str, url:str, postid:int, reason:str
- Needs:
- Post URL
- Post ID
- Reason
- Type (pos, neg)
- '''
- pId = None
- if '#post' in kw['url']:
- pId = kw['url'].split('#post')[1]
- html = self.session.get(kw['url'], headers=self.headers).text
- data = {
- 'do': 'addreputation',
- 'p': pId,
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_sessionhash': self.cookies['session_hash'],
- 'securitytoken': self._getSecurityToken(html),
- 'ajax': 1,
- 'reputation': kw['type'],
- 'reason': kw['reason'],
- 'p': pId,
- 'url': kw['url'],
- }
- self.session.post(URL_REPUTATION_POST, data, headers=self.headers)
- return True
- def sendVm(self, text: str, touser: str):
- '''
- Sends a VM to a specific user ID.
- '''
- html = self.session.get(URL_MEMBER + str(touser)).text
- data = {
- 'do': 'message',
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_sessionhash': self.cookies['session_hash'],
- 'ajax': 1,
- 'wysiwyg': 0,
- 'fromquickcomment': 1,
- 's': '',
- 'securitytoken': self._getSecurityToken(html),
- 'u': touser,
- 'u2': '',
- 'loggedinuser': self.accountid,
- 'parseurl': 1,
- 'allow_ajax_qc': 1,
- 'fromconverse': '',
- 'message_backup': text,
- 'message': text,
- '': text,
- }
- self.session.post(URL_VM, data, headers=self.headers)
- def react(self, url: str, type_: str):
- '''
- Reacts to a post. Varnames:
- likes
- hates
- WTFs
- thanks
- laugh
- '''
- if '#post' in url:
- pId = url.split('#post')[1]
- else:
- return
- html = self.session.get(url, headers=self.headers).text
- data = {
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_sessionhash': self.cookies['session_hash'],
- 'securitytoken': self._getSecurityToken(html),
- 'do': 'ajax',
- 'action': 'entry',
- 'varname': type_,
- 'p': pId,
- }
- self.session.post(URL_REACT_POST, data, headers=self.headers)
- def sendPm(self, **kw):
- # not working
- html = self.session.get(URL_SENDPM, headers=self.headers).text
- data = {
- 'do': 'insertpm',
- 'pmid': '',
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_sessionhash': self.cookies['session_hash'],
- 'recipients': '; '.join(kw['to']),
- 'title': kw['subject'],
- 'message_backup': kw['message'],
- 'message': kw['message'],
- 'securitytoken': self._getSecurityToken(html),
- 'pmid': '',
- 'forward': '',
- 'sbutton': 'Submit+Message',
- 'savecopy': 1,
- 'parseurl': 1,
- }
- print(data)
- self.session.post(URL_SENDPM, data=data, headers=self.headers)
- def postFc(self, **kw):
- '''
- - Posts a forum complaint. Needed:
- - rule_breakers
- - rules_broken
- - evidence
- - level
- '''
- self._setCookies(URL_FC)
- html = self.session.get(URL_FC, headers=self.headers).text
- data = {
- 'do': 'form',
- 'fid': 107,
- '__cfduid': self.cookies['cfduid'],
- 'bb_userid': self.accountid,
- 'bb_password': self.hashedPw,
- 'bb_cpsession': self.cookies['cpsession'],
- 'bb_sessionhash': self.cookies['session_hash'],
- '1176': 'FC_BOT',
- '1177[Bday]': '01',
- '1177[Bmonth]': '1',
- '1177[Byear]': '2017',
- '1178': kw['rule_breakers'],
- '1179': kw['rules_broken'],
- '1180': kw['evidence'],
- '1181': kw['level'],
- 'sbutton': 'Submit',
- 'do': 'postform',
- 'fid': 107,
- 's': '',
- 'posthash': self._getPostHash(html),
- 'poststarttime': self._getPostStartTime(html),
- 'embed': 0,
- 'hiddenfield1': '',
- 'hiddenfield2': '',
- 'hiddenfield3': '',
- 'userid': self.accountid,
- 'username': self.username,
- 'securitytoken': self._getSecurityToken(html),
- }
- self.session.post(URL_FORMS, data=data, headers=self.headers)
- def _getSecurityToken(self, html: str):
- _soup = BeautifulSoup(html, 'html.parser')
- return _soup.find('input', {'name': 'securitytoken'})['value']
- def _getPostHash(self, html: str):
- _soup = BeautifulSoup(html, 'html.parser')
- return _soup.find('input', {'name': 'posthash'})['value']
- def _getPostStartTime(self, html: str):
- _soup = BeautifulSoup(html, 'html.parser')
- return _soup.find('input', {'name': 'poststarttime'})['value']
- def _setCookies(self, url: str = URL_HOMEPAGE):
- self.session.get(url)
- self.cookies['cfduid'] = self.session.cookies.get('__cfduid')
- self.cookies['session_hash'] = self.session.cookies.get('bb_sessionhash')
- self.cookies['cpsession'] = self.session.cookies.get('bb_cpsession')
- ###########################################
- #
- # Login - Account.login()
- # Change Signature - Account.updateSignature()
- # Check for notification - Account.checkNotification()
- # Add reputation - Account.addReputation()
- # Send VMs - Account.sendVm()
- #
- # TODO:
- # - Send PMs
- # - Like / Hate / Laugh etc. at posts
- # - Reply to threads
- ###########################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement