-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmail.py
177 lines (146 loc) · 4.13 KB
/
readmail.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys; reload(sys); sys.setdefaultencoding('utf-8')
import sys
from pprint import pprint
import email_interface
import docs_interface
import tags # tags.tags = { u"name": ['nicknames', ...], ... }
import re
try:
from local_settings import *
except:
EMAIL = 'somemail@gmail.com'
PASSWORD = 'somepassword'
SPREADSHEET = 'someuglylookinggooglespreadsheethash'
DEBUG = True
email_interface.DEBUG = DEBUG
docs_interface.EMAIL = email_interface.EMAIL = EMAIL
docs_interface.PASSWORD = email_interface.PASSWORD = PASSWORD
docs_interface.SPREADSHEET = dict(
key = SPREADSHEET,
sheet = 'od6'
)
def detect_numbers(line):
num1 = ur'(^|\s)([0-9]{3,})(\s|$)'
num2 = ur'(^|\s)([0-9]{1,})\s*(тр|К|K|k|к|ТР)(\s|$)'
m = re.search(num1, line)
if not m:
m = re.search(num2, line)
if m:
result = int(m.group(2))
try:
m.group(3)
except:
return result
if m.group(3):
if 'кkКK'.find(m.group(3)) >= 0 or len(m.group(3)) > 1:
result *= 1000
return result
num3 = r'([0-9+]{5,})'
m = re.search(num3, line)
if m:
results = [int(x) for x in m.group(1).split('+')]
sum=0
for i in results:
sum+=i
return sum
return None #''
def is_report(lineset):
if re.search(ur'Состояние', lineset[0]):
return True
if re.search(ur'Финплан', lineset[0]):
return True
if re.search(ur'Отчёт', lineset[0]):
return True
if re.search(ur'Отчет', lineset[0]):
return True
if re.search(ur'Инструкция', lineset[0]):
return True
return False
def checkout():
for i in email_interface.iterate_gmail():
for l in i['lines']:
print '"%s",' % l
def test_detect_numbers():
import debug_data
print
for i in debug_data.data:
print '%s\t\t%s' % (str(detect_numbers(i)), i)
print
# работа с тагами
def prepare_tags():
taglist = []
for t in tags.tags.items():
taglist.append( (unicode(t[0]).lower(), t[0]) )
for tt in t[1]:
taglist.append( (unicode(tt).lower(), t[0]) )
return taglist
def prepare_tags_regex(taglist):
r = []
for tag, name in taglist:
r.append( '('+tag+')' )
return ur'(\s)' + '|'.join(r) + '\s'
def find_tag(taglist, tagre, line):
m = re.search(tagre, unicode(' '+line+' ').lower())
if m:
i = m.lastindex-2
tag, name = taglist[i]
return name
return ''# None
def test_detect_tags():
import debug_data
t = prepare_tags()
tre = prepare_tags_regex(t)
print tre
print
for i in debug_data.data:
print '%s\t\t%s' % (str(find_tag(t, tre, i)), i)
print
# готовимся к выводу
def comment(item, line=None):
r = []
if line and item['lines'][0] != line:
r = [line, ' == ']
r.append( '; '.join(item['lines']) )
return "".join(r)
def proceed_iterator():
tags = prepare_tags()
tags_regex = prepare_tags_regex(tags)
for i in email_interface.iterate_gmail():
# получили письмо, и если оно не по шаблону отчёта, то обрабатываем
if not is_report(i['lines']):
is_processed = False
tag = ' '
for l in i['lines']:
# каждую строчку отдельно: ищем сумму и таг
# если сумма не нулевая, то мы заносим её в общий реестр
amount = detect_numbers(l)
new_tag = find_tag(tags, tags_regex, l)
if tag == ' ' and new_tag is not None:
tag = new_tag
if amount is not None:
is_processed = True
yield dict(
datetime= i['date'], #.strftime('%d.%m.%y')
tag= tag,
amount = amount,
comments = comment(i, l)
)
# если ни одной суммы в письме не нашли, то заносим её в дополнительный реестр
if not is_processed:
yield dict(
datetime = i['date'],
tag='', amount='',
comments = comment(i)
)
def main():
for line in proceed_iterator():
line['month'] = line['datetime'].strftime('%Y-%m')
line['date'] = line['datetime'].strftime('%Y-%m-%d')
del line['datetime']
# print line['date'], line['tag'], '\t', line['amount'], '\t', line['comments']
docs_interface.push(line)
main()
# test_detect_numbers()
# test_detect_tags()