forked from abjennings/gmail-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixdates.py
70 lines (57 loc) · 1.75 KB
/
fixdates.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
# -*- coding: utf-8 -*-
import re
import os
import email
import email.utils
import datetime
import time
import calendar
FILE_RE = re.compile(r"(\d+).eml$")
LAST_DATE_FIXED_FILENAME = "last_email_fixed.dat"
def get_message_ctime(d):
orig_d = d
dt_src = email.utils.parsedate_tz(d)
if not dt_src:
return None
if not dt_src[-1]:
# TZ INFO IS MESSY
if " --" in d:
d = d.replace(" --", " -")
dt_src = email.utils.parsedate_tz(d)
try:
dt = datetime.datetime(*dt_src[:6])
except Exception, e:
print e
print "orig date: %r, curr date: %r, dt_src: %r" % (orig_d, d, dt_src)
return None
if dt_src[-1]:
dt = dt - datetime.timedelta(seconds=dt_src[-1])
dt = datetime.datetime.fromtimestamp(calendar.timegm(dt.timetuple()))
message_ctime = time.mktime(dt.timetuple())
return message_ctime
try:
with open(LAST_DATE_FIXED_FILENAME) as f:
last_file_int_fixed = int(f.read().strip())
except:
last_file_int_fixed = -1
EMAIL_NUMBERS = []
for fname in os.listdir("."):
m = FILE_RE.match(fname)
if m:
file_int = int(m.group(1))
if file_int > last_file_int_fixed:
EMAIL_NUMBERS.append(int(m.group(1)))
EMAIL_NUMBERS.sort()
for i, file_int in enumerate(EMAIL_NUMBERS):
fname = str(file_int) + ".eml"
print "(%d of %d) %s" % (i + 1, len(EMAIL_NUMBERS), fname)
with open(fname) as f:
file_contents = f.read()
message = email.message_from_string(file_contents)
message_ctime = get_message_ctime(message['date'])
if message_ctime:
os.utime(fname, (message_ctime, message_ctime))
if EMAIL_NUMBERS:
f = open(LAST_DATE_FIXED_FILENAME, 'w')
f.write(str(EMAIL_NUMBERS[-1]))
f.close()