-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_mails.py
124 lines (100 loc) · 4.84 KB
/
read_mails.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
# Importing libraries
import imaplib
import re
import datetime as dt
from datetime import timedelta
import sys
dates = (dt.datetime.now() - timedelta(days = 1)).strftime("%d-%b-%Y")
user = '[email protected]'
password = sys.argv[1]
imap_url = 'imap.gmail.com'
# Function to get email content part i.e its body part
def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
else:
return msg.get_payload(None, True)
# Function to search for a key value pair
# def search(key, value, con):
# result, data = con.search(None, key, '"{}"'.format(value))
# return data
def search(con):
result, data = con.search(None, '(SENTSINCE {date})'.format(date=dates))
return data
# Function to get the list of emails under this label
def get_emails(result_bytes):
msgs = [] # all the email data are pushed inside an array
for num in result_bytes[0].split():
typ, data = con.fetch(num, '(RFC822)')
msgs.append(data)
return msgs
flag = True
con = imaplib.IMAP4_SSL(imap_url)
con.login(user, password)
con.select('INBOX')
#msgs = get_emails(search('FROM', '[email protected]', con))
msgs = get_emails(search(con))
#print(msgs)
import sys
# sys.stdout = open('file', 'w')
# printing the body
for msg in msgs[::-1]:
for body in msg:
if type(body) is tuple:
# encoding set as utf-8
content = str(body[1], 'ISO 8859-1')
data = str(content)
# Handling errors related to unicodenecode
try:
indexstart = data.find("Subject:")
data2 = data[indexstart + 9: len(data)]
indexend = data2.find("From:")
if re.search("advisories and nightlies", data2[0: indexend].strip()):
#print("outter: ",data2[0: indexend].strip())
indexstart = data.find("ltr")
clean_data = re.sub('<[^<]+?>', '', data[indexstart + 5: len(data)])
clean_data = re.split("--[0-9a-zA-Z]*--$" ,clean_data)
clean_data[0] = clean_data[0].strip()
clean_data[0] = clean_data[0].replace('=C2=A0', ' ').replace('=\r\n', '').replace(':-', '\n').replace("Nightlies", "- Nightlies:")
clean_data[0] = clean_data[0][:clean_data[0].find("JIRA")]
clean_data[0] = re.split("- ",clean_data[0])
for builds in clean_data[0]:
builds=builds.replace('=C2=A0', ' ')
# builds=builds.replace("Nightlies", "\n\n- Nightlies:")
if re.search(" image:", builds):
print(data2[0: indexend].strip())
print("\nAdvisories:")
print("image: ", builds[builds.find("image:"):].strip())
if re.search("rpm", builds):
print("rpm: ", builds[builds.find(":")+2:])
if re.search("extras", builds):
print("extras: ", builds[builds.find(":")+1:].strip())
if re.search("metadata:", builds):
print("metadata: ", builds[builds.find(":")+1:].strip())
if re.search("Nightlies:", builds):
print("\nNightlies:")
if re.search("ppc64le:", builds):
print("ppc64le: ", builds[builds.find("ppc64le:")+9:].strip()) # build
print("ppc64le: ", builds[builds.find("release-ppc64le:")+16:].strip())
build_no= re.search(":[0-9.]+",builds[builds.find("ppc64le:")+9:].strip())
if build_no is not None:
print("build no: ", build_no.group()[1:])
with open("build_no.sh","w") as f:
f.write("#!/bin/bash\nexport NIGHTLY_Z_VARAD="+build_no.group()[1:]+"\n")
f.close()
#print("export NIGHTLY_Z_VARAD=%s" % (pipes.quote(str(build_no.group()[1:]))))
if re.search("s390x:", builds):
print("s390x: ", builds[builds.find(":")+1:].strip())
if re.search("x86_64:", builds):
print("x86_64: ", builds[builds.find(":")+1:].strip())
flag=False
print("\n================================================================================================")
break
#print(builds)
except UnicodeEncodeError as e:
pass
if flag == False:
break
if flag == False:
break
# sys.stdout.close()