Created
March 13, 2015 18:02
-
-
Save sdague/4f22ae38b8a4048d8ceb to your computer and use it in GitHub Desktop.
Fastmail sync script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import difflib | |
import re | |
import bs4 | |
import mechanize | |
import yaml | |
cfg = yaml.load(open("account.yaml", 'r')) | |
LOGIN="https://www.fastmail.fm" | |
br = mechanize.Browser() | |
br.set_handle_robots(False) | |
# initial login | |
br.open(LOGIN) | |
br.select_form(nr=0) | |
br.form["username"] = cfg["user"] | |
br.form["password"] = cfg["pass"] | |
br.submit() | |
# now the sieve page | |
br.open(cfg["url"]) | |
br.select_form(nr=0) | |
#print br.form["FFIA-SieveSource"] | |
#print "*****************" | |
sieve_data = "" | |
with open ("rules.txt", "r") as myfile: | |
sieve_data=myfile.read() | |
br.form["FFIA-SieveSource"] = sieve_data | |
# the first button is the revert | |
br.submit(nr=1) | |
soup = bs4.BeautifulSoup(br.response().read()) | |
errors = soup.find('div', class_='errorMessage') | |
syntax = "" | |
if errors: | |
syntax = errors.find('pre').text | |
# Verify that things saved | |
br.open(cfg["url"]) | |
br.select_form(nr=0) | |
saved_rules = br.form["FFIA-SieveSource"] | |
saved_rules = saved_rules.replace("\r\n", "\n") | |
if saved_rules == sieve_data: | |
print "OK: Rules saved" | |
else: | |
print "ERROR saving rules: %s" % syntax | |
# then print out the diff | |
for line in difflib.context_diff( | |
saved_rules.split("\n"), | |
sieve_data.split("\n")): | |
print line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment