#!/usr/bin/env python3 import browser_cookie3 import requests import logging logging.basicConfig(level=logging.DEBUG) logging.debug("About to load Firefox cookiejar") BASE = "https://discuss.ocaml.org" cookiejar = browser_cookie3.firefox(domain_name="discuss.ocaml.org") logging.debug("Got cookies: %r" % ([c.name for c in cookiejar])) s = requests.Session() s.cookies = cookiejar reply = s.get("%s/session/csrf" % BASE, headers={"X-Requested-With": "XMLHttpRequest"}) csrf = reply.json()['csrf'] logging.debug("Got CSRF token") username = reply.headers["x-discourse-username"] logging.info("Got username %s" % username) data = { "mailing_list_mode": "true", # "mailing_list_mode": "true", "mailing_list_mode_frequency": "1", "email_digests": "true", "email_in_reply_to": "true", "email_messages_level": "0", "email_level": "0", "email_previous_replies": "1", "digest_after_minutes": "10080", "include_tl0_in_digests": "true", } logging.info("About to set mailing list mode to %s" % data["mailing_list_mode"]) headers = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", "X-CSRF-Token": csrf, } reply = s.put("%s/u/%s.json" % (BASE, username), data=data, headers=headers) json = reply.json() if 'success' in json: logging.info(json['success']) else: logging.error(json)