def post(self): self.response.headers['Content-Type'] = 'application/json;charset=utf-8' account = urllib2.unquote(self.request.get('account')).encode('utf-8') contacts = urllib2.unquote(self.request.get('contacts')).encode('utf-8') accountInfo = json.loads(account) contactInfo = json.loads(contacts) username = accountInfo['email'] access_token = accountInfo['access_token'] g = Gmail() try: g.authenticate(username, access_token) context = {} for contact in contactInfo['contacts']: emails = None emails = g.sent().mail(to=contact) numEmails = len(emails) context[contact]=numEmails g.logout() self.response.out.write(json.dumps(context)) except Exception, e: print "Authentication failure." print "Error:",str(e) return
def getUserID(accountInfo, db): g = Gmail() try: g.authenticate(accountInfo['email'], accountInfo['access_token']) g.logout() userID = cloudsqldb.getUserID(accountInfo['email'], db) return userID except Exception, e: print "Exception: ", str(e) return -1
def post(self): try: self.response.headers['Content-Type'] = 'application/json;charset=utf-8' account = urllib2.unquote(self.request.get('account')).encode('utf-8') contacts = urllib2.unquote(self.request.get('contacts')).encode('utf-8') duration = urllib2.unquote(self.request.get('duration')).encode('utf-8') accountInfo = json.loads(account) contactsInfo = json.loads(contacts) durationInfo = json.loads(duration) username = accountInfo['email'] access_token = accountInfo['access_token'] targets = contactsInfo['targets'] starttime = durationInfo['starttime'] endtime = durationInfo['endtime'] #print "Duration:", starttime, "~", endtime after = datetime.datetime.utcfromtimestamp(starttime) before = datetime.datetime.utcfromtimestamp(endtime) #print "contacts info:", contactsInfo g = Gmail() g.authenticate(username, access_token) for p in range(0, len(targets)): contact = targets[p] #print contact if (starttime!=-1 & endtime!=-1): #print "Before:", before #print "After:", after emails = g.inbox().mail(fr=contact, before=before, after=after) #print "AssignTask for from:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'from', len(emails), starttime, endtime) emails = g.sent().mail(to=contact, before=before, after=after) #print "AssignTask for to:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'to', len(emails), starttime, endtime) elif (starttime!=-1 & endtime==-1): before = datetime.utcnow(); emails = g.inbox().mail(fr=contact, before=before, after=after) #print "AssignTask for from:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'from', len(emails), starttime, endtime) emails = g.sent().mail(to=contact, before=before, after=after) #print "AssignTask for to:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'to', len(emails), starttime, endtime) elif (starttime==-1 & endtime!=-1): emails = g.inbox().mail(fr=contact, before=before, after=after) #print "AssignTask for from:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'from', len(emails), starttime, endtime) emails = g.sent().mail(to=contact, before=before, after=after) #print "AssignTask for to:", contact, ", ", len(emails), "emails between ", starttime, " and ", endtime self.assignTasks(account, contact, 'to', len(emails), starttime, endtime) else: # Get all emails from contact emails = g.inbox().mail(fr=contact) #print "AssignTask for from:", contact, ", ", len(emails), "emails" self.assignTasks(account, contact, 'from', len(emails), starttime, endtime) # Get all emails to contact emails = g.sent().mail(to=contact) #print "AssignTask for to:", contact, ", ", len(emails), "emails" self.assignTasks(account, contact, 'to', len(emails), starttime, endtime) g.logout() except Exception, e: print "Exception: ", e print traceback.format_exc() self.abort(403)
def post(self): context = {} searchType = self.request.get('searchType') contact = self.request.get('contact') start = self.request.get('start') stop = self.request.get('stop') starttime = self.request.get('starttime') endtime = self.request.get('endtime') account = self.request.get('account') accountInfo = json.loads(account) username = accountInfo['email'] access_token= accountInfo['access_token'] start = int(start) stop = int(stop) #print "Worker: start = ", start, ", stop = ", stop #print "starttime = ", starttime, ", endtime = ", endtime starttime = int(starttime) endtime = int(endtime) #print "username = "******"start = ", start, ", stop = ", stop #print "starttime = ", starttime, ", endtime = ", endtime #print "contact:", contact #print "searchType", searchType db = cloudsqldb.connect_db() #Worker is spawn off from RunVerbalucceHandler3, no need to do another authentication userID = cloudsqldb.getUserID(accountInfo['email'], db) try: vblc = verbalucce.Verbalucce() g = Gmail() g.authenticate(username, access_token) emails = None #print contact if (starttime!=-1 & endtime!=-1): #print "Find emails between ", starttime, " and ", endtime after = datetime.datetime.utcfromtimestamp(starttime) before = datetime.datetime.utcfromtimestamp(endtime) if (searchType =='from'): emails = g.inbox().mail(fr=contact, before=before, after=after) FROM = contact TO = username elif (searchType == 'to'): emails = g.sent().mail(to=contact, before=before, after=after) FROM = username TO = contact elif (starttime!=-1 & endtime==-1): #print "Find emails between ", starttime, " and ", endtime after = datetime.datetime.utcfromtimestamp(starttime) before = datetime.datetime.utcnow() if (searchType =='from'): emails = g.inbox().mail(fr=contact, before=before, after=after) FROM = contact TO = username elif (searchType == 'to'): emails = g.sent().mail(to=contact, before=before, after=after) FROM = username TO = contact elif (starttime==-1 & endtime!=-1): #print "Find emails between ", starttime, " and ", endtime after = datetime.datetime.utcfromtimestamp(starttime) before = datetime.datetime.utcfromtimestamp(endtime) if (searchType =='from'): emails = g.inbox().mail(fr=contact, before=before, after=after) FROM = contact TO = username elif (searchType == 'to'): emails = g.sent().mail(to=contact, before=before, after=after) FROM = username TO = contact else: if (searchType =='from'): emails = g.inbox().mail(fr=contact) FROM = contact TO = username elif (searchType == 'to'): emails = g.sent().mail(to=contact) FROM = username TO = contact #print "Number of emails:", len(emails) #print "From:", FROM #print "to:", TO for i in range(start, stop, -1): #print "#", i," email to be fetched" try: emails[i].fetch() except Exception, e: print "Couldn't fetch email #", i print str(e) pass g.logout() for i in range(start, stop, -1): #print "Run verbalucce on #", i email = emails[i] #Only search for emails specifically to USER or #Only search for emails specifically to target contact if (email.to!=None and email.fr!=None and (email.to.lower().find(TO.lower())!=-1) and (email.fr.lower().find(FROM.lower())!=-1) and (len(email.to.split("@")) == 2) and (email.cc==None) and (not(re.search("^Chat with",email.subject))) and email.body!=None and (email.body.find("BEGIN:VCALENDAR")==-1) and (email.calendar==None)): #print "UID:", email.uid if (cloudsqldb.isEmailInDB(userID, email.uid, db)): #print "Email(UID", email.uid,") is found. Skipping.." continue r = vblc.RunVerbalucce(email) timestamp = email.sent_at timestamp = time.mktime(timestamp.timetuple()) cloudsqldb.insertOrUpdateEmailData(userID, email, FROM, TO, timestamp, r, db) #g.logout() db.commit() db.close() print "Successful verbalucce run!"