make the form submission work, and add a temp hack for csrf until 0.2.0 jschan

dev
Thomas Lynch 2 years ago
parent 9fe2e61afa
commit 9b0fc04749
  1. 6
      notification_button.py
  2. 33
      session.py

@ -1,4 +1,5 @@
import logging import logging
import subprocess
import sys, getopt import sys, getopt
from config import config from config import config
@ -21,8 +22,9 @@ def main(argv):
session.update_csrf() session.update_csrf()
#todo: for board, postid and action args, send the request res = session.post_actions(board=optdict['-b'], postid=optdict['-p'], actions=optdict['-a'])
session.post_actions(board=optdict['-b'], postid=optdict['-p'], actions=optdict['-a']) if 'message' in res:
subprocess.call(['termux-toast', res['message']])
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])

@ -36,21 +36,34 @@ class ModSession(Session):
def update_csrf(self): def update_csrf(self):
try: try:
res = self.get(url=f'{self.imageboard_url}/csrf.json',
headers={'Referer': f'{self.imageboard_url}/csrf.json'}).json() # res = self.get(url=f'{self.imageboard_url}/csrf.json',
if 'token' in res: # headers={'Referer': f'{self.imageboard_url}/csrf.json'}).json()
self.csrf_token = res['token'] # if 'token' in res:
else: # self.csrf_token = res['token']
raise Exception('Unable to update csrf token') # else:
# raise Exception('Unable to update csrf token')
#temporary hack to get csrf token to test on <=0.1.10
res2 = self.get(url=f'{self.imageboard_url}/account.html',
headers={'Referer': f'{self.imageboard_url}/account.html'})
csrfi = res2.text.index('_csrf')
self.csrf_token = res2.text[csrfi+14:csrfi+50]
except requests.RequestException as e: except requests.RequestException as e:
logging.error(f'Exception {e} occurred while updating csrf token') logging.error(f'Exception {e} occurred while updating csrf token')
raise Exception('Unable to update csrf token') raise Exception('Unable to update csrf token')
def post_actions(self, **kwargs): def post_actions(self, **kwargs):
try: try:
raise Exception('not implemented') actions = kwargs['actions'].split(',')
# res = self.post(url=f'{self.imageboard_url}/forms/board/{kwargs["board"]}/modactions', body = {'checkedposts':kwargs["postid"],'_csrf':self.csrf_token,'log_message':'globalafk'}
# headers={'Referer': f'{self.imageboard_url}/forms/board/{kwargs["board"]}/modactions'}).json() for action in actions:
body[action] = '1'
res = self.post(url=f'{self.imageboard_url}/forms/board/{kwargs["board"]}/modactions',
headers={'Referer': f'{self.imageboard_url}/forms/board/{kwargs["board"]}/modactions','x-using-xhr': 'true'},
data=body).json()
return res
except requests.RequestException as e: except requests.RequestException as e:
logging.error(f'Exception {e} occurred while authenticating moderator') logging.error(f'Exception {e} occurred while posting action')
raise Exception('Failed to submit post actions') raise Exception('Failed to submit post actions')

Loading…
Cancel
Save