3 #Note: We have a custom version of libgmail to fix a bug involving binary attachments (which obviously we have.)
6 warnings
.simplefilter("ignore",DeprecationWarning)
17 os
.system("rm rawdata_*")
22 tregex
= re
.compile("rawdata_([0-9a-f]{32,32})(.*)\.bz2")
23 outp
= commands
.getoutput("s3cmd ls s3://questhelper_data/rawdata_")
24 print "S3 listing snagged"
25 for line
in outp
.split('\n'):
26 if line
== "Bucket 's3://questhelper_data':":
28 serch
= tregex
.search(line
)
34 filehashdict
[toki
] = ext
35 print "Filenames isolated: %d" % len(filehashdict
)
37 ga
= libgmail
.GmailAccount(passwords
.gmail_username
, passwords
.gmail_password
)
40 destination
="./LocalInput/"
41 label
=passwords
.gmail_label
43 argument
= "!label:" + label
+ " has:attachment"
44 argument
= "has:attachment"
45 inbox
=ga
.getMessagesByQuery(argument
)
48 print `
len(inbox
)`
+" messages"
52 for message
in thread
:
54 if thread
.getLabels().count(label
) != 0:
59 print "message "+`i`
+" id: "+message
.id
60 #print thread.getLabels()
61 #print thread.getLabels().count("downloaded")
63 if True: # we used to make sure it had the right label, or more accurately, didn't
65 print '\t'+`
len(message
.attachments
)`
+" attachments"
66 for a
in message
.attachments
:
67 a
.filename
= a
.filename
.encode('ascii', 'ignore').replace('*', '_')
68 print '\t\t filename:', a
.filename
74 #dex=filename.find(".")
75 tup
=a
.filename
.partition(".")
76 name
=pre
+tup
[1]+tup
[2]
77 f
=open(destination
+name
,"w")
80 #message.addLabel("downloaded")
84 s3name
= "rawdata_" + name
+ ".bz2"
85 if not pre
in filehashdict
:
86 # okay, that's cool. Now we S3 it.
87 assert(os
.system("bzip2 -k --best -c \"%s\" > \"%s\"" % (destination
+ name
, s3name
)) == 0)
88 assert(os
.system("s3cmd put \"%s\" s3://questhelper_data/" % (s3name
)) == 0)
89 assert(os
.system("rm rawdata_*") == 0)
91 filehashdict
[pre
] = name
.partition(".")[1] + name
.partition(".")[2] # we only look at the first page of emails, over and over. this way, on the second pass through that page, we'll get and delete instead of just re-storing over and over.
94 s3oldname
= "rawdata_" + pre
+ filehashdict
[pre
] + ".bz2"
95 if s3oldname
!= s3name
:
96 print "\t\t WARNING: Name mismatch! %s vs %s" % (s3name
, s3oldname
)
97 s3cg
= "s3cmd --force get \"s3://questhelper_data/%s\" \"%s\"" % (s3oldname
, s3oldname
)
98 while os
.system(s3cg
) != 0:
99 print "\t\t s3cmd failed, sleeping for 15 seconds . . ."
101 assert(os
.system("cat \"%s\" | bunzip2 > rawdata_temptest" % (s3oldname
)) == 0)
102 assert(os
.system("diff -q rawdata_temptest \"%s\"" % (destination
+ name
)) == 0)
103 assert(os
.system("rm rawdata_temptest \"%s\"" % (s3oldname
)) == 0)
104 assert(os
.system("rm \"%s\"" % (destination
+ name
)) == 0)
106 print "foobared attachment"
111 ga
.trashMessage(message
)
115 thread
.addLabel(label
)
119 inbox
=ga
.getMessagesByQuery(argument
)
122 print `i`
+" messages examined and saved"
124 os
.system("rm rawdata_*")