Attachment 'migrate_gg_v0.1.py'

Download

   1 #!/usr/bin/env python
   2 # author: jessinio
   3 # version: v0.1
   4 # changelog:
   5 #  * compose by muitl code files
   6 
   7 import os, sys
   8 import traceback
   9 import re
  10 import subprocess
  11 import time
  12 import email as emaillib
  13 import getpass, imaplib
  14 
  15 
  16 # change email date to float time
  17 # Target: sort email by exist time
  18 def getEmailTime(TimeStr):
  19     #format : Fri, 10 Oct 2008 07:50:52
  20      try:
  21          FloatTime = time.mktime(time.strptime(TimeStr, "%a, %d %b %Y %H:%M:%S"))
  22      except:
  23          #other condition: by 10.114.170.12 with HTTP; Wed, 30 Jul 2008 20:44:36 -0700 (PDT)
  24          TimeStr = TimeStr.split(";")[1].strip()
  25          FloatTime = time.mktime(time.strptime(TimeStr, "%a, %d %b %Y %H:%M:%S"))
  26          assert FloatTime
  27      return FloatTime
  28 
  29  
  30 
  31 ##############################################################
  32 #          main function: send email file to MDA
  33 ##############################################################
  34 
  35 # send email (format) to MDA program
  36 def sendMbox():
  37     try:
  38         GroupName = raw_input("which group will be send to?")
  39     except:
  40         print "Can't get group name!"
  41         sys.exit()
  42     if GroupName == "\n":
  43         print "Can't get group name!"
  44         sys.exit()
  45 
  46     MDAPath = "/usr/local/mailman/mail/mailman"
  47     try:
  48         raw_input("which MDA do you want?[return to %s]" % MDAPath)
  49     except:
  50         pass
  51     if not os.path.exists(MDAPath):
  52         print "no MDA! stupid"
  53         sys.exit()
  54     try:
  55         raw_input("where is the mbox file?")
  56     except:
  57         print "Can't get mbox file"
  58         sys.exit()
  59     if not os.path.exists( GoogleMailFile):
  60         print "Can't get mbox file"
  61         sys.exit()
  62     email_file = open(GoogleMailFile, "r")
  63     email_context = email_file.read()
  64     email_file.close()
  65 
  66 
  67     EmailMemory = []
  68     for EmailStr in email_context.split("#" * 20 + "\n"):
  69         # maybe match blank string, so continue
  70         if len(EmailStr) == 0:
  71             continue
  72         # get email time from mail message object
  73         emsg =  emaillib.message_from_string(EmailStr)
  74         FloatTime = getEmailTime(emsg["Received"].split("\n")[-1][:-12].strip())
  75         EmailMemory.append((FloatTime, EmailStr))
  76 
  77     # sort!
  78     EmailMemory.sort()
  79 
  80     # call MDA program
  81     def sendToMDA(GName):
  82             os.system("cat /tmp/temp_email_file.eml |%s post %s" % (MDAPath, GroupName))
  83             print "do one....."
  84     # save each email (format) file to disk, it will be read by MDA
  85     def saveToFile(EmailStr):
  86          temp_file = open("/tmp/temp_email_file.eml","w")
  87          temp_file.write(EmailStr)
  88          temp_file.close()
  89 
  90     for EmailStr in EmailMemory:
  91         #print EmailStr[0]
  92         saveToFile(EmailStr[1])
  93         sendToMDA(GName)
  94         time.sleep(3)
  95 
  96 ##############################################################
  97 #            main function: download file
  98 ##############################################################
  99 
 100 # download gmail email (format) to localhost file
 101 def downMail():
 102     ImapServerName = "imap.gmail.com"
 103     ImapServerPort = 993
 104     UserName = None
 105     UserName = raw_input("User Name: ")
 106     # get runing program user name
 107     if not UserName :
 108         print "auto get %s" % getpass.getuser()
 109         UserName = getpass.getuser()
 110     print "input user ",
 111     PassWord = getpass.getpass()
 112     M = imaplib.IMAP4_SSL(ImapServerName, ImapServerPort)
 113     try:
 114         M.login(UserName, PassWord)
 115     except:
 116         print "Cant't login %s" % ImapServerName
 117         print "Please check user name and password, or network"
 118         sys.exit()
 119     try:
 120         LabelName = raw_input("which label do you want download?[return to Inbox]")
 121     except:
 122         LabelName = "Inbox"
 123     if not LabelName:
 124         LabelName = "Inbox"
 125 
 126     # get label emails' ID
 127     M.select(LabelName)
 128     try:
 129         typ, data = M.search(None, 'ALL')
 130     except:
 131         print "Can't search this label"
 132         sys.exit()
 133     
 134     # Get output file's path
 135     try:
 136         MboxFile = raw_input("which do you want to save email file?[return to /tmp/%s.box" % \
 137                                 LabelName)
 138     except:
 139         MboxFile = "/tmp/%s.box" % LabelName
 140     if not MboxFile:
 141         MboxFile = "/tmp/%s.box" % LabelName
 142     if os.path.exists(MboxFile) and not os.path.basename(MboxFile):
 143         print "not exist this path: %s" % os.path.basename(MboxFile)
 144         sys.exit()
 145     # create output file
 146     try:
 147         OutFile = open(MboxFile,"w+")
 148     except:
 149         print "Can't create file: %s" % MboxFile
 150         print "Please check permission"
 151         sys.exit()
 152     
 153     # write emails to file though emails' ID
 154     for num in data[0].split():
 155         typ, data = M.fetch(num, '(RFC822)')
 156         print "writing.... %s" % data[0][0]
 157         OutFile.write('%s\n%s\n' % ("#" * 20, data[0][1]))
 158 
 159     # exit program
 160     OutFile.close()
 161     M.close()
 162     M.logout()
 163 ##############################################################
 164 #     main function:
 165 #     make mbox file
 166 ##############################################################
 167 
 168 def makeMbox():
 169     OldMboxFileStr = raw_input("where is the mbox file ?")
 170     NewMboxFileStr = raw_input("which mbox will be save to")
 171     try:
 172         NewMboxFile = open(NewMboxFileStr,"w")
 173         OldMboxFile = open(OldMboxFileStr, "r")
 174         email_context = OldMboxFile.read()
 175         OldMboxFile.close()
 176         EmailMemory = []
 177         for EmailStr in email_context.split("#" * 20 + "\n")[1:]:
 178             # get email time from mail message object
 179             emsg =  emaillib.message_from_string(EmailStr)
 180             Sender = emsg["From"]
 181             FloatTime = getEmailTime(emsg["Received"].split("\n")[-1][:-12].strip())
 182             EmailMemory.append((FloatTime, emsg))
 183         EmailMemory.sort()
 184         
 185         patten = re.compile("<(.*?@.*?)>")
 186         for FloatTime, emsg in EmailMemory:
 187             FromStr = emsg["From"]
 188             try:
 189                 FromStr = patten.search(FromStr).group(1) 
 190             except:
 191                 pass
 192             emsgStr = "From " + FromStr + " " + time.strftime("%a %b %d %H:%M:%S %Y", time.localtime(FloatTime)) + "\n" + emsg.as_string()
 193             NewMboxFile.write(emsgStr)
 194         NewMboxFile.close()
 195 
 196     except:
 197         traceback.print_exc()
 198         sys.exit()
 199 
 200 
 201 
 202 
 203 
 204 ##############################################################
 205 #     interview code:
 206 #     choice: call main function
 207 ##############################################################
 208 
 209 if __name__ == "__main__":
 210     while 1:
 211         print "what can I do for you?"
 212         print "d/D: download email to localhost mbox format"
 213         print "s/S: send localhost mbox file to MDA"
 214         print "m/M: make mbox file which can be use be maillist software"
 215         try:
 216             answer = raw_input("which are your choice?")
 217         except:
 218             print "No input! thanks you run me!"
 219             sys.exit()
 220         if  answer.upper() == "D":
 221             downMail()
 222         elif answer.upper() == "S":
 223             sendMbox()
 224         elif answer.upper() == "M":
 225             makeMbox()
 226         else:
 227             print "thanks you run me!"
 228             sys.exit()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-05-11 08:52:12, 71.8 KB) [[attachment:cpc-mailman.png]]
  • [get | view] (2021-05-11 08:52:12, 6.9 KB) [[attachment:migrate_gg_v0.1.py]]
  • [get | view] (2021-05-11 08:52:12, 33.3 KB) [[attachment:snap-gg-thread.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.