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()

Monday, September 26, 2011

Saturday, January 15, 2011

Wednesday, January 5, 2011

എറൂളാന്‍‌‌‌‌‌‌



കോഴിക്കുഞ്ഞുങ്ങളെ റാഞ്ചാന്‍ വരാറുള്ള എറൂളാന്‍ എന്നറിയപ്പെടുന്ന (വേറേയും പേരുകളുണ്ടോ) പക്ഷി. ഒരു ഓന്തിനെ റാഞ്ചാനുള്ള വിഫലശ്രമത്തിനു ശേഷം വിഷമിച്ചിരിക്കുന്നു.

Saturday, January 1, 2011

ഇരട്ടത്തലച്ചി (തൊപ്പിക്കിളി)




അങ്ങനെ ബ്ലാക്ക് ആണ്‍ഡ് വൈറ്റ് ലോകത്തില്‍ നിന്നും കളറിലേക്ക്(നാട്ടിലെത്തി).

Sunday, September 5, 2010

Ubuntu & Bamboo Pen

Every time after a kernel update received from repo, the bamboo pen stops working. This is because the .ko file is copied to kernel specific directory(ex: /lib/modules/2.6.32-24-generic/kernel/drivers/input/tablet/), and for new kernel I have to copy this to new location. I used to fix the issue by recompiling wacom.

Copying the steps from this site.

Download the latest linuxwacom driver
wget http://prdownloads.sourceforge.net/linuxwacom/linuxwacom-0.8.6.tar.bz2

Now unpack, configure compile and install it:
tar -xf linuxwacom-0.8.6.tar.bz2
cd linuxwacom-0.8.6
./configure --enable-wacom
cd src/2.6.30/
make
sudo cp wacom.ko /lib/modules/`uname -r`/kernel/drivers/input/tablet/
sudo rmmod wacom
sudo modprobe wacom

Friday, December 11, 2009

പണ്ടു വന്ന ചില വാര്‍‌‌ത്തകള്‍‌‌‌‌

പണ്ടു വന്ന ചില വാര്‍‌‌ത്തകള്‍‌‌‌‌ (മും‌‌ബൈ കൂട്ടക്കൊലയോട് അനുബന്ധിച്ച്).


Monday, September 21, 2009

രാവേലി

ഈ കഥ പറയലിനെ 'രാവേലി വായിക്കല്‍‌‌‌‌‌‌‌‌‌‌' എന്നാണു ഞങ്ങള്‍‌‌ പറയാറ്. കേട്ടു നോക്കൂ.

Monday, July 20, 2009

വെള്ളപ്പൊക്കം













* നാട്ടില്‍ നിന്നും അനിയന്‍ അയച്ചു തന്ന ചിത്രങ്ങള്‍.

Friday, December 26, 2008

വത്തിക്കാന്‍‌‌












ഇവിടെ ഒരു പാട് ശില്പങ്ങളുണ്ടെങ്കിലും ഈയൊരു ശില്പത്തില്‍ മാത്രമേ കണ്ണിന്റെ കൃഷ്ണമണി കാണുന്നുള്ളു.


ഈ ശില്പത്തിനും ഒരു പ്രത്യേകത ഉണ്ട് :-)










Thursday, December 25, 2008

വെനീസ്സ്

ഒരു ദ്വീപ്


വേറെ ഒരു ദ്വീപ്


പിള്ളേർ ഈ പ്രാവുകളെ ഞെക്കിക്കൊല്ലുമോ?


ഈ വഞ്ചിയില്‍ താഴെക്കാണുന്ന പോലത്തെ വഴികളിലൂടെ കറങ്ങാം


ചെറിയ വഞ്ചികള്ക്ക് പോകാവുന്ന വഴിക‌‌ള്‍

Sunday, August 24, 2008

ചിത്രശലഭങ്ങള്‍ (ഫോട്ടോ)

സാധാരണ ഇവിടെ ചിത്രശലഭങ്ങളെ കാണാറില്ല. കണ്ടിട്ടുള്ളത് വളരെ ചെറിയ, അധികം ഭംഗിയൊന്നുമില്ലാത്ത ഒന്നോ രണ്ടോ എണ്ണത്തിനെ. അതു കൊണ്ട് ഇന്ന് പതിവില്ലാതെ ഒരു ചിത്രശലഭത്തിനെ കണ്ടപ്പോള്‍ ക്യാമറയുമായി അവന്റെ പുറകേ കൂടി. അപ്പോഴാണ്‍ കാണുന്നത് ഒരു പാടെണ്ണമുണ്ട് അവിടെ എന്ന്. ആറ് തരത്തിലുള്ള ചിത്രശലഭങ്ങളെയാണ്‍ ക്യാമറയില്‍ കിട്ടിയത്. ചിത്രങ്ങള്‍ കാണുക.















അവസാനത്തെ ഫോട്ടോയുടെ കൂടിയ റെസല്യൂഷനിലുള്ള ഫോട്ടോ ഇവിടെ അപ്‌‌ലോഡ് ചെയ്തിട്ടുണ്ട്. ഡെസ്ക്ടോപ്പില്‍ ഇടാന്‍ കൊള്ളാം എന്നു തോന്നുന്നു. (അവിടെ കാണുന്ന 'Download Photo' എന്ന ലിന്കില്‍ ക്ലിക്ക് ചെയ്ത് ഡൌണ്ലോഡ് ചെയ്യുക, ഇമേജില്‍ റൈറ്റ് ക്ലിക്ക് ചെയ്ത് സേവ് ചെയ്താല്‍ ചെറിയ ഇമേജ് തന്നെയേ കിട്ടൂ.)