#!/usr/bin/env python # -*- coding: utf8 -*- # Copyright 2007 hisitech, SLU or its licensors, as # applicable. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import quopri # quoted printable emails import getpass, poplib # simple pop3 handling # # Introduce la direccion de tu pop. Ej: pop.gmail.com # pop3url = 'pop.gmail.com' # # Introduce la cuenta de email. Ej: myemail # popuser = 'myemail' # # Introduce el password. Ej: mypasswd # poppass = 'mypassword' # # Convierte el formato email en la variable msg y extrae el SMS # en formato ('numero de movil', 'mensaje de text sms') # def lee_SMS(msg): for i in range(len(msg)): # Leemos Content-Type y encoding "al paso" if msg[i].startswith('Content-Type:'): try: ctype, encoding = map(str.strip, msg[i].split(':')[1].split(';')) encoding = encoding.split('=')[1].strip() except: encoding = 'ascii' # Encuentro el final de cabeceras (primera lĂ­nea en blanco) if not msg[i]: break #cabeceras #heads=msg[:i] #cuerpo body=msg[i+1:] # primer match numero = body[0].split(':',1) if len(numero)>1: numero = numero[1] mensaje = body[1].split(':',1) if len(mensaje)>1: mensaje = unicode(quopri.decodestring(mensaje[1]),'utf8') return numero, mensaje else: return None # no es un SMS def lee_mailbox(): M = poplib.POP3_SSL(pop3url) M.user(popuser) M.pass_(poppass) if verbose: print M.list()[0] print "###### SMS recibidos #######" msisdnlist = [] textolist = [] for mid, longitud in [i.split() for i in M.list()[1]]: mid = int(mid) ret, msg, size = M.retr(mid) msisdn, texto = lee_SMS(msg) if verbose: print mid,msisdn,texto msisdnlist.append(msisdn) textolist.append(texto) if verbose: print msisdnlist print textolist print "#############" return msisdnlist, textolist def borra_mailbox(): M = poplib.POP3_SSL(pop3url) M.user(popuser) M.pass_(poppass) print M.list()[0] ret, msg, size = M.retr(mid) for i in range(len(msg)): M.dele(i) M.quit() print M.list()[0] return None # # Main: Recuento de Votos # verbose = False msisdnlist, textolist = lee_mailbox() mlist = msisdnlist + [] tlist = textolist + [] mlist.sort() tlist.sort() print "####### Lista de Moviles y Numero de Votos emitidos #######" a = [] for i in mlist: if a != i: print i,"(",mlist.count(i),")" a = i print "####### Lista de Votos y recuento #######" a = [] b = 1 for i in tlist: if a != i: print b,i,"(",tlist.count(i),")" a = i b += 1 if b > 2: prompt ="Valor ganador? (1.."+ str(b-1) + "): " resultado = raw_input(prompt) if int(resultado) > 0 and int(resultado) < b: valresultado = tlist[int(resultado)-1] else: print "Solo hay ", b-1, " votos" print "####### Acertantes ######" if verbose: print msisdnlist print textolist b = 0 for i in textolist: if i == valresultado: print b+1,msisdnlist[int(b)] b += 1 # # Borrar Mailbox? # if "s" == raw_input("Quieres borrar el MailBox? (N/s):"): print "borrar" borra_mailbox()