Saturday, October 15, 2011

മറുമൊഴിയുടെ കോഡ്

രണ്ടു ഫയലുകളുണ്ട്. reader.py ഉം‌‌ sender.py ഉം‌‌. രണ്ടും‌‌‌‌ ക്രോണ്‍‌‌‌‌ജോബ് ആയി റണ്‍‌‌‌‌ചെയ്യുകയായിരുന്നു.
വെബ്സൈറ്റ് ഹോസ്റ്റ് ചെയ്യുന്ന കമ്പനികള്‍‌‌‌‌ പൈത്തണ്‍‌‌‌‌ സ്ക്രിപ്റ്റും‌‌‌‌ സപ്പോര്‍‌‌‌‌ട്ട് ചെയ്യുന്നുണ്ടെങ്കില്‍‌‌‌‌ സെര്‍‌‌വറില്‍‌‌ രണ്ട് ക്രോണ്‍‌‌ജോബ് ആഡ് ചെയ്താല്‍ മതി.

ഇത് ഏറ്റവും‌‌‌‌ അവസാനത്തെ കോഡാണ്. ആദ്യം‌‌‌‌ VC++ ഇല്‍‌‌‌‌ എഴുതി. പിന്നെ ഉപയോഗിച്ചിരുന്ന ഒരു തേഡ് പാര്‍‌‌ട്ടി ലൈബ്രറി പണി മുടക്കിയപ്പോള്‍‌‌‌‌. ജാവയിലേക്ക് മാറി. പിന്നെ വെബ്‌സൈറ്റ് ഹോസ്റ്റ് ചെയ്യുന്ന സെര്വറില്‍ നിന്നു റണ്‍‌‌‌‌ ചെയ്യിക്കാനായി പൈത്തണ്‍‌‌‌‌ സ്ക്രിപ്റ്റാക്കി.

ആര്‍‌‌‌‌ക്കെങ്കിലും‌‌‌‌ ഉപയോഗപ്പെടുമെങ്കില്‍‌‌‌‌ ഉപയോഗിക്കുക.

ഫയലുകളുടെ ഡൗണ്‍‌‌‌‌ലോഡ് ലിങ്ക് -
1. reader.py
2. sender.py



#!/usr/bin/env python
#reader.py
#written by kuthiravattan

import sys, datetime
import getpass, poplib, random, email, os

class MailReader:
def __init__(self, userid, password):
self.userid = userid
self.password = password
self.filters = []
self.mailItem = None
self.msgPayload = None
self.startTime = datetime.datetime.utcnow()
self.processedMails = 0
self.curDir = sys.path[0]

def loadFilters(self):
self.filters.append("qw_er_ty")
filterFileName = self.curDir + "/" + 'filters.txt'
if(os.path.isfile(filterFileName) == False): return
filterFile = open(filterFileName,'r')
if(filterFile == None): return
content = filterFile.read()
filterFile.close()
lines = content.split('\n')
for i in range(len(lines)):
lines[i] = lines[i].strip()
if(len(lines[i])>0): self.filters.append(lines[i])

def readMails(self):
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
if(Mailbox == None): return
Mailbox.user(self.userid)
Mailbox.pass_(self.password)
messageCount = len(Mailbox.list()[1])
try:
for i in range(messageCount):
self.mailItem = Mailbox.retr(i+1)
self.saveMail()
self.processedMails += 1
except Exception:
print("Exception while reading mails")
Mailbox.quit()

def saveMail(self):
if(self.isMailOk() == True):
self.saveMailToSent()
else:
self.saveMailToJunk()

def isMailOk(self):
self.msgPayload = None
ret = True
if( (self.mailItem == None) or (self.mailItem[1] == None) ): return False
emailMsg = email.message_from_string('\n'.join(self.mailItem[1]))
if(emailMsg == None): return False
authresult = emailMsg.get('Authentication-Results')
if((authresult == None) or
(authresult.find('blogger.bounces.google.com designates') == -1) or
(authresult.find('as permitted sender') == -1)):
ret = False
if emailMsg.is_multipart():
part = emailMsg.get_payload(i=1)
if(part == None): return False
self.msgPayload = part.get_payload(decode=True)
contenttype = part.get_content_type()
if(contenttype == "text/html"):
if((ret == True) and
( self.isHitOnFilter() or self.isHitOnIntegrityCheck())):
ret = False
else:
ret = False
else:
self.msgPayload = emailMsg.get_payload(decode=True)
ret = False
return ret

def isHitOnFilter(self):
if(self.msgPayload == None): return True
for i in range(len(self.filters)):
filter = self.filters[i]
if(self.msgPayload.find(filter) != -1): return True
return False

def isHitOnIntegrityCheck(self):
if(self.msgPayload == None): return True
msgLower = self.msgPayload.lower()
hrefCount = msgLower.count("href")
if(hrefCount > 8): return True
lineCount = msgLower.count("br")
if(lineCount > 20): return True
if( (lineCount > 8) and ( (len(msgLower) / lineCount) < 70 ) ): return True
return False


def saveMailToJunk(self):
saveFolderName = "invalid"
self.saveMailToFolder(saveFolderName)

def saveMailToSent(self):
saveFolderName = "received"
self.saveMailToFolder(saveFolderName)

def saveMailToFolder(self,folderName):
if(self.msgPayload == None): return
mailFileName = self.startTime.strftime("%j_%H_%M_%S")
mailFileName += "_" + str(self.processedMails) + ".txt"
mailFileFullPath = self.curDir + "/" + folderName + "/" + mailFileName
mailFile = open(mailFileFullPath,'w')
if(mailFile == None): return
mailFile.write(self.msgPayload)
mailFile.close()
self.msgPayload = None

def createFolders(self):
self.createFolder("invalid")
self.createFolder("received")

def createFolder(self, folderName):
dirName = self.curDir + "/" + folderName
if(os.path.isdir(dirName) == False):
os.makedirs(dirName)

def doWork(self):
self.createFolders()
self.loadFilters()
self.readMails()

def main():
reader = MailReader("marumozhikal@gmail.com","**************************")
reader.doWork()

if __name__ == "__main__":
main()




#!/usr/bin/env python
#sender.py
#Written by Kuthiravattan

import smtplib, random
import sys, datetime, os, shutil

MSGHEADER = """From: Marumozhi <%(username)s>
To: Marumozhi <%(recepient)s>
MIME-Version: 1.0
Content-type: text/html
Subject: [Marumozhi - %(strcount)s Coments - %(commentid)s]

"""

class BulkMessage:
def __init__(self,userid,password,recepient):
self.userid = userid
self.password = password
self.recepient = recepient
self.content = ""
self.mailCount = 0
self.mailFileNames = []
self.maxMessageSize = 1000 * 100
self.msgSize = 0
self.msgHeader = ""
self.curDir = sys.path[0]
self.commentid = "tempid"

def prepareMsgHeader(self):
nowtime = datetime.datetime.utcnow()
self.commentid = nowtime.strftime("%y_%j_%H:%M:%S_") + str(random.randint(100,1000))
self.msgHeader = MSGHEADER % {'username': self.userid,
'recepient': self.recepient,'strcount': str(self.mailCount),
'commentid': self.commentid}

def appendContent(self,content):
self.msgSize += len(content)
self.content += "" + str(self.mailCount) + ". "
self.content += content
self.content += "
"


def appendFileContent(self,fileName):
fName = os.path.join(self.curDir,"received",fileName)
self.mailCount += 1
if(os.path.isfile(fName) == False): return
contentFile = open(fName,'r')
if(contentFile == None): return
content = contentFile.read()
contentFile.close()
self.appendContent(content)

def deleteFile(self,fileName):
try:
source = os.path.join(self.curDir,"received",fileName)
os.remove(source)
except Exception:
print("Not able to delete the file " + source)


def saveContent(self):
fileName = self.commentid + ".txt"
fileName = fileName.replace(":","_")
target = os.path.join(self.curDir,"saved",fileName)
fileToSave = open(target,'w')
if(fileToSave == None): return
fileToSave.write(self.content)
fileToSave.close()

def createFolders(self):
self.createFolder("saved")

def createFolder(self, folderName):
dirName = self.curDir + "/" + folderName
if(os.path.isdir(dirName) == False):
os.makedirs(dirName)


def sendMail(self):
ret = False
try:
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(self.userid, self.password)
self.prepareMsgHeader()
mailServer.sendmail(self.userid, self.recepient, self.msgHeader + self.content)
mailServer.close()
ret = True
except Exception:
ret = False
return ret

def getFileNames(self):
mailFolder = os.path.join(self.curDir,"received")
if(os.path.isdir(mailFolder) == False): return
self.mailFileNames = os.listdir(mailFolder)

def sendBulkMail(self):
self.createFolders()
self.getFileNames()
for i in range(len(self.mailFileNames)):
fileName = self.mailFileNames[i]
self.appendFileContent(fileName)
if(self.msgSize > self.maxMessageSize):
break
if(self.sendMail() == True):
for j in range(i+1):
self.deleteFile(self.mailFileNames[j])
self.saveContent()
self.content = ""
self.msgHeader = ""
self.msgSize = 0

def doWork(self):
self.sendBulkMail()


def main():
msg = BulkMessage("velappi1@gmail.com","*****************","marumozhikal@googlegroups.com")
msg.doWork()

if __name__ == "__main__":
main()