-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SpikeTheMaster/master
Add Amazon and Steam parsing changes
- Loading branch information
Showing
5 changed files
with
203 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
import re | ||
|
||
class AmazonEmailParser(object): | ||
|
||
def __init__(self): | ||
self.orderTotalRE = re.compile(r"(?<=Order Total:) (?:.*?)(\d+.\d+)") | ||
self.postageRE = re.compile(r"(?<=Postage & Packing:) (?:.*?)(\d+.\d+)") | ||
self.deliveryRE = re.compile(r"(?<=Delivery & Handling::) (?:.*?)(\d+.\d+)") | ||
self.orderItemsRE = re.compile(r"==========\r\n\r\n") | ||
self.costRE = re.compile(r"(\d+\.\d+)") | ||
|
||
def canParse(self, email): | ||
try: | ||
if 'auto-confirm@amazon' in email['from']: | ||
return True | ||
else: | ||
return False | ||
except: | ||
return False | ||
|
||
def parse(self, email): | ||
body = email['body'] | ||
|
||
if 'Order Confirmation' in body: | ||
postage = 0 | ||
orderTotal = 0 | ||
|
||
result = re.search(self.orderTotalRE, body) | ||
|
||
if result: | ||
orderTotal = float(result.groups()[0]) | ||
|
||
result = re.search(self.postageRE, body) | ||
|
||
if result: | ||
postage = float(result.groups()[0]) | ||
else: | ||
result = re.search(self.deliveryRE, body) | ||
if result: | ||
postage = float(result.groups()[0]) | ||
|
||
email['order_details'] = { | ||
"order_items" : [], | ||
"order_total" : orderTotal, | ||
"postage" : postage, | ||
"merchant" : "amazon" | ||
} | ||
|
||
orders = re.split(self.orderItemsRE, body)[1] | ||
orders = orders.split('\r\n\r\n') | ||
|
||
#Remove first and last 3 items | ||
orders.pop(0) | ||
orders.pop() | ||
orders.pop() | ||
orders.pop() | ||
|
||
costTotal = orderTotal | ||
|
||
for item in orders: | ||
if 'Your estimated delivery date is:' in item or 'Your order will be sent to:' in item: | ||
continue | ||
else: | ||
lines = item.replace('_','').split('\r\n') | ||
if len(lines) < 4: | ||
continue | ||
itemName = lines[0].strip() | ||
cost = float(re.search(self.costRE, lines[1].strip()).groups()[0]) | ||
condition = lines[2].rpartition(':')[2].strip() | ||
seller = lines[3].replace('Sold by', '').strip() | ||
|
||
email['order_details']['order_items'].append({"item":itemName, "cost":cost, "condition": condition, "seller": seller}) | ||
costTotal -= cost | ||
|
||
if costTotal != 0: | ||
print "Warning order not parsed correctly, order items may be missing, or promotion may have been applied." | ||
print email['order_details'] | ||
print body | ||
|
||
return email |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class DelegatingEmailParser(object): | ||
|
||
def __init__(self, parsers): | ||
self.parsers = parsers | ||
|
||
def parse(self, email): | ||
for parser in self.parsers: | ||
if parser.canParse(email): | ||
return parser.parse(email) | ||
|
||
return email |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
import re | ||
|
||
class SteamEmailParser(object): | ||
|
||
def __init__(self): | ||
self.orderTotalRE = re.compile(r"(?<=Total:)[ \t]+(\d+.\d+)") | ||
self.orderItemsRE = re.compile(r"(?:\.\r\n)+") | ||
self.costRE = re.compile(r"(\d+\.\d+)") | ||
|
||
def canParse(self, email): | ||
try: | ||
if 'noreply@steampowered.com' in email['from']: | ||
return True | ||
else: | ||
return False | ||
except: | ||
return False | ||
|
||
def parse(self, email): | ||
body = email['body'] | ||
|
||
if 'Thank you' in email['subject'] and 'purchase' in body: | ||
orderTotal = 0 | ||
|
||
result = re.search(self.orderTotalRE, body) | ||
|
||
if result: | ||
orderTotal = float(result.groups()[0]) | ||
|
||
email['order_details'] = { | ||
"order_items" : [], | ||
"order_total" : orderTotal, | ||
"merchant" : "steam" | ||
} | ||
|
||
order = re.split(self.orderItemsRE, body)[2].split('\r\n') #This parser to get order total is currently broken, gift purchases are not parsed | ||
|
||
costTotal = orderTotal | ||
|
||
costTotal = orderTotal | ||
|
||
for item in order: | ||
if '-------' in item: | ||
break | ||
else: | ||
if item == '' or ': ' not in item: | ||
continue | ||
splitResult = item.rpartition(':') | ||
itemName = splitResult[0].strip() | ||
cost = float(re.match(self.costRE, splitResult[2].strip()).groups()[0]) | ||
|
||
email['order_details']['order_items'].append({"item":itemName, "cost":cost}) | ||
costTotal -= cost | ||
|
||
if costTotal != 0: | ||
print "Warning order not parsed correctly, order items may be missing, or promotion may have been applied." | ||
print email['order_details'] | ||
print body | ||
|
||
return email |
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