Skip to content

Instantly share code, notes, and snippets.

@ptb
Last active December 12, 2015 12:26
Show Gist options
  • Save ptb/86913acb6b3b8e8d6acf to your computer and use it in GitHub Desktop.
Save ptb/86913acb6b3b8e8d6acf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Based on: http://pastebin.com/ZBq7euid
import email, email.utils, fnmatch, os, re, sys, time
for a, b, c in os.walk("/home/imap-email/store"):
if "tmp" not in a:
for d in fnmatch.filter(c, '[0123456789]*'):
if int(time.time() - (24*60*60)) < os.path.getmtime(os.path.join(a,d)):
e = os.path.join(a, d)
f = email.message_from_file(open(e, "r"))
g = re.sub("\.", "", f.get("Date"))
h = email.utils.parsedate_tz(g)
if h is not None:
i = int(email.utils.mktime_tz(h))
j = 0
k = f.get_all("Received", [g])
for l in k:
m = l.split(";")[-1]
n = email.utils.parsedate_tz(m)
o = int(email.utils.mktime_tz(n))
if (o - i) > (24*60*60):
continue
j = o
break
if (j < 1):
j = i
p = re.compile("([0-9]+)(\..*$)").match(d)
if len(p.groups()) == 2:
q = str(j)+p.groups()[1]
r = os.path.join(a, q)
if not os.path.isfile(r):
os.rename(e, r)
os.utime(r, (j, j))
print "Renamed '"+e+"' to '"+r+"'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment