Gmail Migration Script chinese

Python October 15th, 2008

?Download sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
 
import cPickle as pickle
import imaplib
import sys
 
from sexp import scan_sexp
 
def savetemp(name, value):
    f = open(name, 'w')
    pickle.dump(value, f)
    f.close()
 
def loadtemp(name):
    try:
        f = open(name, 'r')
        value = pickle.load(f)
        f.close()
        return value
    except:
        return {}
 
HOST = 'imap.gmail.com'
PORT = 993
SOURCE_USER = 'XXX@gmail.com'
SOURCE_PASS = 'XXX'
DEST_USER = 'XXX@gmail.com'
DEST_PASS = 'XXX'
TEMPFILE = 'temp'
 
print 'connecting...'
source = imaplib.IMAP4_SSL(HOST, PORT)
print 'logging in...'
source.login(SOURCE_USER, SOURCE_PASS)
print 'connecting...'
dest = imaplib.IMAP4_SSL(HOST, PORT)
print 'logging in...'
dest.login(DEST_USER, DEST_PASS)
 
labels = {
    '[Gmail]/All Mail': '[Gmail]/All Mail',
}
 
temp = loadtemp(TEMPFILE)
 
for slabel, dlabel in labels.items():
    # creating temp for slabel
    if not temp.has_key(slabel):
        temp[slabel] = {'dest_uids': {}, 'dest_info': {}, 'source_uids': {}}
 
    # printing temp info for slabel
    print 'loaded for %s: "dest_uids": %d, "dest_info": %d, "source_uids": %d' % (slabel,
                                                                                  len(temp[slabel]['dest_uids']),
                                                                                  len(temp[slabel]['dest_info']),
                                                                                  len(temp[slabel]['source_uids']))
 
    print 'selecting source folder %s...' % slabel
    if source.select(slabel)[0] == 'NO':
        print 'error: select failed'
        continue
    else:
        print 'selecting dest folder %s...' % dlabel
        if dest.select(dlabel)[0] == 'NO':
            print 'dest folder not found; creating...'
            if dest.create(dlabel)[0]=='NO' or dest.select(dlabel)[0]=='NO':
                print 'error: could not create folder'
                continue
 
        print 'analyzing existing messages...'
        uids = dest.search(None, 'ALL')[1][0].split()
        if uids:
            i = 1
            length = len(uids)
            for uid in uids:
                # progress
                sys.stdout.write('\r%d/%d' % (i, length))
                sys.stdout.flush()
                i += 1
 
                if not temp[slabel]['dest_uids'].has_key(uid):
                    try:
                        msg_id = scan_sexp(dest.fetch(uid, 'ENVELOPE')[1][0])[1][1][-1]
                        temp[slabel]['dest_uids'][uid] = True
                        temp[slabel]['dest_info'][msg_id] = True
                    except:
                        pass
 
        print '\nwriting dest_uids and dest_info temp...'
        savetemp(TEMPFILE, temp)
 
        print 'migrating...'
        uids = source.search(None, 'ALL')[1][0].split()
        if uids:
            i = 1
            length = len(uids)
            for uid in uids:
                # progress
                sys.stdout.write('\r%d/%d' % (i, length))
                sys.stdout.flush()
                i += 1
 
                if not temp[slabel]['source_uids'].has_key(uid):
                    try:
                        msg_id = scan_sexp(source.fetch(uid, 'ENVELOPE')[1][0])[1][1][-1]
                        if not temp[slabel]['dest_info'].has_key(msg_id):
                            msg = source.fetch(uid, '(RFC822 FLAGS INTERNALDATE)')
                            dest.append(dlabel, \
                                        imaplib.ParseFlags(msg[1][1][:-1]), \
                                        imaplib.Internaldate2tuple(msg[1][1][:-1]), \
                                        msg[1][0][1])
                            temp[slabel]['source_uids'][uid] = True
                        else:
                            temp[slabel]['source_uids'][uid] = True
                    except:
                        pass
 
        print '\nwriting dest_uids and dest_info temp...'
        savetemp(TEMPFILE, temp)
 
try:
    source.close()
    dest.close()
except:
    pass
 
print '\ndone'

Read the rest of this entry »

Tags: