sheet = GoogleConfig(user, password, dockey, sheetname) gconfig = sheet.fetch() # recipients += gconfig["recipients"] # blacklist += gconfig["blacklist"] print '='*80 # Start processing emails gmail = Gmail(user, password) result = gmail.fetch("logs", blacklist, gconfig["options"]) if (len(result)): mailto, filelist, options = result # Upload logs to api.safecast.org if gconfig["apikey"] != "": api = SafecastAPI(gconfig["apikey"]) for f in filelist: api.setMetadata(os.path.basename(f), gconfig["details"], gconfig["credits"], gconfig["cities"]) api.upload(f) # Create email body reports = processFiles(filelist, options) # Send emails with attachments if recipients != "": # default recipients print "Default recipients =",recipients mailto = mailto + [m.strip() for m in recipients.split(",")] # Cleanup for any duplicates mailto = list(set(mailto))
def fetch(self, folder, blacklist, postfix = ""): self.folder = folder if not os.path.exists("%s" % (self.folder)): os.makedirs("%s" % (self.folder)) # connecting to the gmail imap server logPrint("[GMAIL] Connecting to gmail server ...") m = imaplib.IMAP4_SSL("imap.gmail.com") m.login(self.user,self.pwd) m.select("[Gmail]/All Mail") logPrint("[GMAIL] Selecting all unseen emails ...") resp, items = m.search(None, '(UNSEEN)') # Get the mails id items = items[0].split() items = items[:1] # only one by one result = [] options = Options() options.language = "jp" options.charset = "iso-2022-jp" options.pdf = False options.kml = False options.gpx = False options.csv = False options.world = True # set as default options.time = True # set as default options.distance = True # set as default options.summary = False options.instant = False options.area = False options.peak = False report = 0 for emailid in items: logPrint("[GMAIL] Processing email id %s" % emailid) resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail email_body = data[0][1] # getting the mail content mail = email.message_from_string(email_body) # Check if any attachments at all if mail.get_content_maintype() != 'multipart': continue if mail["Subject"] == None: mail["Subject"] = "" # Add extra options to subject mail["Subject"] += postfix logPrint("[GMAIL] ["+mail["From"]+"] :" + mail["Subject"]) # Check subject for any requests if mail["Subject"].upper().find("[EN]") != -1: options.language = "en" if mail["Subject"].upper().find("[PDF]") != -1: options.pdf = True report += 1 if mail["Subject"].upper().find("[KML]") != -1: options.kml = True report += 1 if mail["Subject"].upper().find("[GPX]") != -1: options.gpx = True report += 1 if mail["Subject"].upper().find("[CSV]") != -1: options.csv = True report += 1 if mail["Subject"].upper().find("[UTF8]") != -1: options.charset = "utf8" if mail["Subject"].upper().find("[JIS]") != -1: options.charset = "shift-jis" if mail["Subject"].upper().find("[WORLD]") != -1: options.world = True if mail["Subject"].upper().find("[SUMMARY]") != -1: options.summary = True if mail["Subject"].upper().find("[SPLIT]") != -1: options.area = True if mail["Subject"].upper().find("[PEAK60]") != -1: options.instant = False options.peak = True if mail["Subject"].upper().find("[PEAK5]") != -1: options.instant = True options.peak = True # If no special type requested, set to default if not report: options.pdf = True options.kml = True # Default recipient is the sender email_pattern = re.compile("[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+") mailto = re.findall(email_pattern, mail["From"]) # Check for emails in "Subject" emails = re.findall(email_pattern, mail["Subject"]) emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto = emails # Check for emails in "To" emails = re.findall(email_pattern, mail["To"]) emails = [e for e in emails if e != self.user] # except user itself emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto = emails # Check for emails in "Cc" if mail["Cc"] != None: emails = re.findall(email_pattern, mail["Cc"]) emails = [e for e in emails if e != self.user] # except user itself emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto += emails # Cleanup for any duplicates mailto = list(set(mailto)) # Mark as read m.uid('STORE', emailid, '+FLAGS', '(\Seen)') # Process the parts filelist = [] for part in mail.walk(): # multipart are just containers, so we skip them if part.get_content_maintype() == 'multipart': continue # is this part an attachment ? if part.get('Content-Disposition') is None: continue filename = part.get_filename() counter = 1 # if there is no filename, we create one with a counter to avoid duplicates if not filename: filename = 'part-%03d%s' % (counter, '.LOG') counter += 1 logPrint("[GMAIL] - Fetching %s" % filename) att_path = os.path.join(self.folder, filename) # Write the attachment fp = open(att_path, 'wb') fp.write(part.get_payload(decode=True)) fp.close() if (os.path.splitext(filename)[1]).upper() not in attachment_extensions: logPrint("Check for attached zip file ...") try: filezip = zipfile.ZipFile(att_path, "r") for info in filezip.infolist(): if (os.path.splitext(info.filename)[1]).upper() in attachment_extensions: logname = os.path.join(self.folder, os.path.basename(info.filename)) logPrint(" - %s [%d bytes]" % (logname, info.file_size)) fp = open(logname, "wb") data = filezip.read(info.filename) fp.write(data) fp.close() filelist.append(logname) except: # Wrong file, continue to next attachment continue finally: os.remove(att_path) logPrint("Done.") else: filelist.append(att_path) result = [mailto, filelist, options] # Upload to Safecast API if mail["Subject"].upper().find("[API ") != -1: pattern = re.compile("([a-zA-Z0-9]+)") position = mail["Subject"].upper().find("[API ") + 5 apikey = re.findall(pattern, mail["Subject"][position:])[0] api = SafecastAPI(apikey) for f in filelist: api.setMetadata(os.path.basename(f), "", re.findall(email_pattern, mail["From"])[0], "") api.upload(f) logPrint("[GMAIL] Done.") return result
def fetch(self, folder, blacklist, postfix=""): self.folder = folder if not os.path.exists("%s" % (self.folder)): os.makedirs("%s" % (self.folder)) # connecting to the gmail imap server logPrint("[GMAIL] Connecting to gmail server ...") m = imaplib.IMAP4_SSL("imap.gmail.com") m.login(self.user, self.pwd) m.select("[Gmail]/All Mail") logPrint("[GMAIL] Selecting all unseen emails ...") resp, items = m.search(None, '(UNSEEN)') # Get the mails id items = items[0].split() items = items[:1] # only one by one result = [] options = Options() options.language = "jp" options.charset = "iso-2022-jp" options.pdf = False options.kml = False options.gpx = False options.csv = False options.world = True # set as default options.time = True # set as default options.distance = True # set as default options.summary = False options.instant = False options.area = False options.peak = False report = 0 for emailid in items: logPrint("[GMAIL] Processing email id %s" % emailid) resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail email_body = data[0][1] # getting the mail content mail = email.message_from_string(email_body) # Check if any attachments at all if mail.get_content_maintype() != 'multipart': continue if mail["Subject"] == None: mail["Subject"] = "" # Add extra options to subject mail["Subject"] += postfix logPrint("[GMAIL] [" + mail["From"] + "] :" + mail["Subject"]) # Check subject for any requests if mail["Subject"].upper().find("[EN]") != -1: options.language = "en" if mail["Subject"].upper().find("[PDF]") != -1: options.pdf = True report += 1 if mail["Subject"].upper().find("[KML]") != -1: options.kml = True report += 1 if mail["Subject"].upper().find("[GPX]") != -1: options.gpx = True report += 1 if mail["Subject"].upper().find("[CSV]") != -1: options.csv = True report += 1 if mail["Subject"].upper().find("[UTF8]") != -1: options.charset = "utf8" if mail["Subject"].upper().find("[JIS]") != -1: options.charset = "shift-jis" if mail["Subject"].upper().find("[WORLD]") != -1: options.world = True if mail["Subject"].upper().find("[SUMMARY]") != -1: options.summary = True if mail["Subject"].upper().find("[SPLIT]") != -1: options.area = True if mail["Subject"].upper().find("[PEAK60]") != -1: options.instant = False options.peak = True if mail["Subject"].upper().find("[PEAK5]") != -1: options.instant = True options.peak = True # If no special type requested, set to default if not report: options.pdf = True options.kml = True # Default recipient is the sender email_pattern = re.compile( "[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+") mailto = re.findall(email_pattern, mail["From"]) # Check for emails in "Subject" emails = re.findall(email_pattern, mail["Subject"]) emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto = emails # Check for emails in "To" emails = re.findall(email_pattern, mail["To"]) emails = [e for e in emails if e != self.user] # except user itself emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto = emails # Check for emails in "Cc" if mail["Cc"] != None: emails = re.findall(email_pattern, mail["Cc"]) emails = [e for e in emails if e != self.user] # except user itself emails = [e for e in emails if e not in blacklist] # except if blacklisted if len(emails): mailto += emails # Cleanup for any duplicates mailto = list(set(mailto)) # Mark as read m.uid('STORE', emailid, '+FLAGS', '(\Seen)') # Process the parts filelist = [] for part in mail.walk(): # multipart are just containers, so we skip them if part.get_content_maintype() == 'multipart': continue # is this part an attachment ? if part.get('Content-Disposition') is None: continue filename = part.get_filename() counter = 1 # if there is no filename, we create one with a counter to avoid duplicates if not filename: filename = 'part-%03d%s' % (counter, '.LOG') counter += 1 logPrint("[GMAIL] - Fetching %s" % filename) att_path = os.path.join(self.folder, filename) # Write the attachment fp = open(att_path, 'wb') fp.write(part.get_payload(decode=True)) fp.close() if (os.path.splitext(filename)[1] ).upper() not in attachment_extensions: logPrint("Check for attached zip file ...") try: filezip = zipfile.ZipFile(att_path, "r") for info in filezip.infolist(): if (os.path.splitext(info.filename)[1] ).upper() in attachment_extensions: logname = os.path.join( self.folder, os.path.basename(info.filename)) logPrint(" - %s [%d bytes]" % (logname, info.file_size)) fp = open(logname, "wb") data = filezip.read(info.filename) fp.write(data) fp.close() filelist.append(logname) except: # Wrong file, continue to next attachment continue finally: os.remove(att_path) logPrint("Done.") else: filelist.append(att_path) result = [mailto, filelist, options] # Upload to Safecast API if mail["Subject"].upper().find("[API ") != -1: pattern = re.compile("([a-zA-Z0-9]+)") position = mail["Subject"].upper().find("[API ") + 5 apikey = re.findall(pattern, mail["Subject"][position:])[0] api = SafecastAPI(apikey) for f in filelist: api.setMetadata(os.path.basename(f), "", re.findall(email_pattern, mail["From"])[0], "") api.upload(f) logPrint("[GMAIL] Done.") return result