From 18faa5288c90c57973e8475167075bd0f8371e30 Mon Sep 17 00:00:00 2001 From: steffen123 Date: Wed, 7 Jul 2010 02:53:27 +0200 Subject: [PATCH] first steps to get PS tourney results from IMAP --- pyfpdb/ImapSummaries.py | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 pyfpdb/ImapSummaries.py diff --git a/pyfpdb/ImapSummaries.py b/pyfpdb/ImapSummaries.py new file mode 100755 index 00000000..e2620411 --- /dev/null +++ b/pyfpdb/ImapSummaries.py @@ -0,0 +1,60 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +#Copyright 2008-2010 Steffen Schaumburg +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU Affero General Public License as published by +#the Free Software Foundation, version 3 of the License. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU Affero General Public License +#along with this program. If not, see . +#In the "official" distribution you can find the license in agpl-3.0.txt. + +"""This file is to fetch summaries through IMAP and pass them on to the appropriate parser""" +#see http://docs.python.org/library/imaplib.html for the python interface +#see http://tools.ietf.org/html/rfc2060#section-6.4.4 for IMAP4 search criteria + +#TODO: move all these into config file +#TODO: This is currently for PS only. If anyone wants to expand IMAP +configHost="schaumburger.info" +configUser="fpdb-test@schaumburger.info" +import sys +configPw=sys.argv[1] + +from imaplib import IMAP4_SSL + +server = IMAP4_SSL(configHost) #TODO: optionally non-SSL +response = server.login(configUser, configPw) #TODO catch authentication error +#print "response to logging in:",response +#print "server.list():",server.list() #prints list of folders + +response = server.select("INBOX") +#print "response to selecting INBOX:",response +if response[0]!="OK": + raise error #TODO: show error message + +neededMessages=[] +response, searchData = server.search(None, "SUBJECT", "PokerStars Tournament History Request") +for messageNumber in searchData[0].split(" "): + response, headerData = server.fetch(messageNumber, "(BODY[HEADER.FIELDS (SUBJECT)])") + #print "response to fetch subject:",response + if response!="OK": + raise error #TODO: show error message + if headerData[1].find("Subject: PokerStars Tournament History Request - Last x")!=1: + neededMessages.append(messageNumber) + +if len(neededMessages)==0: + raise error #TODO: show error message +for messageNumber in neededMessages: + response, bodyData = server.fetch(messageNumber, "(UID BODY[TEXT])") + if response!="OK": + raise error #TODO: show error message + print "bodyData",bodyData[0][1] + +server.close() +server.logout()