Last active
November 17, 2016 04:01
-
-
Save FirePanther/9af354a5dcaaefe13060 to your computer and use it in GitHub Desktop.
deprecated
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
function parseBanks() { | |
var calendar = CalendarApp.getCalendarsByName('[a] Banks')[0], | |
threads = GmailApp.search('-in:Trash AND ('+ | |
'from:infoservices@information.deutsche-bank.de'+ | |
' OR (to:number26@suat.be AND "You received a transfer of")'+ | |
' OR (to:number26@suat.be AND "Your payment of")'+ | |
' OR (to:number26@suat.be AND "CASH26 Withdraw")'+ | |
' OR (to:number26@suat.be AND "Direct Debit collected")'+ | |
' OR (to:number26@suat.be AND "Refund successful")'+ | |
' OR (to:number26@suat.be AND "Transfer received")'+ | |
')'), | |
i, msg, body, match, mdate, date, person, created; | |
Logger.log("Threads: " + threads.length); | |
for (var x in threads) { | |
Logger.log("Found thread: " + threads[x].getFirstMessageSubject()); | |
msg = threads[x].getMessages(); | |
for (i = 0; i < msg.length; i++) { | |
if (msg[i].isInTrash()) continue; | |
body = msg[i].getBody().replace(/\s+/g, ' '); | |
created = false; | |
mdate = msg[i].getDate(); | |
if (match = body.match(/es wurde eine Abbuchung in Höhe von ([\d,]+) (\w+) am (.*?) von/)) { | |
Logger.log("Match: Deutsche Bank Payment"); | |
money = parseMoney(match[1]); | |
currency = parseCurrency(match[2]); | |
date = parseDate(match[3]); | |
if (date !== false) { | |
if (date.getFullYear() == mdate.getFullYear() && date.getMonth() == mdate.getMonth() && date.getDate() == mdate.getDate()) { | |
date = mdate; | |
} else { | |
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 9, 0); | |
} | |
calendar.createEvent("-"+money+currency+" on DBank", date, date); | |
created = true; | |
} | |
} else if (match = body.match(/es wurde eine Gutschrift in Höhe von ([\d,]+) (\w+) am (.*?) auf/)) { | |
Logger.log("Match: Deutsche Bank Receipt"); | |
money = parseMoney(match[1]); | |
currency = parseCurrency(match[2]); | |
date = parseDate(match[3]); | |
if (date !== false) { | |
if (date.getFullYear() == mdate.getFullYear() && date.getMonth() == mdate.getMonth() && date.getDate() == mdate.getDate()) { | |
date = mdate; | |
} else { | |
date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 9, 0); | |
} | |
if (money != "0.00") calendar.createEvent("+"+money+currency+" on DBank", date, date); | |
created = true; | |
} | |
} else if (match = body.match(/You received a transfer of (.{1,3}) ([\d,.]+) from (.*?) at (\d+):(\d+)./)) { | |
Logger.log("Match: Number26 Receipt"); | |
money = parseMoney(match[2]); | |
currency = parseCurrency(match[1]); | |
date = new Date(mdate.getFullYear(), mdate.getMonth(), mdate.getDate() - (parseInt(match[4]) > mdate.getHours() + 12 ? 1 : 0), match[4], match[5]); | |
person = match[3]; | |
calendar.createEvent("+"+money+currency+" from "+person+" on N26", date, date); | |
created = true; | |
} else if (match = body.match(/Your payment of (.{1,3}) ([\d,.]+) to (.*?) was successfully processed at (\d+):(\d+)./)) { | |
Logger.log("Match: Number26 Payment"); | |
money = parseMoney(match[2]); | |
currency = parseCurrency(match[1]); | |
date = new Date(mdate.getFullYear(), mdate.getMonth(), mdate.getDate() - (parseInt(match[4]) > mdate.getHours() + 12 ? 1 : 0), match[4], match[5]); | |
person = match[3]; | |
calendar.createEvent("-"+money+currency+" to "+person+" on N26", date, date); | |
created = true; | |
} else if (match = body.match(/You have withdrawn (.{1,3}) ([\d,.]+) at (\d+):(\d+) with CASH26/)) { | |
Logger.log("Match: Number26 Cash26 Withdraw"); | |
money = parseMoney(match[2]); | |
currency = parseCurrency(match[1]); | |
date = new Date(mdate.getFullYear(), mdate.getMonth(), mdate.getDate() - (parseInt(match[3]) > mdate.getHours() + 12 ? 1 : 0), match[3], match[4]); | |
calendar.createEvent("-"+money+currency+" via CASH26 on N26", date, date); | |
created = true; | |
} else if (match = body.match(/Your direct debit payment of (.{1,3}) ([\d,.]+) was collected by (.*?) at (\d+):(\d+)./)) { | |
Logger.log("Match: Number26 direct debit"); | |
money = parseMoney(match[2]); | |
currency = parseCurrency(match[1]); | |
date = new Date(mdate.getFullYear(), mdate.getMonth(), mdate.getDate() - (parseInt(match[4]) > mdate.getHours() + 12 ? 1 : 0), match[4], match[5]); | |
person = match[3]; | |
calendar.createEvent("-"+money+currency+" to "+person+" direct debit on N26", date, date); | |
created = true; | |
} else if (match = body.match(/Your refund of (.{1,3}) ([\d,.]+) from (.*?) was successfully processed at (\d+):(\d+)./)) { | |
Logger.log("Match: Number26 refund"); | |
money = parseMoney(match[2]); | |
currency = parseCurrency(match[1]); | |
date = new Date(mdate.getFullYear(), mdate.getMonth(), mdate.getDate() - (parseInt(match[4]) > mdate.getHours() + 12 ? 1 : 0), match[4], match[5]); | |
person = match[3]; | |
calendar.createEvent("+"+money+currency+" by "+person+" refunded on N26", date, date); | |
created = true; | |
} else if (msg[i].getSubject().match(/Payment declined/)) { | |
created = true; | |
} else { | |
Logger.log("No matches:\n-------\n"+body+"\n-------"); | |
} | |
if (created) { | |
Logger.log("-- created"); | |
msg[i].markRead().moveToTrash(); | |
} | |
} | |
} | |
} | |
function parseMoney(s) { | |
return s.replace(',', '.').replace(/\s/g, ''); | |
} | |
function parseCurrency(s) { | |
s = s.toLowerCase().substr(0, 3); | |
if (s == "$" || s == "usd" || s == "dol") return "$"; | |
return "€"; | |
} | |
function parseDate(s) { | |
var months = [ 'jan', 'feb', 'mär', 'apr', 'mai', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dez' ], | |
match, month = -1; | |
if (match = s.match(/^(\d+)\.?\s*(\w+)\s*(\d+)$/)) { | |
var day = match[1], | |
monthStr = match[2].toLowerCase().substr(0, 3), | |
year = match[3]; | |
for (var i = 0; i < months.length; i++) { | |
if (monthStr == months[i]) { | |
month = i; | |
break; | |
} | |
} | |
if (month != -1) { | |
return new Date(year, month, day); | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bluefirex, google apps script!