Skip to content

Instantly share code, notes, and snippets.

@edwintorok
Created January 20, 2022 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwintorok/44363badc311587175ffba1d9498c039 to your computer and use it in GitHub Desktop.
Save edwintorok/44363badc311587175ffba1d9498c039 to your computer and use it in GitHub Desktop.
ml.py
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment